package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidbcloud/metering/pkg/common/constants"
)

var (
	MetricsReportLag = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "metering",
			Subsystem: "metering_report",
			Name:      "lag_seconds",
			Help:      "lag of metering data reporting.",
		}, []string{constants.MetricsTaskName},
	)
	MetricsNetworkFailedCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "network_meter",
			Name:      "failed_total",
			Help:      "total failed of network metering.",
		}, []string{constants.MetricsMeterTs, constants.MetricsFailedType, constants.MetricsLblProviderType},
	)
	MetricsRquestAPICount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "common_meter",
			Name:      "request_api_total",
			Help:      "total api reqeusts for 3rd api.",
		}, []string{constants.MetricsAPIName, constants.MetricsAPIResult, constants.MetricsLblProviderType},
	)
	MetricsNetworkRerunCount = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "metering",
			Subsystem: "network_meter",
			Name:      "rerun_total",
			Help:      "total failed of network metering.",
		}, []string{constants.MetricsRerunDiffTs, constants.MetricsLblProviderType},
	)
	MetricsNetworkMeterDuration = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "metering",
			Subsystem: "network_meter",
			Name:      "duration_seconds",
			Help:      "duration of metering for target.",
		}, []string{constants.MetricsLblProviderType, constants.MetricsMeterTs, constants.MetricsNetworkMeterType},
	)
	MetricsInstanceFailedCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "instance_meter",
			Name:      "failed_total",
			Help:      "total failed of instance metering.",
		}, []string{constants.MetricsTaskName, constants.MetricsFailedType},
	)
	MetricsNextGenTaskCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "nextgen_task",
			Name:      "count_total",
			Help:      "total executions of nextgen tasks partitioned by result type.",
		}, []string{constants.MetricsTaskName, constants.MetricsLblProviderType, constants.MetricsTaskResultType},
	)
	MetricsNextGenTaskDuration = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "metering",
			Subsystem: "nextgen_task",
			Name:      "duration_seconds",
			Help:      "duration of nextgen task operations in seconds.",
		}, []string{constants.MetricsTaskName, constants.MetricsLblProviderType, constants.MetricsTaskOperation},
	)
	MetricsNextGenMeteringSkipCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "nextgen_metering",
			Name:      "skip_total",
			Help:      "total count of nextgen metering datapoints skipped because metadata was missing or SKU rules were not satisfied.",
		}, []string{constants.MetricsLblProviderType, constants.MetricsLblNextGenCategory, constants.MetricsLblNextGenReason},
	)
	MetricsNextGenMeteringMaxRCUFallbackCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "metering",
			Subsystem: "nextgen_metering",
			Name:      "max_rcu_fallback_total",
			Help:      "total count of nextgen metering RCU records falling back to legacy maxRcu.",
		}, []string{constants.MetricsLblProviderType, constants.MetricsLblNextGenCategory, constants.MetricsLblNextGenReason},
	)
	MetricsCheckpointTimestamp = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "metering",
			Subsystem: "checkpoint",
			Name:      "timestamp_seconds",
			Help:      "current checkpoint timestamp in seconds.",
		}, []string{constants.MetricsTaskName},
	)
)

func init() {
	prometheus.MustRegister(MetricsReportLag)
	prometheus.MustRegister(MetricsNetworkFailedCount)
	prometheus.MustRegister(MetricsRquestAPICount)
	prometheus.MustRegister(MetricsNetworkRerunCount)
	prometheus.MustRegister(MetricsNetworkMeterDuration)
	prometheus.MustRegister(MetricsInstanceFailedCount)
	prometheus.MustRegister(MetricsNextGenTaskCount)
	prometheus.MustRegister(MetricsNextGenTaskDuration)
	prometheus.MustRegister(MetricsNextGenMeteringSkipCount)
	prometheus.MustRegister(MetricsNextGenMeteringMaxRCUFallbackCount)
	prometheus.MustRegister(MetricsCheckpointTimestamp)
}
