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

Add API to fetch charting datapoints for a chart inside a dashbaord #157

Merged
merged 15 commits into from
Feb 21, 2025
Merged
12 changes: 12 additions & 0 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ type DashboardChart struct {
bun.BaseModel `json:"-"`
}

type DashboardChartPosition struct {
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4(),pk" json:"id,omitempty"`
DashboardID uuid.UUID `json:"dashboard_id,omitempty"`
ChartID uuid.UUID `json:"chart_id,omitempty"`
OrderIndex int64 `json:"order_index,omitempty"`

bun.BaseModel `json:"-"`
}

type ListDashboardOptions struct {
Paginator Paginator
WorkspaceID uuid.UUID
Expand All @@ -68,4 +77,7 @@ type DashboardRepository interface {
AddChart(context.Context, *DashboardChart) error
List(context.Context, ListDashboardOptions) ([]Dashboard, int64, error)
GetCharts(context.Context, FetchDashboardChartsOption) ([]DashboardChart, error)

UpdateDashboardPositions(context.Context, uuid.UUID, []DashboardChartPosition) error
GetDashboardPositions(context.Context, uuid.UUID) ([]DashboardChartPosition, error)
}
8 changes: 5 additions & 3 deletions integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ type IntegrationDataValues struct {
// InternalName + ProviderID search in db
// We cannot use only InternalName becasue some integrations
// like mercury have the same InternalName twice ( each account has a savings and checkings which we track)
InternalName IntegrationChartInternalNameType
ProviderID string
Data IntegrationDataPoint
InternalName IntegrationChartInternalNameType
UserFacingName string
ProviderID string
Data IntegrationDataPoint
}

type IntegrationChartValues struct {
Expand Down Expand Up @@ -188,4 +189,5 @@ type IntegrationRepository interface {
AddDataPoint(context.Context, *WorkspaceIntegration, []IntegrationDataValues) error
ListCharts(context.Context, uuid.UUID) ([]IntegrationChart, error)
GetChart(context.Context, FetchChartOptions) (IntegrationChart, error)
GetDataPoints(context.Context, IntegrationChart) ([]IntegrationDataPoint, error)
}
60 changes: 60 additions & 0 deletions internal/datastore/postgres/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"time"

"github.com/ayinke-llc/malak"
"github.com/google/uuid"
"github.com/uptrace/bun"
)

Expand All @@ -26,6 +27,10 @@
ctx, cancelFn := withContext(ctx)
defer cancelFn()

if dashboard.Title == "" {
return errors.New("dashboard title is required")
}

return d.inner.RunInTx(ctx, &sql.TxOptions{},
func(ctx context.Context, tx bun.Tx) error {

Expand Down Expand Up @@ -128,3 +133,58 @@

return charts, err
}

func (d *dashboardRepo) GetDashboardPositions(ctx context.Context,
dashboardID uuid.UUID) ([]malak.DashboardChartPosition, error) {

ctx, cancelFn := withContext(ctx)
defer cancelFn()

positions := make([]malak.DashboardChartPosition, 0)

err := d.inner.NewSelect().
Model(&positions).
Where("dashboard_id = ?", dashboardID).
Scan(ctx)

return positions, err
}

func (d *dashboardRepo) UpdateDashboardPositions(ctx context.Context,
dashboardID uuid.UUID,
positions []malak.DashboardChartPosition) error {

ctx, cancelFn := withContext(ctx)
defer cancelFn()

return d.inner.RunInTx(ctx, &sql.TxOptions{},
func(ctx context.Context, tx bun.Tx) error {
// First check if dashboard exists
exists, err := tx.NewSelect().
Model((*malak.Dashboard)(nil)).
Where("id = ?", dashboardID).
Exists(ctx)
if err != nil {
return err
}

Check warning on line 169 in internal/datastore/postgres/dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/postgres/dashboard.go#L168-L169

Added lines #L168 - L169 were not covered by tests
if !exists {
return malak.ErrDashboardNotFound
}

_, err = tx.NewDelete().
Model(new(malak.DashboardChartPosition)).
Where("dashboard_id = ?", dashboardID).
Exec(ctx)
if err != nil {
return err
}

Check warning on line 180 in internal/datastore/postgres/dashboard.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/postgres/dashboard.go#L179-L180

Added lines #L179 - L180 were not covered by tests

if len(positions) > 0 {
_, err = tx.NewInsert().Model(&positions).
Exec(ctx)
return err
}

return nil
})
}
Loading
Loading