Beispiel #1
0
// protobuf of latency summary info.
func TestDataCompletenessSummary(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/completeness/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.DataCompletenessSummaryResult

	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 != "completeness.gnss.1hz" {
		t.Errorf("expected gnss.1hz got %s", d.TypeID)
	}

	if d.Seconds == 0 {
		t.Error("unexpected zero seconds")
	}

	r.URL = "/data/completeness/summary?typeID=completeness.gnss.1hz"

	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.Errorf("expected 1 result got %d results", len(f.Result))
	}
}
func dataCompletenessSummaryProto(r *http.Request, h http.Header, b *bytes.Buffer) *weft.Result {
	typeID := r.URL.Query().Get("typeID")

	var err error
	var rows *sql.Rows
	var expected int

	switch typeID {
	case "":
		rows, err = dbR.Query(`SELECT siteID, typeID, count, expected
		FROM data.completeness_summary
		JOIN data.site USING (sitePK)
		JOIN data.completeness_type USING (typePK)`)
	default:
		var typePK int
		if err = dbR.QueryRow(`SELECT typePK FROM data.completeness_type WHERE typeID = $1`,
			typeID).Scan(&typePK); err != nil {
			if err == sql.ErrNoRows {
				return &weft.NotFound
			}
			return weft.InternalServerError(err)
		}

		rows, err = dbR.Query(`SELECT siteID, typeID, count, expected
		FROM data.completeness_summary
		JOIN data.site USING (sitePK)
		JOIN data.completeness_type USING (typePK)
		WHERE typeID = $1;`, typeID)
	}

	if err != nil {
		return weft.InternalServerError(err)
	}

	defer rows.Close()

	var t time.Time
	var dcr mtrpb.DataCompletenessSummaryResult

	for rows.Next() {
		var count int
		var siteID string

		if err = rows.Scan(&siteID, &typeID, &count, &expected); err != nil {
			return weft.InternalServerError(err)
		}

		c := float32(count) / (float32(expected) / 288)
		dc := mtrpb.DataCompletenessSummary{TypeID: typeID, SiteID: siteID, Completeness: c, Seconds: t.Unix()}
		dcr.Result = append(dcr.Result, &dc)
	}

	var by []byte

	if by, err = proto.Marshal(&dcr); err != nil {
		return weft.InternalServerError(err)
	}

	b.Write(by)

	return &weft.StatusOK
}