Exemple #1
0
// 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
}
Exemple #2
0
func (a *tagSearch) dataLatency() <-chan *weft.Result {
	out := make(chan *weft.Result)
	go func() {
		defer close(out)
		var err error
		var rows *sql.Rows

		if rows, err = dbR.Query(`SELECT siteID, typeID, time, mean, fifty, ninety, lower, upper
	 			  FROM data.latency_tag
	 			  JOIN data.latency_summary USING (sitePK, typePK)
	 			  JOIN data.latency_threshold USING (sitePK, typePK)
	 			  JOIN data.site USING (sitePK)
				  JOIN data.type USING (typePK)
			          WHERE tagPK = (SELECT tagPK FROM mtr.tag WHERE tag = $1)
			          OR siteID = $2`, a.tag, a.tag); err != nil {
			out <- weft.InternalServerError(err)
			return
		}
		defer rows.Close()

		var tm time.Time

		for rows.Next() {

			var dls mtrpb.DataLatencySummary

			if err = rows.Scan(&dls.SiteID, &dls.TypeID, &tm, &dls.Mean, &dls.Fifty, &dls.Ninety,
				&dls.Lower, &dls.Upper); err != nil {
				out <- weft.InternalServerError(err)
				return
			}

			dls.Seconds = tm.Unix()
			a.tagResult.DataLatency = append(a.tagResult.DataLatency, &dls)
		}

		out <- &weft.StatusOK
		return
	}()
	return out
}