Exemple #1
0
// TODO: returns weft.NotFound when query result is empty?
func fieldLatestProto(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 deviceID, modelID, typeid, time, value, lower, upper, scale
		FROM field.metric_summary
		JOIN field.device using (devicePK)
		JOIN field.model using (modelPK)
		JOIN field.threshold using (devicePK, typePK)
		JOIN field.type using (typePK)`)
	default:
		rows, err = dbR.Query(`select deviceID, modelID, typeid, time, value, lower, upper, scale
		FROM field.metric_summary
		JOIN field.device using (devicePK)
		JOIN field.model using (modelPK)
		JOIN field.threshold using (devicePK, typePK)
		JOIN field.type using (typePK)
		WHERE typeID = $1;`, typeID)
	}
	if err != nil {
		return weft.InternalServerError(err)
	}

	defer rows.Close()

	var t time.Time
	var fmlr mtrpb.FieldMetricSummaryResult

	for rows.Next() {
		var fmr mtrpb.FieldMetricSummary

		if err = rows.Scan(&fmr.DeviceID, &fmr.ModelID, &fmr.TypeID, &t, &fmr.Value,
			&fmr.Lower, &fmr.Upper, &fmr.Scale); err != nil {
			return weft.InternalServerError(err)
		}

		fmr.Seconds = t.Unix()

		fmlr.Result = append(fmlr.Result, &fmr)
	}
	rows.Close()

	var by []byte

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

	b.Write(by)

	return &weft.StatusOK
}
Exemple #2
0
// protobuf of field metric summary info.
func TestFieldMetricsSummary(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: "/field/metric/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.FieldMetricSummaryResult

	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.DeviceID != "gps-taupoairport" {
		t.Errorf("expected gps-taupoairport got %s", d.DeviceID)
	}

	if d.ModelID != "Trimble NetR9" {
		t.Errorf("expected Trimble NetR9 got %s", d.ModelID)
	}

	if d.TypeID != "voltage" {
		t.Errorf("expected voltage got %s", d.TypeID)
	}

	if d.Value != 14100 {
		t.Errorf("expected 14100 got %d", d.Value)
	}

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

	if d.Upper != 45000 {
		t.Errorf("expected 45000 got %d", d.Upper)
	}

	if d.Lower != 12000 {
		t.Errorf("expected 12000 got %d", d.Lower)
	}

	if d.Scale != 0.001 {
		t.Errorf("expected 0.001 got %f", d.Scale)
	}

	// should be no errors and empty result for typeID=conn
	r.URL = "/field/metric/summary?typeID=conn"

	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) != 0 {
		t.Error("expected 0 results.")
	}
}