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 (refactor):
It was really a question of responsibility.  Now the information is right where it is most relevant (next to where we identify which key in statsd we want to graph, ie the logical place to also mention what type that key is).

I needed to do some of this refactoring because the new chart types had slightly different requirements (no `sumSeries`, etc.)


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]
}