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

added utils for filter #1421

Merged
merged 14 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
12 changes: 6 additions & 6 deletions controller/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (ctrl *Controller) ListAccounts(c context.Context) (accounts []models.Account, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &accounts, nil)
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &accounts, nil, "")
return
}

Expand All @@ -20,32 +20,32 @@ func (ctrl *Controller) CountResources(c context.Context, provider, name string)
if name != "" {
conditions = append(conditions, [3]string{"account", "=", name})
}
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCountKey, &output, conditions)
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCountKey, &output, conditions, "")
return
}

func (ctrl *Controller) InsertAccount(c context.Context, account models.Account) (lastId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &account, nil)
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &account, nil, "")
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) RescanAccount(c context.Context, account *models.Account, accountId string) (rows int64, err error) {
res, err := ctrl.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}})
res, err := ctrl.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}}, "")
if err != nil {
return 0, err
}
return res.RowsAffected()
}

func (ctrl *Controller) DeleteAccount(c context.Context, accountId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}})
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}}, "")
return
}

func (ctrl *Controller) UpdateAccount(c context.Context, account models.Account, accountId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}})
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}}, "")
return
}
6 changes: 3 additions & 3 deletions controller/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

func (ctrl *Controller) InsertAlert(c context.Context, alert models.Alert) (alertId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &alert, nil)
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &alert, nil, "")
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) UpdateAlert(c context.Context, alert models.Alert, alertId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAlertKey, &alert, [][3]string{{"id", "=", alertId}})
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAlertKey, &alert, [][3]string{{"id", "=", alertId}},"")
return
}

func (ctrl *Controller) DeleteAlert(c context.Context, alertId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Alert), [][3]string{{"id", "=", alertId}})
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Alert), [][3]string{{"id", "=", alertId}}, "")
return
}
5 changes: 4 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package controller
import (
"context"
"database/sql"

"github.com/tailwarden/komiser/models"
)

type totalOutput struct {
Expand Down Expand Up @@ -30,7 +32,8 @@ type accountOutput struct {
}

type Repository interface {
HandleQuery(context.Context, string, interface{}, [][3]string) (sql.Result, error)
HandleQuery(context.Context, string, interface{}, [][3]string, string) (sql.Result, error)
GenerateFilterQuery(view models.View, queryTitle string, arguments []int64, queryParameter string) ([]string, error)
}

type Controller struct {
Expand Down
44 changes: 38 additions & 6 deletions controller/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,63 @@ import (
)

func (ctrl *Controller) GetResource(c context.Context, resourceId string) (resource models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resource, [][3]string{{"resource_id", "=", resourceId}})
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resource, [][3]string{{"resource_id", "=", resourceId}}, "")
return
}

func (ctrl *Controller) GetResources(c context.Context, idList string) (resources []models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{{"id", "IN", "(" + strings.Trim(idList, "[]") + ")"}})
resources = make([]models.Resource, 0)
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{{"id", "IN", "(" + strings.Trim(idList, "[]") + ")"}}, "")
return
}

func (ctrl *Controller) ListResources(c context.Context) (resources []models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{})
resources = make([]models.Resource, 0)
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{}, "")
return
}

func (ctrl *Controller) CountRegionsFromResources(c context.Context) (regions totalOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{})
_, err = ctrl.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{}, "")
return
}

func (ctrl *Controller) CountRegionsFromAccounts(c context.Context) (accounts totalOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.AccountsResourceCountKey, &accounts, [][3]string{})
_, err = ctrl.repo.HandleQuery(c, repository.AccountsResourceCountKey, &accounts, [][3]string{}, "")
return
}

func (ctrl *Controller) SumResourceCost(c context.Context) (cost costOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCostSumKey, &cost, [][3]string{})
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCostSumKey, &cost, [][3]string{}, "")
return
}

func (ctrl *Controller) ResourceWithFilter(c context.Context, view models.View, arguments []int64, queryParameter string) (resources []models.Resource, err error) {
resources = make([]models.Resource, 0)
queries, err := ctrl.repo.GenerateFilterQuery(view, repository.ListResourceWithFilter, arguments, queryParameter)
if err != nil {
return
}
for _, query := range queries {
_, err = ctrl.repo.HandleQuery(c, repository.ListResourceWithFilter, &resources, [][3]string{}, query)
if err != nil {
return
}
}
return
}

func (ctrl *Controller) RelationWithFilter(c context.Context, view models.View, arguments []int64, queryParameter string) (resources []models.Resource, err error) {
resources = make([]models.Resource, 0)
queries, err := ctrl.repo.GenerateFilterQuery(view, repository.ListResourceWithFilter, arguments, queryParameter)
if err != nil {
return
}
for _, query := range queries {
_, err = ctrl.repo.HandleQuery(c, repository.ListResourceWithFilter, &resources, [][3]string{}, query)
if err != nil {
return
}
}
return
}
34 changes: 29 additions & 5 deletions controller/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,50 @@ import (
)

func (ctrl *Controller) LocationStatsBreakdown(c context.Context) (groups []models.OutputResources, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.LocationBreakdownStatKey, &groups, [][3]string{})
_, err = ctrl.repo.HandleQuery(c, repository.LocationBreakdownStatKey, &groups, [][3]string{}, "")
return
}

func (ctrl *Controller) ListRegions(c context.Context) (regions []regionOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListRegionsKey, &regions, nil)
_, err = ctrl.repo.HandleQuery(c, repository.ListRegionsKey, &regions, nil, "")
return
}

func (ctrl *Controller) ListProviders(c context.Context) (providers []providerOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListProvidersKey, &providers, nil)
_, err = ctrl.repo.HandleQuery(c, repository.ListProvidersKey, &providers, nil, "")
return
}

func (ctrl *Controller) ListServices(c context.Context) (services []serviceOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListServicesKey, &services, nil)
_, err = ctrl.repo.HandleQuery(c, repository.ListServicesKey, &services, nil, "")
return
}

func (ctrl *Controller) ListAccountNames(c context.Context) (accounts []accountOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListAccountsKey, &accounts, nil)
_, err = ctrl.repo.HandleQuery(c, repository.ListAccountsKey, &accounts, nil, "")
return
}

func (ctrl *Controller) StatsWithFilter(c context.Context, view models.View, arguments []int64, queryParameter string) (regionCount totalOutput, resourceCount totalOutput, costCount costOutput, err error) {
queries, err := ctrl.repo.GenerateFilterQuery(view, repository.ListStatsWithFilter, arguments, queryParameter)
if err != nil {
return
}
_, err = ctrl.repo.HandleQuery(c, repository.ListStatsWithFilter, &regionCount, [][3]string{}, queries[0])
if err != nil {
return
}

// for resource count
_, err = ctrl.repo.HandleQuery(c, repository.ListStatsWithFilter, &resourceCount, [][3]string{}, queries[1])
if err != nil {
return
}

// for cost sum
_, err = ctrl.repo.HandleQuery(c, repository.ListStatsWithFilter, &costCount, [][3]string{}, queries[2])
if err != nil {
return
}
return
}
2 changes: 1 addition & 1 deletion controller/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import (

func (ctrl *Controller) UpdateTags(c context.Context, tags []models.Tag, resourceId string) (resource models.Resource, err error) {
resource.Tags = tags
_, err = ctrl.repo.HandleQuery(c, repository.UpdateTagsKey, &resource, [][3]string{{"id", "=", fmt.Sprint(resourceId)}})
_, err = ctrl.repo.HandleQuery(c, repository.UpdateTagsKey, &resource, [][3]string{{"id", "=", fmt.Sprint(resourceId)}}, "")
return
}
14 changes: 7 additions & 7 deletions controller/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ import (
)

func (ctrl *Controller) GetView(c context.Context, viewId string) (view models.View, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &view, [][3]string{{"id", "=", viewId}})
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &view, [][3]string{{"id", "=", viewId}}, "")
return
}

func (ctrl *Controller) ListViews(c context.Context) (views []models.View, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &views, [][3]string{})
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &views, [][3]string{}, "")
return
}

func (ctrl *Controller) InsertView(c context.Context, view models.View) (viewId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &view, nil)
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &view, nil, "")
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) UpdateView(c context.Context, view models.View, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewKey, &view, [][3]string{{"id", "=", viewId}})
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewKey, &view, [][3]string{{"id", "=", viewId}}, "")
return
}

func (ctrl *Controller) DeleteView(c context.Context, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.View), [][3]string{{"id", "=", viewId}})
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.View), [][3]string{{"id", "=", viewId}}, "")
return
}

func (ctrl *Controller) UpdateViewExclude(c context.Context, view models.View, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewExcludeKey, &view, [][3]string{{"id", "=", viewId}})
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewExcludeKey, &view, [][3]string{{"id", "=", viewId}}, "")
return
}

func (ctrl *Controller) ListViewAlerts(c context.Context, viewId string) (alerts []models.Alert, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &alerts, [][3]string{{"view_id", "=", viewId}})
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &alerts, [][3]string{{"view_id", "=", viewId}}, "")
return
}
Loading
Loading