package metrics

import (
	"fmt"
	"strings"
	"time"

	dtoMetrics "github.com/pingcap-inc/clinic/clinic_data_service/pkg/dto/metrics"
)

func getResourceQueryResult(webHost, varOrgId string, queryPoint []dtoMetrics.QueryPoints, clusterId string, startTime, endTime time.Time, rule string, tidbVersion string) []dtoMetrics.TableRowAggregation {
	var results []dtoMetrics.TableRowAggregation

	fromTimestamp := startTime.UnixMilli()
	toTimestamp := endTime.UnixMilli()

	for _, queryPointItem := range queryPoint {
		// Iterate through all DiffResult of each queryPoint
		for _, diffResult := range queryPointItem.DiffResult {
			result := dtoMetrics.TableRowAggregation{}

			// Get QueryName from queryPoint
			result.QueryName = queryPointItem.TypeName

			if diffResult.TimeAggregation == "" {
				result.TimeAggregation = "AVG"
			} else {
				result.TimeAggregation = diffResult.TimeAggregation
			}

			result.Threshold = fmt.Sprintf("%.1f%%", diffResult.Diff*100)
			result.Link = ""

			result.Status = "No Risk"

			queryName := queryPointItem.TypeName
			if rule == "Resource" {
				queryName = queryPointItem.TypeName + "_" + result.TimeAggregation
			}

			// Check if it's a latency type, if so multiply by 1000 first before formatting to avoid precision loss
			isLatencyType := isLatencyQuery(queryName)
			if isLatencyType {
				result.Max = fmt.Sprintf("%.2f", diffResult.Max*1000)
				result.Avg = fmt.Sprintf("%.2f", diffResult.Avg*1000)
			} else {
				result.Max = fmt.Sprintf("%.2f", diffResult.Max)
				result.Avg = fmt.Sprintf("%.2f", diffResult.Avg)
			}

			// 设置 PromSQL
		result.PromSQL = queryPointItem.PromSQL

		processQueryName(webHost, queryName, varOrgId, &result, diffResult.Diff, clusterId, fromTimestamp, toTimestamp, tidbVersion)

			results = append(results, result)
		}
	}

	return results
}

// isLatencyQuery checks if it's a latency type query
func isLatencyQuery(queryName string) bool {
	queryNameLower := strings.ToLower(queryName)
	return strings.Contains(queryNameLower, "latency") || strings.Contains(queryNameLower, "duration")
}

func processQueryName(webHost string, queryName string, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	switch queryName {
	case "P99TiKVLatency_coprocessor":
		getP99TiKVLatencyCoprocessorState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99TiKVLatency_kv_prewrite":
		getP99TiKVLatencyKVPrewriteState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99RegionNum":
		getP99RegionNumState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99TnxDuration_optimistic":
		getP99TnxDurationOptimisticState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99TnxDuration_pessimistic":
		getP99TnxDurationPessimisticState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99WriteTiKV":
		getP99WriteTiKVState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "DiskUsage":
		getDiskUsageState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "ReadWaitLatency":
		getReadWaitLatencyState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "WriteWaitLatency":
		getWriteWaitLatencyState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "CPS":
		getCPSState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P999QueryLatency":
		getP999QueryLatencyState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "P99QueryLatency":
		getP99QueryLatencyState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "QPS":
		getQPSState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	// Resource
	case "TiKV_CPU_P80_P90":
		getTiKVCPUP80P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_CPU_P80_AVG":
		getTiKVCPUP80AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_CPU_P90_P90":
		getTiKVCPUP90P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_CPU_P90_AVG":
		getTiKVCPUP90AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_CPU_P90":
		getTiKVCPUP90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_CPU_AVG":
		getTiKVCPUAVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	// TiDB CPU cases
	case "TiDB_CPU_P90_P90":
		getTiDBCPUP90P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_CPU_P90_AVG":
		getTiDBCPUP90AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_CPU_P80_P90":
		getTiDBCPUP80P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_CPU_P80_AVG":
		getTiDBCPUP80AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_CPU_P90":
		getTiDBCPUP90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_CPU_AVG":
		getTiDBCPUAVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	// TiKV MEM cases
	case "TiKV_MEM_P90_P90":
		getTiKVMEMP90P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_MEM_P90_AVG":
		getTiKVMEMP90AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_MEM_P80_P90":
		getTiKVMEMP80P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_MEM_P80_AVG":
		getTiKVMEMP80AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_MEM_P90":
		getTiKVMEMP90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiKV_MEM_AVG":
		getTiKVMEMAVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	// TiDB MEM cases
	case "TiDB_MEM_P90_P90":
		getTiDBMEMP90P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_MEM_P90_AVG":
		getTiDBMEMP90AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_MEM_P80_P90":
		getTiDBMEMP80P90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_MEM_P80_AVG":
		getTiDBMEMP80AVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_MEM_P90":
		getTiDBMEMP90State(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	case "TiDB_MEM_AVG":
		getTiDBMEMAVGState(webHost, varOrgId, targetRow, threshold, clusterId, fromTimestampByDay, toTimestamp, tidbVersion)
	default:
		// If no matching case, use default handling
		getDefaultState(targetRow, threshold)
	}
}

func getP99TiKVLatencyCoprocessorState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.25 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99TiKVLatency_coprocessor", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99TiKVLatencyKVPrewriteState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.25 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99TiKVLatency_kv_prewrite", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getWriteWaitLatencyState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.15 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "WriteWaitLatency", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getCPSState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "CPS", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP999QueryLatencyState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P999QueryLatency", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99QueryLatencyState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99QueryLatency", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getQPSState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "QPS", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99RegionNumState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.2 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99RegionNum", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99TnxDurationOptimisticState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.3 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99TnxDuration_optimistic", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99TnxDurationPessimisticState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.3 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99TnxDuration_pessimistic", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getP99WriteTiKVState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.2 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "P99WriteTiKV", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getDiskUsageState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.1 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "DiskUsage", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getReadWaitLatencyState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 1.15 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "ReadWaitLatency", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

func getDefaultState(targetRow *dtoMetrics.TableRowAggregation, threshold float64) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	} else {
		targetRow.Status = "No Risk"
	}
}

// TiDB CPU methods
func getTiDBCPUP90P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_P90_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBCPUP90AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_P90_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBCPUP80P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_P80_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBCPUP80AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_P80_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBCPUP90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBCPUAVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_CPU_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

// TiKV CPU methods
func getTiKVCPUP80P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_P80_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVCPUP90P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_P90_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVCPUP90AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_P90_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVCPUP80AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_P80_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVCPUP90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVCPUAVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_CPU_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

// TiDB MEM methods
func getTiDBMEMP90P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_P90_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBMEMP90AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_P90_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBMEMP80P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_P80_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBMEMP80AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_P80_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBMEMP90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiDBMEMAVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiDB_MEM_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}

// TiKV MEM methods
func getTiKVMEMP90P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_P90_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVMEMP90AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_P90_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVMEMP80P90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_P80_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVMEMP80AVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_P80_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVMEMP90State(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_P90", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
func getTiKVMEMAVGState(webHost, varOrgId string, targetRow *dtoMetrics.TableRowAggregation, threshold float64, clusterId string, fromTimestampByDay, toTimestamp int64, tidbVersion string) {
	if threshold > 0.2 {
		targetRow.Status = "Need Attention"
	}
	targetRow.Link = BuildGrafanaLink(webHost, "TiKV_MEM_AVG", clusterId, varOrgId, tidbVersion, fromTimestampByDay, toTimestamp)
}
