Repository: nsqio/nsq
File: nsqadmin/graphs.go (Go)

Code before:
	params.Set("margin", "0")
	params.Set("colorList", color)
	params.Set("yMin", "0")
	interval := fmt.Sprintf("%dsec", *statsdInterval/time.Second)
	params.Set("target", fmt.Sprintf(`summarize(%s,"%s","avg")`, target, interval))
	params.Set("from", g.GraphInterval.GraphFrom)
	params.Set("until", g.GraphInterval.GraphUntil)
	return template.URL(fmt.Sprintf("%s/render?%s", g.GraphiteUrl, params.Encode()))
}

func (g *GraphOptions) LargeGraph(gr GraphTarget, key string, metricType string) template.URL {
	target, color := gr.Target(key)
	target = fmt.Sprintf(target, g.Prefix(metricType))
	params := url.Values{}
	params.Set("height", "450")
	params.Set("width", "800")
	params.Set("bgcolor", "ff000000") // transparent
	params.Set("fgcolor", "999999")
	params.Set("colorList", color)
	params.Set("yMin", "0")
	interval := fmt.Sprintf("%dsec", *statsdInterval/time.Second)
	target = fmt.Sprintf(`summarize(%s,"%s","avg")`, target, interval)
	if metricType == "counter" {
		scale := fmt.Sprintf("%.04f", 1/float64(*statsdInterval/time.Second))
		target = fmt.Sprintf(`scale(%s,%s)`, target, scale)
	}
	params.Set("target", target)
	params.Set("from", g.GraphInterval.GraphFrom)
	params.Set("until", g.GraphInterval.GraphUntil)
	return template.URL(fmt.Sprintf("%s/render?%s", g.GraphiteUrl, params.Encode()))
}

func (g *GraphOptions) Rate(gr GraphTarget) string {
	target, _ := gr.Target("message_count")
	return fmt.Sprintf(target, g.Prefix("counter"))
}

func rateQuery(target string) string {
	params := url.Values{}
	fromInterval := fmt.Sprintf("-%dsec", *statsdInterval*2/time.Second)
	params.Set("from", fromInterval)
	untilInterval := fmt.Sprintf("-%dsec", *statsdInterval/time.Second)
	params.Set("until", untilInterval)
	params.Set("format", "json")
	params.Set("target", target)
	return fmt.Sprintf("/render?%s", params.Encode())
}

func parseRateResponse(body []byte) ([]byte, error) {
	js, err := simplejson.NewJson([]byte(body))
	if err != nil {

Review comment (suggestion):
Is there a specific reason you didn't like this metric type logic consolidated here? We added this function after having found that we missed capturing the right type for some metrics in some display contexts.


Code after:
	params.Set("yMin", "0")
	interval := fmt.Sprintf("%dsec", *statsdInterval/time.Second)
	params.Set("target", fmt.Sprintf(`summarize(%s,"%s","avg")`, target, interval))
	params.Set("from", g.GraphInterval.GraphFrom)
	params.Set("until", g.GraphInterval.GraphUntil)
	return template.URL(fmt.Sprintf("%s/render?%s", g.GraphiteUrl, params.Encode()))
}

func (g *GraphOptions) LargeGraph(gr GraphTarget, key string) template.URL {
	target, color := gr.Target(key)
	target = fmt.Sprintf(target, g.Prefix(metricType(key)))
	params := url.Values{}
	params.Set("height", "450")
	params.Set("width", "800")
	params.Set("bgcolor", "ff000000") // transparent
	params.Set("fgcolor", "999999")
	params.Set("colorList", color)
	params.Set("yMin", "0")
	interval := fmt.Sprintf("%dsec", *statsdInterval/time.Second)
	target = fmt.Sprintf(`summarize(%s,"%s","avg")`, target, interval)
	if metricType(key) == "counter" {
		scale := fmt.Sprintf("%.04f", 1/float64(*statsdInterval/time.Second))
		target = fmt.Sprintf(`scale(%s,%s)`, target, scale)
	}
	params.Set("target", target)
	params.Set("from", g.GraphInterval.GraphFrom)
	params.Set("until", g.GraphInterval.GraphUntil)
	return template.URL(fmt.Sprintf("%s/render?%s", g.GraphiteUrl, params.Encode()))
}

func (g *GraphOptions) Rate(gr GraphTarget) string {
	target, _ := gr.Target("message_count")
	return fmt.Sprintf(target, g.Prefix(metricType("message_count")))
}

func metricType(key string) string {
	return map[string]string{
		"depth":           "gauge",
		"in_flight_count": "gauge",
		"deferred_count":  "gauge",
		"requeue_count":   "counter",
		"timeout_count":   "counter",
		"message_count":   "counter",
		"clients":         "gauge",
		"*_bytes":         "gauge",
		"gc_pause_*":      "gauge",
		"gc_runs":         "counter",
		"heap_objects":    "gauge",
	}[key]
}