// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"net/http"
	_ "net/http/pprof" // For pprof
	"strconv"
	"strings"

	"github.com/ngaut/log"
	"github.com/pingcap/tidb-enterprise-tools/pkg/utils"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
	binlogEvents = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "syncer",
			Name:      "binlog_event",
			Help:      "cost of binlog event transform",
			Buckets:   prometheus.ExponentialBuckets(0.00005, 2, 19),
		}, []string{"type"})

	binlogSkippedEventsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "syncer",
			Name:      "binlog_skipped_events_total",
			Help:      "total number of skipped binlog events",
		}, []string{"type"})

	finishedJobsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "syncer",
			Name:      "finished_jobs_total",
			Help:      "total number of finished jobs",
		}, []string{"type", "queueNo"})

	addedJobsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "syncer",
			Name:      "add_jobs_total",
			Help:      "total number of added jobs",
		}, []string{"type", "queueNo"})

	sqlRetriesTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "syncer",
			Name:      "sql_retries_total",
			Help:      "total number of sql retryies",
		}, []string{"type"})

	binlogPos = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "syncer",
			Name:      "binlog_pos",
			Help:      "current binlog pos",
		}, []string{"node"})

	binlogFile = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "syncer",
			Name:      "binlog_file",
			Help:      "current binlog file index",
		}, []string{"node"})

	binlogGTID = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "syncer",
			Name:      "gtid",
			Help:      "current transaction id",
		}, []string{"node"})

	txnCost = prometheus.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: "syncer",
			Name:      "txn_cost_in_second",
			Help:      "transaction cost in second",
			Buckets:   prometheus.ExponentialBuckets(0.00005, 2, 20),
		})
)

func initStatusAndMetrics(addr string) {
	prometheus.MustRegister(binlogEvents)
	prometheus.MustRegister(binlogSkippedEventsTotal)
	prometheus.MustRegister(addedJobsTotal)
	prometheus.MustRegister(finishedJobsTotal)
	prometheus.MustRegister(sqlRetriesTotal)
	prometheus.MustRegister(binlogPos)
	prometheus.MustRegister(binlogFile)
	prometheus.MustRegister(binlogGTID)
	prometheus.MustRegister(txnCost)

	go func() {
		http.HandleFunc("/status", func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("Content-Type", "text/plain")
			text := utils.GetRawInfo()
			w.Write([]byte(text))
		})

		// HTTP path for prometheus.
		http.Handle("/metrics", promhttp.Handler())
		log.Infof("listening on %v for status and metrics report.", addr)
		err := http.ListenAndServe(addr, nil)
		if err != nil {
			log.Fatal(err)
		}
	}()
}

func getBinlogIndex(filename string) float64 {
	spt := strings.Split(filename, ".")
	if len(spt) == 1 {
		return 0
	}
	idxStr := spt[len(spt)-1]

	idx, err := strconv.ParseFloat(idxStr, 64)
	if err != nil {
		log.Warnf("[syncer] parse binlog index %s, error %s", filename, err)
		return 0
	}
	return idx
}
