-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add label endpoint #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1dc69af
chore: remove version from docker compose file
olblak 62d6902
feat: add label endpoint
olblak df7e27a
chore: add label post test case
olblak 6d86bfc
fix: use correctly variable
olblak 01f670d
Merge remote-tracking branch 'upstream/main' into add/label/endpoint
olblak eeab621
feat: allow to filter pipelinereport and scm summary by label
olblak 909540f
fix: test
olblak f593a0f
Merge branch 'main' into add/label/endpoint
olblak 041e82a
chore: various tidy up
olblak 38011db
Merge branch 'add/label/endpoint' of github.com:olblak/udash into add…
olblak 41833c6
chore: various improvements
olblak 4abd834
fix: typo
olblak c53dc5e
chore: simplify code
olblak 0d2cd51
chore: make keyonly param more robust
olblak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/updatecli/udash/pkg/model" | ||
|
|
||
| "github.com/stephenafamo/bob/dialect/psql" | ||
| "github.com/stephenafamo/bob/dialect/psql/im" | ||
| "github.com/stephenafamo/bob/dialect/psql/sm" | ||
| ) | ||
|
|
||
| // InsertLabel creates a new label and inserts it into the database. | ||
| // | ||
| // It returns the ID of the newly created label. | ||
| func InsertLabel(ctx context.Context, key, value string) (string, error) { | ||
| query := psql.Insert( | ||
| im.Into("labels", "key", "value"), | ||
| im.Values(psql.Arg(key), psql.Arg(value)), | ||
| im.Returning("id"), | ||
| ) | ||
|
|
||
| queryString, args, err := query.Build(ctx) | ||
|
|
||
| if err != nil { | ||
| logrus.Errorf("building query failed: %s\n\t%s", queryString, err) | ||
| return "", err | ||
| } | ||
|
|
||
| var id uuid.UUID | ||
| err = DB.QueryRow(ctx, queryString, args...).Scan( | ||
| &id, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| logrus.Errorf("query failed: %q\n\t%s", queryString, err) | ||
| return "", err | ||
| } | ||
|
|
||
| return id.String(), nil | ||
| } | ||
|
|
||
| // GetLabelKeyOnlyRecords returns a list of labels from the labels database table. | ||
| func GetLabelKeyOnlyRecords(ctx context.Context, startTime, endTime string, limit, page int) ([]string, int, error) { | ||
|
|
||
| query := psql.Select( | ||
| sm.Columns("id", "key", "created_at", "updated_at", "last_pipeline_report_at"), | ||
| sm.From("labels"), | ||
| sm.OrderBy("key"), | ||
| sm.Distinct("key"), | ||
| ) | ||
|
|
||
| if err := applyRangeFilter( | ||
| "last_pipeline_report_at", | ||
| dateRangeFilterParams{ | ||
| Query: &query, | ||
| DateRangeDays: 0, | ||
| StartTime: startTime, | ||
| EndTime: endTime, | ||
| }); err != nil { | ||
| return nil, 0, fmt.Errorf("applying last_pipeline_report_at range filter: %w", err) | ||
| } | ||
|
|
||
| totalCount := 0 | ||
| totalQuery := psql.Select(sm.From(query), sm.Columns("count(*)")) | ||
| totalQueryString, totalArgs, err := totalQuery.Build(ctx) | ||
| if err != nil { | ||
| logrus.Errorf("building total count query failed: %s\n\t%s", totalQueryString, err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| if err = DB.QueryRow(ctx, totalQueryString, totalArgs...).Scan( | ||
| &totalCount, | ||
| ); err != nil { | ||
| logrus.Errorf("parsing total count result: %s", err) | ||
| } | ||
|
|
||
| if limit < totalCount && limit > 0 { | ||
| query.Apply( | ||
| sm.Limit(limit), | ||
| sm.Offset((page-1)*limit), | ||
| ) | ||
| } | ||
|
|
||
| queryString, args, err := query.Build(ctx) | ||
|
|
||
| if err != nil { | ||
| logrus.Errorf("building query failed: %s\n\t%s", queryString, err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| rows, err := DB.Query(ctx, queryString, args...) | ||
| if err != nil { | ||
| logrus.Errorf("query failed: %s\n\t%s", queryString, err) | ||
| return nil, 0, err | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| results := []string{} | ||
|
|
||
| for rows.Next() { | ||
| r := model.Label{} | ||
|
|
||
| err = rows.Scan(&r.ID, &r.Key, &r.CreatedAt, &r.UpdatedAt, &r.LastPipelineReportAt) | ||
| if err != nil { | ||
| logrus.Errorf("scanning label row failed: %s", err) | ||
| continue | ||
| } | ||
|
|
||
| results = append(results, r.Key) | ||
| } | ||
|
|
||
| if err := rows.Err(); err != nil { | ||
| logrus.Errorf("iterating label rows failed: %s", err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| return results, totalCount, nil | ||
| } | ||
|
|
||
| // GetLabelRecords returns a list of labels from the labels database table. | ||
| func GetLabelRecords(ctx context.Context, id, key, value, startTime, endTime string, limit, page int) ([]model.Label, int, error) { | ||
|
|
||
| query := psql.Select( | ||
| sm.Columns("id", "key", "value", "created_at", "updated_at", "last_pipeline_report_at"), | ||
| sm.From("labels"), | ||
| sm.OrderBy("key"), | ||
| ) | ||
|
|
||
| if key != "" { | ||
| query.Apply( | ||
| sm.Where(psql.Quote("key").EQ(psql.Arg(key))), | ||
| ) | ||
| } | ||
|
|
||
| if value != "" { | ||
| query.Apply( | ||
| sm.Where(psql.Quote("value").EQ(psql.Arg(value))), | ||
| ) | ||
| } | ||
|
|
||
| if id != "" { | ||
| query.Apply( | ||
| sm.Where(psql.Quote("id").EQ(psql.Arg(id))), | ||
| ) | ||
| } | ||
|
|
||
| if err := applyRangeFilter( | ||
| "last_pipeline_report_at", | ||
| dateRangeFilterParams{ | ||
| Query: &query, | ||
| DateRangeDays: 0, | ||
| StartTime: startTime, | ||
| EndTime: endTime, | ||
| }); err != nil { | ||
| return nil, 0, fmt.Errorf("applying last_pipeline_report_at range filter: %w", err) | ||
| } | ||
|
|
||
| totalCount := 0 | ||
| totalQuery := psql.Select(sm.From(query), sm.Columns("count(*)")) | ||
| totalQueryString, totalArgs, err := totalQuery.Build(ctx) | ||
| if err != nil { | ||
| logrus.Errorf("building total count query failed: %s\n\t%s", totalQueryString, err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| if err = DB.QueryRow(ctx, totalQueryString, totalArgs...).Scan( | ||
| &totalCount, | ||
| ); err != nil { | ||
| logrus.Errorf("parsing total count result: %s", err) | ||
| } | ||
|
|
||
| if limit < totalCount && limit > 0 { | ||
| query.Apply( | ||
| sm.Limit(limit), | ||
| sm.Offset((page-1)*limit), | ||
| ) | ||
| } | ||
|
|
||
| queryString, args, err := query.Build(ctx) | ||
|
|
||
| if err != nil { | ||
| logrus.Errorf("building query failed: %s\n\t%s", queryString, err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| rows, err := DB.Query(ctx, queryString, args...) | ||
| if err != nil { | ||
| logrus.Errorf("query failed: %s\n\t%s", queryString, err) | ||
| return nil, 0, err | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| results := []model.Label{} | ||
|
|
||
| for rows.Next() { | ||
| r := model.Label{} | ||
|
|
||
| err = rows.Scan(&r.ID, &r.Key, &r.Value, &r.CreatedAt, &r.UpdatedAt, &r.LastPipelineReportAt) | ||
| if err != nil { | ||
| logrus.Errorf("scanning label row failed: %s", err) | ||
| continue | ||
| } | ||
|
|
||
| results = append(results, r) | ||
| } | ||
|
|
||
| if err := rows.Err(); err != nil { | ||
| logrus.Errorf("iterating label rows failed: %s", err) | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| return results, totalCount, nil | ||
| } | ||
|
|
||
| // InitLabels takes a map of labels and ensures that they exist in the database, creating them if necessary. | ||
| func InitLabels(ctx context.Context, labels map[string]string) ([]uuid.UUID, error) { | ||
| errs := []error{} | ||
| labelIDs := []uuid.UUID{} | ||
|
|
||
| for labelKey, labelValue := range labels { | ||
| if labelKey == "" { | ||
| errs = append(errs, fmt.Errorf("missing key, ignoring label:\t%q:%q", labelKey, labelValue)) | ||
| continue | ||
| } | ||
|
|
||
| if labelValue == "" { | ||
| errs = append(errs, fmt.Errorf("missing value, ignoring label:\t%q:%q", labelKey, labelValue)) | ||
| continue | ||
| } | ||
|
|
||
| labelRecords, totalCount, err := GetLabelRecords(ctx, "", labelKey, labelValue, "", "", 0, 1) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("failed to get labels: %s", err)) | ||
| continue | ||
| } | ||
|
|
||
| switch totalCount { | ||
| case 0: | ||
| id, err := InsertLabel(ctx, labelKey, labelValue) | ||
| if err != nil { | ||
| err := fmt.Errorf("insert label data: %s", err) | ||
| errs = append(errs, err) | ||
| continue | ||
| } | ||
|
|
||
| parsedID, err := uuid.Parse(id) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("parsing id: %s", err)) | ||
| continue | ||
| } | ||
|
|
||
| labelIDs = append(labelIDs, parsedID) | ||
| case 1: | ||
| if labelValue == labelRecords[0].Value { | ||
| labelIDs = append(labelIDs, labelRecords[0].ID) | ||
| } | ||
| default: | ||
| errMsg := fmt.Errorf("something went wrong multiple labels found for key %s", labelKey) | ||
| logrus.Error(errMsg) | ||
| errs = append(errs, errMsg) | ||
| } | ||
| } | ||
|
|
||
| if len(errs) > 0 { | ||
| for i := range errs { | ||
| logrus.Errorln(errs[i]) | ||
| } | ||
| return nil, fmt.Errorf("something went wrong during label creation") | ||
| } | ||
|
|
||
| return labelIDs, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/stephenafamo/bob" | ||
| "github.com/stephenafamo/bob/dialect/psql" | ||
| "github.com/stephenafamo/bob/dialect/psql/dialect" | ||
| "github.com/stephenafamo/bob/dialect/psql/sm" | ||
| ) | ||
|
|
||
| // labelFilterParams holds parameters for applying a date range filter to a query. | ||
| type labelFilterParams struct { | ||
| Query *bob.BaseQuery[*dialect.SelectQuery] | ||
| Labels map[string]string | ||
| StartTime string | ||
| EndTime string | ||
| Ctx context.Context | ||
| } | ||
|
|
||
| func applyLabelFilter(params labelFilterParams) error { | ||
|
|
||
| if params.Ctx == nil { | ||
| params.Ctx = context.Background() | ||
| } | ||
|
|
||
| if len(params.Labels) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| errs := []error{} | ||
| for key, value := range params.Labels { | ||
| if key == "" { | ||
| errs = append(errs, fmt.Errorf("label key cannot be empty")) | ||
| continue | ||
| } | ||
|
|
||
| results, totalCounts, err := GetLabelRecords( | ||
| params.Ctx, | ||
| "", | ||
| key, | ||
| value, | ||
| params.StartTime, | ||
| params.EndTime, | ||
olblak marked this conversation as resolved.
Show resolved
Hide resolved
olblak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 0, | ||
| 1, | ||
| ) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("failed getting label records: %s", err)) | ||
| continue | ||
| } | ||
|
|
||
| if totalCounts == 0 { | ||
| if value == "" { | ||
| errs = append(errs, fmt.Errorf("label not found for key %s", key)) | ||
| } else { | ||
| errs = append(errs, fmt.Errorf("label not found for %s=%s", key, value)) | ||
| } | ||
| continue | ||
olblak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| ids := make([]string, 0, len(results)) | ||
| for i := range results { | ||
| ids = append(ids, results[i].ID.String()) | ||
| } | ||
|
|
||
| if len(ids) == 0 { | ||
| if value == "" { | ||
| errs = append(errs, fmt.Errorf("no label ids found for key %s", key)) | ||
| } else { | ||
| errs = append(errs, fmt.Errorf("no label ids found for %s=%s", key, value)) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| params.Query.Apply( | ||
| sm.Where( | ||
| psql.Raw(`label_ids && ?`, fmt.Sprintf("{%s}", strings.Join(ids, ","))), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| if len(errs) > 0 { | ||
| return fmt.Errorf("errors occurred while applying label filter: %v", errs) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| DROP INDEX IF EXISTS idx_labels_key; | ||
| DROP INDEX IF EXISTS idx_labels_value; | ||
| ALTER TABLE IF EXISTS labels DROP CONSTRAINT IF EXISTS labels_key_value_unique; | ||
| DROP TABLE IF EXISTS labels; |
19 changes: 19 additions & 0 deletions
19
pkg/database/migrations/000007_create_labels_tables.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| -- This migration will create the labels table in the database, which will store the labels associated with the database. | ||
| BEGIN; | ||
| CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
| CREATE TABLE IF NOT EXISTS labels( | ||
| id uuid DEFAULT uuid_generate_v4 (), | ||
| key VARCHAR NOT NULL, | ||
| value VARCHAR NOT NULL, | ||
| created_at TIMESTAMP, | ||
| updated_at TIMESTAMP, | ||
| last_pipeline_report_at TIMESTAMP, | ||
| CONSTRAINT labels_key_value_unique UNIQUE (key, value) | ||
| ); | ||
| ALTER TABLE labels ALTER COLUMN created_at SET DEFAULT now(); | ||
| ALTER TABLE labels ALTER COLUMN updated_at SET DEFAULT now(); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_labels_key ON labels (key); | ||
| CREATE INDEX IF NOT EXISTS idx_labels_value ON labels (value); | ||
|
|
||
| COMMIT; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.