Пример #1
0
func quakeInVolcanoRegionStatsProto(r *http.Request, h http.Header, b *bytes.Buffer) *weft.Result {

	if res := weft.CheckQuery(r, []string{}, []string{}); !res.Ok {
		return res
	}
	var q haz.QuakeStats

	var rows *sql.Rows
	var err error
	var volcanoId string

	if volcanoId, err = getVolcanoRegionStats(r); err != nil {
		return weft.BadRequest(err.Error())
	}

	if rows, err = db.Query(fmt.Sprintf(quakesPerDayInVolcanoRegionSQL, 90), volcanoId); err != nil {
		return weft.ServiceUnavailableError(err)
	}
	defer rows.Close()

	for rows.Next() {
		var t time.Time
		var r haz.Rate

		if err = rows.Scan(&t, &r.Count); err != nil {
			return weft.ServiceUnavailableError(err)
		}

		r.Time = &haz.Timestamp{Sec: t.Unix(), Nsec: int64(t.Nanosecond())}

		q.PerDay = append(q.PerDay, &r)
	}
	rows.Close()

	q.Year = make(map[int32]int32)

	if rows, err = db.Query(fmt.Sprintf(sumMagsInVolcanoRegionSQL, 365), volcanoId); err != nil {
		return weft.ServiceUnavailableError(err)
	}

	defer rows.Close()

	for rows.Next() {
		var k, v int32
		if err = rows.Scan(&k, &v); err != nil {
			return weft.ServiceUnavailableError(err)
		}
		q.Year[k] = v
	}
	rows.Close()

	q.Month = make(map[int32]int32)

	if rows, err = db.Query(fmt.Sprintf(sumMagsInVolcanoRegionSQL, 28), volcanoId); err != nil {
		return weft.ServiceUnavailableError(err)
	}
	defer rows.Close()

	for rows.Next() {
		var k, v int32
		if err = rows.Scan(&k, &v); err != nil {
			return weft.ServiceUnavailableError(err)
		}
		q.Month[k] = v
	}
	rows.Close()

	q.Week = make(map[int32]int32)

	if rows, err = db.Query(fmt.Sprintf(sumMagsInVolcanoRegionSQL, 7), volcanoId); err != nil {
		return weft.ServiceUnavailableError(err)
	}
	defer rows.Close()

	for rows.Next() {
		var k, v int32
		if err = rows.Scan(&k, &v); err != nil {
			return weft.ServiceUnavailableError(err)
		}
		q.Week[k] = v
	}
	rows.Close()

	var by []byte

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

	b.Write(by)

	h.Set("Content-Type", protobuf)
	h.Set("Surrogate-Control", maxAge300)

	return &weft.StatusOK
}