Skip to content

Commit

Permalink
Added additional data to the /layers route (#239)
Browse files Browse the repository at this point in the history
* feat(api): add runCount to layer object

* feat(api): added UID to /layers

* feat(api): added latestRuns & lastRunAt in the /layers

* fix(api): send only the 5 latest runs for /layers
  • Loading branch information
fabiopadok authored Feb 19, 2024
1 parent eeae5b2 commit 4833922
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions internal/server/api/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ import (
)

type layer struct {
UID string `json:"uid"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Path string `json:"path"`
State string `json:"state"`
RunCount int `json:"runCount"`
LastRunAt string `json:"lastRunAt"`
LastResult string `json:"lastResult"`
IsRunning bool `json:"isRunning"`
IsPR bool `json:"isPR"`
LatestRuns []run `json:"latestRuns"`
}

type run struct {
ID string `json:"id"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
Status string `json:"status"`
LastRun string `json:"lastRun"`
Retries int `json:"retries"`
}

type layersResponse struct {
Expand All @@ -43,16 +57,21 @@ func (a *API) LayersHandler(c echo.Context) error {
}
results := []layer{}
for _, l := range layers.Items {
layerRunning, runCount, latestRuns, lastRunAt := a.getLayerRunInfo(l)
results = append(results, layer{
UID: string(l.UID),
Name: l.Name,
Namespace: l.Namespace,
Repository: fmt.Sprintf("%s/%s", l.Spec.Repository.Namespace, l.Spec.Repository.Name),
Branch: l.Spec.Branch,
Path: l.Spec.Path,
State: a.getLayerState(l),
RunCount: runCount,
LastRunAt: lastRunAt,
LastResult: l.Status.LastResult,
IsRunning: a.isLayerRunning(l),
IsRunning: layerRunning,
IsPR: a.isLayerPR(l),
LatestRuns: latestRuns,
})
}
return c.JSON(http.StatusOK, &layersResponse{
Expand Down Expand Up @@ -84,21 +103,39 @@ func (a *API) getLayerState(layer configv1alpha1.TerraformLayer) string {
return state
}

func (a *API) isLayerRunning(layer configv1alpha1.TerraformLayer) bool {
func (a *API) getLayerRunInfo(layer configv1alpha1.TerraformLayer) (layerRunning bool, runCount int, latestRuns []run, lastRunAt string) {
runs := &configv1alpha1.TerraformRunList{}
requirement, _ := labels.NewRequirement("burrito/managed-by", selection.Equals, []string{layer.Name})
selector := labels.NewSelector().Add(*requirement)
err := a.Client.List(context.Background(), runs, client.MatchingLabelsSelector{Selector: selector})
runCount = len(runs.Items)
layerRunning = false
if err != nil {
log.Errorf("could not list terraform runs, returning false: %s", err)
return false
return
}
for _, r := range runs.Items {
lastRunAt = runs.Items[len(runs.Items)-1].Status.LastRun
for i := len(runs.Items)-1 ; i >= 0 ; i-- {
r := runs.Items[i]
if r.Status.State == "Running" {
return true
layerRunning = true
if (len(runs.Items)-1-i) >= 5 {
return
}
}
if (len(runs.Items)-1-i) < 5 {
latestRuns = append(latestRuns, run{
ID: string(r.UID),
Name: r.Name,
Namespace: r.Namespace,
Action: r.Spec.Action,
Status: r.Status.State,
LastRun: r.Status.LastRun,
Retries: r.Status.Retries,
})
}
}
return false
return
}

func (a *API) isLayerPR(layer configv1alpha1.TerraformLayer) bool {
Expand Down

0 comments on commit 4833922

Please sign in to comment.