fmt plus name support on time series data

This commit is contained in:
Hayden 2022-12-05 12:32:26 -09:00
parent d2b8c96d76
commit 386885b115
No known key found for this signature in database
GPG key ID: 17CF79474E257545

View file

@ -58,6 +58,7 @@ type (
ValueOverTimeEntry struct { ValueOverTimeEntry struct {
Date time.Time `json:"date"` Date time.Time `json:"date"`
Value float64 `json:"value"` Value float64 `json:"value"`
Name string `json:"name"`
} }
ValueOverTime struct { ValueOverTime struct {
@ -136,11 +137,8 @@ func (r *GroupRepository) StatsLabelsByPurchasePrice(ctx context.Context, GID uu
Aggregate(func(sq *sql.Selector) string { Aggregate(func(sq *sql.Selector) string {
itemTable := sql.Table(item.Table) itemTable := sql.Table(item.Table)
// item to label is a many to many relation
// so we need to join the junction table
jt := sql.Table(label.ItemsTable) jt := sql.Table(label.ItemsTable)
// join the junction table to the item table
sq.Join(jt).On(sq.C(label.FieldID), jt.C(label.ItemsPrimaryKey[0])) sq.Join(jt).On(sq.C(label.FieldID), jt.C(label.ItemsPrimaryKey[0]))
sq.Join(itemTable).On(jt.C(label.ItemsPrimaryKey[1]), itemTable.C(item.FieldID)) sq.Join(itemTable).On(jt.C(label.ItemsPrimaryKey[1]), itemTable.C(item.FieldID))
@ -188,17 +186,25 @@ func (r *GroupRepository) StatsPurchasePrice(ctx context.Context, GID uuid.UUID,
stats.PriceAtEnd = orDefault(maybeEnd, 0) stats.PriceAtEnd = orDefault(maybeEnd, 0)
var v []struct { var v []struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
PurchasePrice float64 `json:"purchase_price"` PurchasePrice float64 `json:"purchase_price"`
} }
// Get Created Date and Price of all items between start and end // Get Created Date and Price of all items between start and end
err = r.db.Item.Query().Where( err = r.db.Item.Query().
item.HasGroupWith(group.ID(GID)), Where(
item.CreatedAtGTE(start), item.HasGroupWith(group.ID(GID)),
item.CreatedAtLTE(end), item.CreatedAtGTE(start),
item.Archived(false)). item.CreatedAtLTE(end),
Select(item.FieldCreatedAt, item.FieldPurchasePrice).Scan(ctx, &v) item.Archived(false),
).
Select(
item.FieldName,
item.FieldCreatedAt,
item.FieldPurchasePrice,
).
Scan(ctx, &v)
if err != nil { if err != nil {
return nil, err return nil, err