Improve attribute name handling

This commit is contained in:
Nise Void 2018-05-28 14:17:59 +02:00
parent 9374108288
commit df44cd0038
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3
3 changed files with 39 additions and 55 deletions

View File

@ -28,17 +28,36 @@ func Count() int {
return *count return *count
} }
// GetAttribute returns the specified attribute for all phases since the specified date // Attribute represents a single attribute
func GetAttribute(attr string, date time.Time) (data []Phases) { type Attribute struct {
m := model.Measurement() Name string
Index int
}
f := reflect.ValueOf(m).Elem().FieldByName(attr + `1`) // GetAttributes returns a list of all known attributes
if !f.IsValid() || f.Type().Name() != `Field` { func GetAttributes() (list []Attribute) {
return nil 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) return
f3 := reflect.ValueOf(m).Elem().FieldByName(attr + `3`).Interface().(*qb.TableField) }
// 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). q := m.Select(m.Time, f1, f2, f3).
OrderBy(qb.Desc(m.Time)). OrderBy(qb.Desc(m.Time)).

View File

@ -29,7 +29,10 @@ func main() {
e.Use(recoverMiddleware) e.Use(recoverMiddleware)
e.GET(`/count`, count) 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`)) panic(e.Start(`localhost:33333`))
} }
@ -38,31 +41,13 @@ func count(c echo.Context) error {
return c.JSON(200, app.Count()) return c.JSON(200, app.Count())
} }
func pmax(c echo.Context) error { func getAttr(index int) echo.HandlerFunc {
attr := c.Param(`attr`) return func(c echo.Context) error {
if len(attr) < 2 { date, err := time.Parse(time.RFC3339, c.Param(`date`))
return c.NoContent(400) if err != nil {
} return c.NoContent(400)
attr = convertAttribute(attr) }
date, err := time.Parse(time.RFC3339, c.Param(`date`)) return c.JSON(200, app.GetAttribute(index, date))
if err != nil {
return c.NoContent(400)
} }
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
} }

View File

@ -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))
}
}