// TODO: returns weft.NotFound when query result is empty? func dataLatencySummaryProto(r *http.Request, h http.Header, b *bytes.Buffer) *weft.Result { typeID := r.URL.Query().Get("typeID") var err error var rows *sql.Rows switch typeID { case "": rows, err = dbR.Query(`SELECT siteID, typeID, time, mean, fifty, ninety, lower, upper, scale FROM data.latency_summary JOIN data.site USING (sitePK) JOIN data.latency_threshold USING (sitePK, typePK) JOIN data.type USING (typePK)`) default: rows, err = dbR.Query(`SELECT siteID, typeID, time, mean, fifty, ninety, lower, upper, scale FROM data.latency_summary JOIN data.site USING (sitePK) JOIN data.latency_threshold USING (sitePK, typePK) JOIN data.type USING (typePK) WHERE typeID = $1;`, typeID) } if err != nil { return weft.InternalServerError(err) } defer rows.Close() var t time.Time var dlsr mtrpb.DataLatencySummaryResult for rows.Next() { var dls mtrpb.DataLatencySummary if err = rows.Scan(&dls.SiteID, &dls.TypeID, &t, &dls.Mean, &dls.Fifty, &dls.Ninety, &dls.Lower, &dls.Upper, &dls.Scale); err != nil { return weft.InternalServerError(err) } dls.Seconds = t.Unix() dlsr.Result = append(dlsr.Result, &dls) } rows.Close() var by []byte if by, err = proto.Marshal(&dlsr); err != nil { return weft.InternalServerError(err) } b.Write(by) return &weft.StatusOK }
// protobuf of latency summary info. func TestDataLatencySummary(t *testing.T) { setup(t) defer teardown() // Load test data. if err := routes.DoAllStatusOk(testServer.URL); err != nil { t.Error(err) } r := wt.Request{ID: wt.L(), URL: "/data/latency/summary", Accept: "application/x-protobuf"} var b []byte var err error if b, err = r.Do(testServer.URL); err != nil { t.Error(err) } var f mtrpb.DataLatencySummaryResult if err = proto.Unmarshal(b, &f); err != nil { t.Error(err) } if len(f.Result) != 1 { t.Error("expected 1 result.") } d := f.Result[0] if d.SiteID != "TAUP" { t.Errorf("expected TAUP got %s", d.SiteID) } if d.TypeID != "latency.strong" { t.Errorf("expected latency.strong got %s", d.TypeID) } if d.Mean != 10000 { t.Errorf("expected 10000 got %d", d.Mean) } if d.Fifty != 0 { t.Errorf("expected 0 got %d", d.Fifty) } if d.Ninety != 0 { t.Errorf("expected 0 got %d", d.Ninety) } if d.Seconds == 0 { t.Error("unexpected zero seconds") } if d.Upper != 15000 { t.Errorf("expected 15000 got %d", d.Upper) } if d.Lower != 12000 { t.Errorf("expected 12000 got %d", d.Lower) } if d.Scale != 1.0 { t.Errorf("expected 1.0 got %f", d.Scale) } r.URL = "/data/latency/summary?typeID=latency.strong" if b, err = r.Do(testServer.URL); err != nil { t.Error(err) } f.Reset() if err = proto.Unmarshal(b, &f); err != nil { t.Error(err) } if len(f.Result) != 1 { t.Error("expected 1 result.") } }