/**
 * PromQL工具类
 * @date 2021-09-13 13:37:53
 * @author 吴锋霖
 */
class PromQLUtils {

    /**
     * 构建当前指标PromQL 查询语句
     * @param namespace 命名空间
     * @param podId K8S POD ID
     * @param metric 查询指标
     */
    static buildInstantPromQL(namespace: string, podId: string, metric: string) {
        switch (metric) {
            case 'MEM_REQUEST':
                return `sum (kube_pod_container_resource_requests_memory_bytes{pod="${podId}", namespace="${namespace}"})`
            case 'MEM_LIMIT':
                return `sum (kube_pod_container_resource_limits_memory_bytes{pod="${podId}", namespace="${namespace}"})`
            case 'MEM_USED':
                // 正常应该是sum，但在serverless集群上pod内存会被统计多次
                return `max (container_memory_working_set_bytes{image!="",pod="${podId}", namespace="${namespace}"})`
            case 'MEM_UTILIZATION':
                // 正常应该是sum，但在serverless集群上pod内存会被统计多次
                return `max (container_memory_working_set_bytes{image!="",pod="${podId}",namespace="${namespace}"}) /  (max (container_spec_memory_limit_bytes{image!="",pod="${podId}",namespace="${namespace}"})) * 100`
            case 'CPU_REQUEST':
                return `sum (kube_pod_container_resource_requests_cpu_cores{pod="${podId}", namespace="${namespace}"})`
            case 'CPU_LIMIT':
                return `sum (kube_pod_container_resource_limits_cpu_cores{pod="${podId}", namespace="${namespace}"})`
            case 'CPU_QUOTA': //cpu 配额
                return `container_spec_cpu_quota{image!="",pod_name="${podId}",namespace="${namespace}"})`
            case 'CPU_USED':
                return `sum (rate (container_cpu_usage_seconds_total{image!="",pod="${podId}",namespace="${namespace}"}[1m]))`
            case 'CPU_UTILIZATION':
                return `sum(rate(container_cpu_usage_seconds_total{image!="",pod_name="${podId}",namespace="${namespace}"}[1m])) by (pod_name, namespace) / (sum(container_spec_cpu_quota{image!="",pod_name="${podId}",namespace="${namespace}"}/100000) by (pod_name, namespace)) * 100`

        }
    }

    /**
      * 构建范围值指标PromQL 查询语句
      * @param namespace 命名空间
      * @param podId K8S POD ID
      * @param metric 查询指标
      */
    static buildRangePromQL(namespace: string, podId: string, metric: string) {
        switch (metric) {
            case 'MEM_USAGE':
                return `max (container_memory_working_set_bytes{image!="",pod_name="${podId}",namespace="${namespace}"}) by (pod_name)` // 正常应该是sum，但在serverless集群上pod内存会被统计多次
            case 'CPU_USAGE':
                return `sum (rate (container_cpu_usage_seconds_total{image!="",pod_name="${podId}",namespace="${namespace}"}[1m])) by (pod_name)`
            case 'NETWORK_RECEIVE': //接收
                return `sum (rate (container_network_receive_bytes_total{image!="",pod_name="${podId}",namespace="${namespace}"}[1m]))`
            case 'NETWORK_TRANSMIT': //发送
                return `- sum (rate (container_network_transmit_bytes_total{image!="",pod_name="${podId}",namespace="${namespace}"}[1m]))`
        }
    }

    static epochSecond(date?: Date): number {
        const seconds = (date || new Date()).getTime() / 1000 + ''
        return parseInt(seconds)
    }

}

export { PromQLUtils }