Skip to content

Commit

Permalink
return empty list instead of nil (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz authored May 14, 2022
1 parent 5b64b7e commit 0b9e334
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,22 +1671,34 @@ func (s *RestServer) deleteTypedUserItemFeedback(request *restful.Request, respo

// Measurement stores a statistical value.
type Measurement struct {
Name string `json:"-"`
Name string
Timestamp time.Time
Value float32
}

type innerMeasurement struct {
Timestamp time.Time `json:"timestamp"`
Value float32 `json:"value"`
}

func NewMeasurementFromScore(name string, score cache.Scored) (Measurement, error) {
m := Measurement{Name: name}
var m innerMeasurement
err := json.Unmarshal([]byte(score.Id), &m)
if err != nil {
base.Logger().Error("failed to decode measurement", zap.Error(err))
}
return m, nil
return Measurement{
Name: name,
Timestamp: m.Timestamp,
Value: m.Value,
}, nil
}

func (m Measurement) GetScore() cache.Scored {
buf, _ := json.Marshal(m)
buf, _ := json.Marshal(innerMeasurement{
Timestamp: m.Timestamp,
Value: m.Value,
})
return cache.Scored{Id: string(buf), Score: float64(m.Timestamp.Unix())}
}

Expand All @@ -1695,7 +1707,7 @@ func (s *RestServer) GetMeasurements(name string, n int) ([]Measurement, error)
if err != nil {
return nil, errors.Trace(err)
}
var measurements []Measurement
measurements := make([]Measurement, 0, len(scores))
for _, score := range scores {
measurement, err := NewMeasurementFromScore(name, score)
if err != nil {
Expand Down

0 comments on commit 0b9e334

Please sign in to comment.