Skip to content
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

feat: add joins in list query #82

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions internal/store/postgres/resource_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
"github.com/goto/entropy/pkg/errors"
)

const listResourceByFilterQuery = `SELECT r.id, r.urn, r.kind, r.name, r.project, r.created_at, r.updated_at, r.spec_configs, r.state_status, r.state_output, r.state_module_data, r.state_next_sync, r.state_sync_result, r.created_by, r.updated_by,
array_agg(rt.tag)::text[] AS tags,
jsonb_object_agg(COALESCE(rd.dependency_key, ''), d.urn) AS dependencies
FROM resources r
LEFT JOIN resource_dependencies rd ON r.id = rd.resource_id
LEFT JOIN resources d ON rd.depends_on = d.id
LEFT JOIN resource_tags rt ON r.id = rt.resource_id
WHERE ($1 = '' OR r.project = $1)
AND ($2 = '' OR r.kind = $2)
GROUP BY r.id
`

type resourceModel struct {
ID int64 `db:"id"`
URN string `db:"urn"`
Expand All @@ -30,6 +42,64 @@ type resourceModel struct {
StateSyncResult json.RawMessage `db:"state_sync_result"`
}

type ListResourceByFilterRow struct {
ID int64
Urn string
Kind string
Name string
Project string
CreatedAt *time.Time
UpdatedAt *time.Time
SpecConfigs []byte
StateStatus string
StateOutput []byte
StateModuleData []byte
StateNextSync *time.Time
StateSyncResult []byte
CreatedBy string
UpdatedBy string
Tags []byte
Dependencies []byte
}

func listResourceByFilter(ctx context.Context, db *sqlx.DB, project, kind string) ([]ListResourceByFilterRow, error) {
rows, err := db.QueryContext(ctx, listResourceByFilterQuery, project, kind)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListResourceByFilterRow
for rows.Next() {
var i ListResourceByFilterRow
if err := rows.Scan(
&i.ID,
&i.Urn,
&i.Kind,
&i.Name,
&i.Project,
&i.CreatedAt,
&i.UpdatedAt,
&i.SpecConfigs,
&i.StateStatus,
&i.StateOutput,
&i.StateModuleData,
&i.StateNextSync,
&i.StateSyncResult,
&i.CreatedBy,
&i.UpdatedBy,
&i.Tags,
&i.Dependencies,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}

func readResourceRecord(ctx context.Context, r sqlx.QueryerContext, urn string, into *resourceModel) error {
cols := []string{
"id", "urn", "kind", "project", "name", "created_at", "updated_at", "created_by", "updated_by",
Expand Down
116 changes: 89 additions & 27 deletions internal/store/postgres/resource_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"encoding/json"
"regexp"
"strings"
"time"

sq "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -72,46 +74,60 @@ func (st *Store) GetByURN(ctx context.Context, urn string) (*resource.Resource,
}

func (st *Store) List(ctx context.Context, filter resource.Filter) ([]resource.Resource, error) {
q := sq.Select("urn").From(tableResources)
if filter.Kind != "" {
q = q.Where(sq.Eq{"kind": filter.Kind})
}
if filter.Project != "" {
q = q.Where(sq.Eq{"project": filter.Project})
ListResourceByFilterRows, err := listResourceByFilter(ctx, st.db, filter.Project, filter.Kind)
if err != nil {
return nil, err
}

if len(filter.Labels) > 0 {
tags := labelMapToTags(filter.Labels)
q = q.Join("resource_tags ON resource_id=id").
Where(sq.Eq{"tag": tags}).
GroupBy("urn").
Having("count(*) >= ?", len(tags))
}
var result []resource.Resource
for _, res := range ListResourceByFilterRows {
var nextSyncAt *time.Time
if res.StateNextSync != nil && !res.StateNextSync.IsZero() {
nextSyncAt = res.StateNextSync
}

rows, err := q.PlaceholderFormat(sq.Dollar).RunWith(st.db).QueryContext(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
var syncResult resource.SyncResult
if len(res.StateSyncResult) > 0 {
if err := json.Unmarshal(res.StateSyncResult, &syncResult); err != nil {
return nil, err
}
}
return nil, err
}
defer rows.Close()

var res []resource.Resource
for rows.Next() {
var urn string
if err := rows.Scan(&urn); err != nil {
deps, err := depsBytesToMap(res.Dependencies)
if err != nil {
return nil, err
}

r, err := st.GetByURN(ctx, urn)
tagsMap, err := byteArrayToMap(res.Tags)
if err != nil {
return nil, err
}
res = append(res, *r)

result = append(result, resource.Resource{
URN: res.Urn,
Kind: res.Kind,
Name: res.Name,
Project: res.Project,
Labels: tagsMap,
CreatedAt: *res.CreatedAt,
UpdatedAt: *res.UpdatedAt,
UpdatedBy: res.UpdatedBy,
CreatedBy: res.CreatedBy,
Spec: resource.Spec{
Configs: res.SpecConfigs,
Dependencies: deps,
},
State: resource.State{
Status: res.StateStatus,
Output: res.StateOutput,
ModuleData: res.StateModuleData,
NextSyncAt: nextSyncAt,
SyncResult: syncResult,
},
})
}

return res, rows.Err()
return result, nil
}

func (st *Store) Create(ctx context.Context, r resource.Resource, hooks ...resource.MutationHook) error {
Expand Down Expand Up @@ -392,3 +408,49 @@ func syncResultAsJSON(syncRes resource.SyncResult) json.RawMessage {
}
return val
}

func byteArrayToMap(data []uint8) (map[string]string, error) {
re := regexp.MustCompile(`(\w+)=(\S+?)(?:,|\}|$)`)
matches := re.FindAllStringSubmatch(string(data), -1)

// Map to store key-value pairs
keyValueMap := make(map[string]string)

if len(matches) == 0 {
if len(data) > 0 {
return nil, errors.New("labels not in the expected format")
}
return keyValueMap, nil
}

// Iterate through matches and populate the map
for _, match := range matches {
key := match[1]
value := match[2]

// Remove quotes from the value if present
value = strings.Trim(value, `"`)

keyValueMap[key] = value
}

return keyValueMap, nil
}

func depsBytesToMap(dependencies []byte) (map[string]string, error) {
deps := map[string]string{}
if len(dependencies) > 0 {
if err := json.Unmarshal(dependencies, &deps); err != nil {
return nil, err
}

for k := range deps {
if k != "" {
break
}
deps = map[string]string{}
}
}

return deps, nil
}
Loading