Exemplo n.º 1
0
func printEntries(writer io.Writer, entries []stats_entry.Entry) error {
	sort.Sort(stats_entry.EntryByTimestampDesc(entries))

	for i := 0; i < len(entries)-1; i++ {
		b := entries[i]
		a := entries[i+1]
		hourDiff := float64(a.Timestamp-b.Timestamp) / float64(time.Hour)
		valueDiff := a.Value - b.Value
		diff := float64(valueDiff) / hourDiff
		t := time.Unix(0, a.Timestamp)
		fmt.Fprintf(writer, "%s %s %s %s/h\n", timeToString(t), extendToLength(fmt.Sprintf("%d", a.Value), 12), extendToLength(fmt.Sprintf("%d", valueDiff), 12), extendToLength(fmt.Sprintf("%.2f", diff), 12))
	}
	return nil
}
func (h *handler) serveHTTP(responseWriter http.ResponseWriter, request *http.Request) error {
	var err error
	var limit int
	if limit, err = strconv.Atoi(request.FormValue("limit")); err != nil {
		limit = -1
	}
	var list []stats_entry.Entry
	if limit > 0 {
		if list, err = h.service.ListLimited(limit); err != nil {
			return err
		}
	} else {
		if list, err = h.service.List(); err != nil {
			return err
		}
	}

	sort.Sort(stats_entry.EntryByTimestampDesc(list))

	j := json_handler.NewJsonHandler(list)
	j.ServeHTTP(responseWriter, request)
	return nil
}