package model

import "time"

type TikvLatency struct {
	Id                 int64   `gorm:"primaryKey;autoIncrement"`
	OrgId              string  `gorm:"type:varchar(255)"`
	ClusterId          string  `gorm:"type:varchar(255);not null;index:idx_clusterid"`
	RangeStartTime     int64   `gorm:"not null;index:idx_range_starttime"`
	RangeEndTime       int64   `gorm:"not null;index:idx_range_endtime"`
	LatencyStartTime   int64   `gorm:"not null;"`
	LatencyEndTime     int64   `gorm:"not null;"`
	Instance           string  `gorm:"type:varchar(200);not null"`
	InstanceMaxLatency float64 `gorm:"not null;"`
	InstanceMaxCpu     float64 `gorm:"not null;"`
	InstanceMaxCpuDiff float64 `gorm:"not null;"`


	MaxLatencyWindow       string    `gorm:"type:varchar(255);"`
	ClusterLatencyP95      float64   `gorm:"not null;"`
	ClusterLatencyP95Diff  float64   `gorm:"not null;"`
	ClusterLatencyP99      float64   `gorm:"not null;"`
	ClusterLatencyP99Diff  float64   `gorm:"not null;"`
	ClusterLatencyP999     float64   `gorm:"not null;"`
	ClusterLatencyP999Diff float64   `gorm:"not null;"`
	CreatedAt              time.Time `gorm:"<-:create"`
}

func (TikvLatency) TableName() string {
	return "tikv_latencies"
}

type TikvHighCPUWindow struct {
	Id              int64   `gorm:"primaryKey;autoIncrement"`
	OrgId           string  `gorm:"type:varchar(255)"`
	ClusterId       string  `gorm:"type:varchar(255);not null;index:idx_clusterid"`
	RangeStartTime  int64   `gorm:"not null;index:idx_range_starttime"`
	RangeEndTime    int64   `gorm:"not null;index:idx_range_endtime"`
	WindowStartTime int64   `gorm:"not null"`
	WindowEndTime   int64   `gorm:"not null"`
	Instance        string  `gorm:"type:varchar(200);not null"`
	MaxCpuUsage     float64 `gorm:"not null"`
	RuleTag         string  `gorm:"type:varchar(50)"` // abs_high / rel_outlier / abs_high,rel_outlier
	ClusterLatencyP95  float64   `gorm:"not null;"`
	ClusterLatencyP99  float64   `gorm:"not null;"`
	ClusterLatencyP999 float64   `gorm:"not null;"`
	CreatedAt          time.Time `gorm:"<-:create"`
}

func (TikvHighCPUWindow) TableName() string {
	return "tikv_high_cpu_windows"
}

type TikvTopDigest struct {
	Id            int64  `gorm:"primaryKey;autoIncrement"`
	TikvLatencyId int64  `gorm:"not null;index:idx_tikvlatencyId"`
	CheckWindowId int64  `gorm:"not null;index:idx_checkwindowid"`
	Digest        string `gorm:"type:varchar(255);not null;index:idx_digest"`
	// Sql            string `gorm:"column:sql_text;type:text"`
	// min
	CpuTime    float64 `gorm:"not null;"`
	CpuPercent float64 `gorm:"not null;"`
    CheckType  string  `gorm:"type:varchar(50);not null;"` // Slow-TiKV  / High-CPU-TiKV
	IsSlowQuery bool `gorm:"not null;"`

	SlowQueryCount        *int64
	SlowQueryMaxMem       *float64
	SlowQueryAvgMem       *float64
	SlowQueryMaxQueryTime *float64
	SlowQueryAvgQueryTime *float64
	SlowQueryDetailLink   string    `gorm:"type:varchar(4000);"`
	SlowQueryListLink     string    `gorm:"type:varchar(4000);"`
	TopSqlLink            string    `gorm:"type:varchar(4000);"`
	CreatedAt             time.Time `gorm:"<-:create"`
}

func (TikvTopDigest) TableName() string {
	return "tikv_top_digests"
}

