package slowquery

import (
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus"
)

var (
	httpRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: "slowquery",
		Name:      "handle_requests_total",
		Help:      "Counter of HTTP requests of SlowQuery.",
	}, []string{"method", "path"})
	httpDurationSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: "slowquery",
		Name:      "handle_duration_seconds",
		Help:      "Summary of HTTP requests of SlowQuery.",
	}, []string{"method", "path", "status_code"})
)

func init() {
	prometheus.MustRegister(httpRequestsTotal)
	prometheus.MustRegister(httpDurationSeconds)
}

func recordHttpMetric(c *gin.Context, path string) func() {
	httpRequestsTotal.WithLabelValues(c.Request.Method, path).Inc()
	t := time.Now()
	return func() {
		dur := time.Since(t)
		httpDurationSeconds.WithLabelValues(c.Request.Method, path, strconv.Itoa(c.Writer.Status())).Observe(dur.Seconds())
	}
}
