-
Notifications
You must be signed in to change notification settings - Fork 4
/
aware.go
84 lines (74 loc) · 3 KB
/
aware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package models
import (
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// AwareParameter struct
type AwareParameter struct {
ID uuid.UUID `json:"id"`
Key string `json:"key"`
ParameterID uuid.UUID `json:"parameter_id" db:"parameter_id"`
UnitID uuid.UUID `json:"unit_id" db:"unit_id"`
}
// AwarePlatformParameterConfig holds information about which parameters are "enabled" for given instrument(s)
// { projectID: <uuid4>, instrument_id: <uuid4>, aware_id: <uuid4>, aware_parameters: { <string>: <uuid4> } }
// aware_parameters is a map of <aware_parameter_key> : <timeseries_id>
type AwarePlatformParameterConfig struct {
ProjectID uuid.UUID `json:"project_id" db:"project_id"`
InstrumentID uuid.UUID `json:"instrument_id" db:"instrument_id"`
AwareID uuid.UUID `json:"aware_id" db:"aware_id"`
AwareParameters map[string]*uuid.UUID `json:"aware_parameters"`
}
type awarePlatformParameterEnabled struct {
ProjectID uuid.UUID `json:"project_id" db:"project_id"`
InstrumentID uuid.UUID `json:"instrument_id" db:"instrument_id"`
AwareID uuid.UUID `json:"aware_id" db:"aware_id"`
AwareParameterKey string `json:"aware_parameter_key" db:"aware_parameter_key"`
TimeseriesID *uuid.UUID `json:"timeseries_id" db:"timeseries_id"`
}
// ListAwareParameters returns aware parameters
func ListAwareParameters(db *sqlx.DB) ([]AwareParameter, error) {
pp := make([]AwareParameter, 0)
if err := db.Select(
&pp, "SELECT id, key, parameter_id, unit_id FROM aware_parameter",
); err != nil {
return make([]AwareParameter, 0), err
}
return pp, nil
}
func listAwarePlatformParameterEnabled(db *sqlx.DB) ([]awarePlatformParameterEnabled, error) {
sql := `SELECT project_id, instrument_id, aware_id, aware_parameter_key, timeseries_id
FROM v_aware_platform_parameter_enabled
ORDER BY project_id, aware_id, aware_parameter_key`
aa := make([]awarePlatformParameterEnabled, 0)
if err := db.Select(&aa, sql); err != nil {
return make([]awarePlatformParameterEnabled, 0), err
}
return aa, nil
}
// ListAwarePlatformParameterConfig returns aware platform parameter configs
func ListAwarePlatformParameterConfig(db *sqlx.DB) ([]AwarePlatformParameterConfig, error) {
ee, err := listAwarePlatformParameterEnabled(db)
if err != nil {
return make([]AwarePlatformParameterConfig, 0), err
}
// reorganize aware_parameter_key, timeseries_id into map for each instrument
// Map of aware parameters to timeseries
m1 := make(map[uuid.UUID]AwarePlatformParameterConfig)
for _, e := range ee {
if _, ok := m1[e.InstrumentID]; !ok {
m1[e.InstrumentID] = AwarePlatformParameterConfig{
ProjectID: e.ProjectID,
InstrumentID: e.InstrumentID,
AwareID: e.AwareID,
AwareParameters: make(map[string]*uuid.UUID),
}
}
m1[e.InstrumentID].AwareParameters[e.AwareParameterKey] = e.TimeseriesID
}
cc := make([]AwarePlatformParameterConfig, 0)
for k := range m1 {
cc = append(cc, m1[k])
}
return cc, nil
}