Improve attribute name handling
This commit is contained in:
parent
9374108288
commit
df44cd0038
39
app/stats.go
39
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()
|
||||
|
||||
f := reflect.ValueOf(m).Elem().FieldByName(attr + `1`)
|
||||
if !f.IsValid() || f.Type().Name() != `Field` {
|
||||
return nil
|
||||
// Attribute represents a single attribute
|
||||
type Attribute struct {
|
||||
Name string
|
||||
Index int
|
||||
}
|
||||
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)
|
||||
|
||||
// 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})
|
||||
}
|
||||
}
|
||||
|
||||
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)).
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
data := app.GetAttribute(attr, date)
|
||||
if data == nil {
|
||||
return c.NoContent(404)
|
||||
return c.JSON(200, app.GetAttribute(index, date))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user