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

Enhancement/collection group ordering #183

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions api/handlers/collection_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,20 @@ func RemoveTimeseriesFromCollectionGroup(db *sqlx.DB) echo.HandlerFunc {
return c.JSON(http.StatusOK, make(map[string]interface{}))
}
}

// UpdateTimeseriesInCollectionGroup updates timeseries associative details in a certain collection group
func UpdateTimeseriesInCollectionGroup(db *sqlx.DB) echo.HandlerFunc {
return func(c echo.Context) error {
cgD := models.CollectionGroupDetails{}
if err := c.Bind(&cgD); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

cgDT, err := models.UpdateTimeseriesInCollectionGroup(db, &cgD)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

return c.JSON(http.StatusOK, cgDT)
}
}
2 changes: 2 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func main() {
private.POST("/projects/:project_id/collection_groups/:collection_group_id/timeseries/:timeseries_id", handlers.AddTimeseriesToCollectionGroup(db))
// // Collection Group; Remove Timeseries from collection_group
private.DELETE("/projects/:project_id/collection_groups/:collection_group_id/timeseries/:timeseries_id", handlers.RemoveTimeseriesFromCollectionGroup(db))
// // Collection Group; Update Timeseries in collection_group
private.PUT("/collection_groups/:collection_group_id/timeseries", handlers.UpdateTimeseriesInCollectionGroup(db))

// Plotting Configurations
public.GET("/projects/:project_id/plot_configurations", handlers.ListPlotConfigurations(db))
Expand Down
34 changes: 33 additions & 1 deletion api/models/collection_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type cgdTsItem struct {
ts.Timeseries
LatestTime *time.Time `json:"latest_time" db:"latest_time"`
LatestValue *float32 `json:"latest_value" db:"latest_value"`
ListOrder *int `json:"list_order" db:"list_order"`
}

var listCollectionGroupsSQL = `
Expand Down Expand Up @@ -66,7 +67,7 @@ func GetCollectionGroupDetails(db *sqlx.DB, projectID *uuid.UUID, collectionGrou
d.Timeseries = make([]cgdTsItem, 0)
if err := db.Select(
&d.Timeseries,
`SELECT t.*, tm.time as latest_time, tm.value as latest_value
`SELECT t.*, tm.time as latest_time, tm.value as latest_value, list_order
FROM collection_group_timeseries cgt
INNER JOIN collection_group cg on cg.id = cgt.collection_group_id
INNER JOIN v_timeseries t on t.id = cgt.timeseries_id
Expand Down Expand Up @@ -145,3 +146,34 @@ func RemoveTimeseriesFromCollectionGroup(db *sqlx.DB, collectionGroupID *uuid.UU
}
return nil
}

// UpdateTimeseriesInCollectionGroup updates timeseries associative details in a certain collection group
func UpdateTimeseriesInCollectionGroup(db *sqlx.DB, cgD *CollectionGroupDetails) (*CollectionGroupDetails, error) {
txn, err := db.Beginx()
if err != nil {
return nil, err
}
defer txn.Rollback()

stmt_timeseries, err := txn.Preparex(
`UPDATE collection_group_timeseries
SET list_order = $1
WHERE collection_group_id = $2 AND timeseries_id = $3;
`,
)
if err != nil {
return nil, err
}

for _, cgdTs := range cgD.Timeseries {
if _, err := stmt_timeseries.Exec(cgdTs.ListOrder, cgD.ID, cgdTs.ID); err != nil {
return nil, err
}
}

if err := txn.Commit(); err != nil {
return nil, err
}

return cgD, nil
}
2 changes: 2 additions & 0 deletions sql/common/V1.3.5__collection_group_timeseries_order.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE collection_group_timeseries
ADD list_order int NULL;
6 changes: 3 additions & 3 deletions sql/local/V999.1.0__seed_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ INSERT INTO collection_group (id, project_id, name, slug) VALUES
('30b32cb1-0936-42c4-95d1-63a7832a57db', '5b6f4f37-7755-4cf9-bd02-94f1e9bc5984', 'High Water Inspection', 'high-water-inspection');

-- collection_group_timeseries
INSERT INTO collection_group_timeseries (collection_group_id, timeseries_id) VALUES
('30b32cb1-0936-42c4-95d1-63a7832a57db', '7ee902a3-56d0-4acf-8956-67ac82c03a96'),
('30b32cb1-0936-42c4-95d1-63a7832a57db', '9a3864a8-8766-4bfa-bad1-0328b166f6a8');
INSERT INTO collection_group_timeseries (collection_group_id, timeseries_id, list_order) VALUES
('30b32cb1-0936-42c4-95d1-63a7832a57db', '7ee902a3-56d0-4acf-8956-67ac82c03a96', 1),
('30b32cb1-0936-42c4-95d1-63a7832a57db', '9a3864a8-8766-4bfa-bad1-0328b166f6a8', 0);

-- plot_configuration
INSERT INTO plot_configuration (project_id, id, slug, name) VALUES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5807,8 +5807,9 @@
" \"latest_time\": {\"type\": \"string\", \"format\": \"date-time\"},",
" \"latest_value\": {\"type\": \"number\"},",
" \"is_computed\": {\"type\": \"boolean\"},",
" \"list_order\": {\"type\": \"number\"},",
" },",
" \"required\": [\"id\", \"slug\", \"name\", \"variable\", \"project_id\", \"project\", \"project_slug\", \"instrument_id\", \"instrument\", \"instrument_slug\", \"parameter_id\", \"parameter\", \"unit_id\", \"unit\", \"latest_time\", \"latest_value\", \"is_computed\"],",
" \"required\": [\"id\", \"slug\", \"name\", \"variable\", \"project_id\", \"project\", \"project_slug\", \"instrument_id\", \"instrument\", \"instrument_slug\", \"parameter_id\", \"parameter\", \"unit_id\", \"unit\", \"latest_time\", \"latest_value\", \"is_computed\", \"list_order\"],",
" \"additionalProperties\": false",
" },",
" },",
Expand Down
Loading