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):
Oh i missed the numSeries change. 

My concern is just in the duplication (i know i know not normally something that bothers me) and opening up for these to get out of sync easily in future changes. Since these are all data points nsqd sends across, could/should we just have a const mapping of keys to their metric type? I'm probably just bikeshedding here so take this implementation input however it's useful.


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