Skip to content

Commit 9011e0f

Browse files
committed
feat: add list of builds per template
1 parent f359e53 commit 9011e0f

File tree

14 files changed

+718
-127
lines changed

14 files changed

+718
-127
lines changed

packages/api/internal/api/api.gen.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/spec.gen.go

Lines changed: 107 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/types.gen.go

Lines changed: 65 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/handlers/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
// This is a security measure to prevent the use of weak secrets (like empty).
4040
const minSupabaseJWTSecretLength = 16
4141

42+
var _ api.ServerInterface = (*APIStore)(nil)
43+
4244
type APIStore struct {
4345
Healthy bool
4446
config cfg.Config

packages/api/internal/handlers/template_build_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem
6565

6666
// early return if still waiting for build start
6767
if buildInfo.BuildStatus == envbuild.StatusWaiting {
68-
result := api.TemplateBuild{
68+
result := api.TemplateBuildInfo{
6969
LogEntries: make([]api.BuildLogEntry, 0),
7070
Logs: make([]string, 0),
7171
TemplateID: templateID,
@@ -79,7 +79,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem
7979
}
8080

8181
// Needs to be before logs request so the status is not set to done too early
82-
result := api.TemplateBuild{
82+
result := api.TemplateBuildInfo{
8383
LogEntries: nil,
8484
Logs: nil,
8585
TemplateID: templateID,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
8+
"github.com/e2b-dev/infra/packages/api/internal/api"
9+
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
10+
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
11+
)
12+
13+
func (a *APIStore) GetTemplatesTemplateID(c *gin.Context, templateID api.TemplateID) {
14+
ctx := c.Request.Context()
15+
16+
team, apiErr := a.GetTeamAndLimits(c, nil)
17+
if apiErr != nil {
18+
a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg)
19+
telemetry.ReportCriticalError(ctx, "error when getting team and tier", apiErr.Err)
20+
21+
return
22+
}
23+
24+
telemetry.SetAttributes(ctx,
25+
telemetry.WithTeamID(team.ID.String()),
26+
)
27+
28+
template, err := a.sqlcDB.GetTemplateByIDWithAliases(ctx, templateID)
29+
if err != nil {
30+
a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting template")
31+
telemetry.ReportCriticalError(ctx, "error when getting template", err)
32+
33+
return
34+
}
35+
36+
if template.TeamID != team.ID {
37+
a.sendAPIStoreError(c, http.StatusNotFound, "Template not found")
38+
telemetry.ReportCriticalError(ctx, "template not found", nil)
39+
40+
return
41+
}
42+
43+
builds, err := a.sqlcDB.GetTemplateBuilds(ctx, templateID)
44+
if err != nil {
45+
a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting builds")
46+
telemetry.ReportCriticalError(ctx, "error when getting builds", err)
47+
48+
return
49+
}
50+
51+
res := api.TemplateWithBuilds{
52+
TemplateID: template.ID,
53+
Public: template.Public,
54+
Aliases: template.Aliases,
55+
CreatedAt: template.CreatedAt,
56+
UpdatedAt: template.UpdatedAt,
57+
LastSpawnedAt: template.LastSpawnedAt,
58+
SpawnCount: template.SpawnCount,
59+
Builds: make([]api.TemplateBuild, 0, len(builds)),
60+
}
61+
62+
for _, item := range builds {
63+
res.Builds = append(res.Builds, api.TemplateBuild{
64+
BuildID: item.ID,
65+
Status: api.TemplateBuildStatus(item.Status),
66+
CreatedAt: item.CreatedAt,
67+
UpdatedAt: item.UpdatedAt,
68+
FinishedAt: item.FinishedAt,
69+
CpuCount: utils.ToPtrIfNotEqual(api.CPUCount(item.Vcpu), 0),
70+
MemoryMB: utils.ToPtrIfNotEqual(api.MemoryMB(item.RamMb), 0),
71+
DiskSizeMB: utils.CastPtr(item.TotalDiskSizeMb, func(v int64) api.DiskSizeMB { return api.DiskSizeMB(v) }),
72+
EnvdVersion: item.EnvdVersion,
73+
})
74+
}
75+
76+
c.JSON(http.StatusOK, res)
77+
}

packages/db/queries/get_template_by_id.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22
SELECT t.*
33
FROM "public"."envs" t
44
WHERE t.id = $1;
5+
6+
-- name: GetTemplateByIDWithAliases :one
7+
SELECT e.*, al.aliases
8+
FROM "public"."envs" e
9+
CROSS JOIN LATERAL (
10+
SELECT array_agg(alias)::text[] AS aliases
11+
FROM public.env_aliases
12+
WHERE env_id = e.id
13+
) AS al
14+
WHERE e.id = $1;

packages/db/queries/get_template_by_id.sql.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: GetTemplateBuilds :many
2+
SELECT eb.*
3+
FROM public.env_builds eb
4+
WHERE eb.env_id = sqlc.arg(template_id)
5+
ORDER BY eb.finished_at DESC;

packages/db/queries/get_template_with_builds.sql.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)