diff --git a/app/internal/model/db.json b/app/internal/model/db.json index d540094..31c3238 100644 --- a/app/internal/model/db.json +++ b/app/internal/model/db.json @@ -4,136 +4,140 @@ "fields": [ { "name": "c_gem_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "c_gem_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "c_gem_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "ep_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "ep_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "ep_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_gem_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_gem_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_gem_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_max_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_max_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "i_max_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, + { + "name": "meter_id", + "data_type": "integer", + "size": 4 + }, { "name": "p_max_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "p_max_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "p_max_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "s_max_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "s_max_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "s_max_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "time", - "type": "timestamp with time zone", - "null": false, + "data_type": "timestamp with time zone", "size": 8 }, { "name": "u_gem_1", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "u_gem_2", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 }, { "name": "u_gem_3", - "type": "double precision", - "null": false, + "data_type": "double precision", "size": 8 } ] + }, + { + "name": "public.meter", + "alias": "me", + "fields": [ + { + "name": "fortop_uid", + "data_type": "character varying", + "size": 50 + }, + { + "name": "id", + "data_type": "integer", + "size": 4 + }, + { + "name": "name", + "data_type": "character varying", + "size": 255 + } + ] } ] diff --git a/app/stats.go b/app/stats.go index de4d8df..85a99b7 100644 --- a/app/stats.go +++ b/app/stats.go @@ -49,8 +49,39 @@ func GetAttributes() (list []Attribute) { return } +// Meter represents a single meter +type Meter struct { + ID int `json:"id"` + Name string `json:"name"` + FortopUID string `json:"fortop_uid"` +} + +// GetMeters returns a list of all known meters +func GetMeters() (list []Meter) { + me := model.Meter() + + q := me.Select(me.ID, me.Name, me.FortopUID) + + rows, err := db.Query(q) + if err != nil { + panic(err) + } + + for rows.Next() { + var meter Meter + err = rows.Scan(&meter.ID, &meter.Name, &meter.FortopUID) + if err != nil { + panic(err) + } + + list = append(list, meter) + } + + return +} + // GetAttribute returns the specified attribute for all phases since the specified date -func GetAttribute(index int, date time.Time) (data []Phases) { +func GetAttribute(index, meter int, date time.Time) (data []Phases) { m := model.Measurement() r := reflect.ValueOf(m).Elem() @@ -61,7 +92,10 @@ func GetAttribute(index int, date time.Time) (data []Phases) { q := m.Select(m.Time, f1, f2, f3). OrderBy(qb.Desc(m.Time)). - Where(qc.Gte(m.Time, date)) + Where( + qc.Gte(m.Time, date), + qc.Eq(m.MeterID, meter), + ) rows, err := db.Query(q) if err != nil { diff --git a/server/main.go b/server/main.go index 3bfab4a..54bf3a9 100644 --- a/server/main.go +++ b/server/main.go @@ -1,6 +1,7 @@ package main import ( + "strconv" "strings" "time" @@ -31,9 +32,10 @@ func main() { pq := e.Group(`/pq`) pq.GET(`/count`, count) pq.GET(`/list`, listAttr) + pq.GET(`/meters`, listMeters) for _, v := range app.GetAttributes() { - pq.GET(`/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index)) + pq.GET(`/:meter/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index)) attrs = append(attrs, strings.ToLower(v.Name)) } @@ -51,13 +53,22 @@ func listAttr(c echo.Context) error { return c.JSON(200, attrs) } +func listMeters(c echo.Context) error { + return c.JSON(200, app.GetMeters()) +} + func getAttr(index int) echo.HandlerFunc { return func(c echo.Context) error { + meter, err := strconv.Atoi(c.Param(`meter`)) + if err != nil { + return c.NoContent(400) + } + date, err := time.Parse(time.RFC3339, c.Param(`date`)) if err != nil { return c.NoContent(400) } - return c.JSON(200, app.GetAttribute(index, date)) + return c.JSON(200, app.GetAttribute(index, meter, date)) } }