From 48339221de7208384a2c250501cc18f87c918091 Mon Sep 17 00:00:00 2001 From: Fabio Benoit <156663372+fabiopadok@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:00:24 +0100 Subject: [PATCH] Added additional data to the /layers route (#239) * 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 --- internal/server/api/layers.go | 49 ++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/internal/server/api/layers.go b/internal/server/api/layers.go index 213e0774..7a7804f9 100644 --- a/internal/server/api/layers.go +++ b/internal/server/api/layers.go @@ -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 { @@ -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{ @@ -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 {