diff --git a/app/stats.go b/app/stats.go index 1ad47f7..de4d8df 100644 --- a/app/stats.go +++ b/app/stats.go @@ -28,17 +28,36 @@ func Count() int { return *count } -// GetAttribute returns the specified attribute for all phases since the specified date -func GetAttribute(attr string, date time.Time) (data []Phases) { - m := model.Measurement() +// Attribute represents a single attribute +type Attribute struct { + Name string + Index int +} - f := reflect.ValueOf(m).Elem().FieldByName(attr + `1`) - if !f.IsValid() || f.Type().Name() != `Field` { - return nil +// GetAttributes returns a list of all known attributes +func GetAttributes() (list []Attribute) { + m := model.Measurement() + r := reflect.TypeOf(m).Elem() + + for i := 0; i < r.NumField(); i++ { + f := r.Field(i) + if f.Name[len(f.Name)-1] == '1' { + list = append(list, Attribute{f.Name[:len(f.Name)-1], i}) + } } - f1 := f.Interface().(*qb.TableField) - f2 := reflect.ValueOf(m).Elem().FieldByName(attr + `2`).Interface().(*qb.TableField) - f3 := reflect.ValueOf(m).Elem().FieldByName(attr + `3`).Interface().(*qb.TableField) + + return +} + +// GetAttribute returns the specified attribute for all phases since the specified date +func GetAttribute(index int, date time.Time) (data []Phases) { + m := model.Measurement() + r := reflect.ValueOf(m).Elem() + + f1, f2, f3 := + r.Field(index).Interface().(*qb.TableField), + r.Field(index+1).Interface().(*qb.TableField), + r.Field(index+2).Interface().(*qb.TableField) q := m.Select(m.Time, f1, f2, f3). OrderBy(qb.Desc(m.Time)). diff --git a/server/main.go b/server/main.go index 3e90d57..d766d00 100644 --- a/server/main.go +++ b/server/main.go @@ -29,7 +29,10 @@ func main() { e.Use(recoverMiddleware) e.GET(`/count`, count) - e.GET(`/:attr/:date`, pmax) + + for _, v := range app.GetAttributes() { + e.GET(`/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index)) + } panic(e.Start(`localhost:33333`)) } @@ -38,31 +41,13 @@ func count(c echo.Context) error { return c.JSON(200, app.Count()) } -func pmax(c echo.Context) error { - attr := c.Param(`attr`) - if len(attr) < 2 { - return c.NoContent(400) - } - attr = convertAttribute(attr) +func getAttr(index int) echo.HandlerFunc { + return func(c echo.Context) error { + date, err := time.Parse(time.RFC3339, c.Param(`date`)) + 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)) } - - data := app.GetAttribute(attr, date) - if data == nil { - return c.NoContent(404) - } - - return c.JSON(200, data) -} - -func convertAttribute(attr string) string { - attr = strings.ToUpper(attr[:1]) + strings.ToLower(attr[1:]) - - attr = strings.Replace(attr, `gem`, `Gem`, -1) - attr = strings.Replace(attr, `max`, `Max`, -1) - - return attr } diff --git a/server/main_test.go b/server/main_test.go deleted file mode 100644 index 507655a..0000000 --- a/server/main_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestConvertAttribute(t *testing.T) { - var data = map[string]string{ - `cgem`: `CGem`, - `ep`: `Ep`, - `PMAX`: `PMax`, - `Test`: `Test`, - } - - for in, expected := range data { - assert.Equal(t, expected, convertAttribute(in)) - } -}