package com.microservices.mon.k8s.utils;

import java.util.List;

/**
 * Prometheus查询常量
 */
public class PromQLConstant {
    // 集群可用节点数:sum(kube_node_info)
    public static String CLUSTER_NODE_COUNT(String clusterId) {
        return String.format("sum(kube_node_info{cluster=~\"%s\"})", clusterId);
    }

    // 集群不可用节点数:sum(kube_node_spec_unschedulable)
    public static String CLUSTER_UNAVAILABLE_NODE_COUNT(String clusterId) {
        return String.format("sum(kube_node_spec_unschedulable{cluster=~\"%s\"})", clusterId);
    }

    // 集群cpu使用率:(sum(kube_pod_container_resource_requests{resource="cpu"})/ sum(kube_node_status_allocatable{resource="cpu"}))*100
    public static String CLUSTER_CPU_USAGE(String clusterId) {
        return String.format("(sum(kube_pod_container_resource_requests{cluster=~\"%s\",resource=\"cpu\",node!=\"\"})/ sum(kube_node_status_allocatable{cluster=~\"%s\",resource=\"cpu\"}))*100", clusterId, clusterId);
    }

    // 集群POD使用率:(sum(kube_pod_info) / sum(kube_node_status_allocatable{resource="pods"}))*100
    public static String CLUSTER_POD_USAGE(String clusterId) {
        return String.format("(sum(kube_pod_info{cluster=~\"%s\"}) / sum(kube_node_status_allocatable{cluster=~\"%s\",resource=\"pods\"}))*100", clusterId, clusterId);
    }

    // 集群内存使用率:(sum(kube_pod_container_resource_requests{resource="memory"}) / sum(kube_node_status_allocatable{resource="memory"}))*100
    public static String CLUSTER_MEMORY_USAGE(String clusterId) {
        return String.format("(sum(kube_pod_container_resource_requests{cluster=~\"%s\",resource=\"memory\",node!=\"\"}) / sum(kube_node_status_allocatable{cluster=~\"%s\",resource=\"memory\"}))*100", clusterId, clusterId);
    }

    // 集群最近24小时请求总数:sum(increase(nginx_http_response_count_total[1d]))
    public static final String TOTAL_REQUEST_COUNT_TODAY = "round(sum(increase(nginx_http_response_count_total[1d])))";
    // 集群最近24小时平均处理时间:sum(rate(nginx_http_response_time_seconds_sum[1d]))*1000
    public static final String AVERAGE_REQUEST_PROCESSING_TIME_TODAY = "sum(rate(nginx_http_response_time_seconds_sum[1d]))*1000";
    // 集群最近24小时请求成功率:(sum(nginx_http_response_count_total{status=~"2.*|3.*"})/sum(nginx_http_response_count_total))*100
    public static final String REQUEST_SUCCESS_RATE_TODAY = "(sum(nginx_http_response_count_total{status=~\"2.*|3.*\"})/sum(nginx_http_response_count_total))*100";

    // 集群最近24小时微服务重启次数:sum(increase(kube_pod_container_status_restarts_total{namespace=~"base-service|data|forgeplus|microservices|portal"}[1d]))
    public static String MICROSERVICE_RESTART_COUNT_TODAY(String clusterId, List<String> namespaceNameList) {
        return String.format("sum(increase(kube_pod_container_status_restarts_total{cluster=~\"%s\",namespace=~\"%s\"}[1d]))", clusterId, String.join("|", namespaceNameList));
    }

    // POD运行状态:sum(kube_pod_status_phase{namespace=~"base-service|data|forgeplus|microservices|portal", phase!="Unknown"}) by (phase)
    public static String POD_STATUS(String clusterId, List<String> namespaceNameList) {
        return String.format("sum(kube_pod_status_phase{cluster=~\"%s\",namespace=~\"%s\"}) by (phase)", clusterId, String.join("|", namespaceNameList));
    }

    // 应用最近24小时请求数:sort_desc(round(sum(increase(nginx_http_response_count_total[1d])) by (server_port)))
    public static String APPLICATION_REQUEST_COUNT = "sort_desc(round(sum(increase(nginx_http_response_count_total[1d])) by (server_port)))";
    // 集群QPS:sum(rate(nginx_http_response_time_seconds_count[10m]))
    public static String CLUSTER_QPS = "sum(rate(nginx_http_response_time_seconds_count[10m]))";

    // 容器CPU使用率:sum without (dc,from,id) (irate(container_cpu_user_seconds_total{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name="xxxxxxx"}[5m]) * 100)
    public static String POD_CPU_USAGE(String podName) {
        return String.format("sum without (dc,from,id) (irate(container_cpu_user_seconds_total{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name=\"%s\"}[5m]) * 100)", podName);
    }

    // 所有容器CPU使用率:
    public static String ALL_POD_CPU_USAGE(List<String> namespaceNameList) {
        return String.format("sort_desc(" +
                "sum (" +
                "irate(" +
                "container_cpu_user_seconds_total{container_label_io_kubernetes_docker_type=\"container\",image!=\"\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}[5m]) * 100) " +
                "by (container_label_io_kubernetes_pod_name))", String.join("|", namespaceNameList));
    }

    // 容器内存使用情况:sum without (dc,from,id) (container_memory_usage_bytes{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name="xxxxxxx"} - container_memory_cache{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name="xxxxxxx"})
    public static String POD_MEMORY_USAGE(String podName) {
        return String.format("sum without (dc,from,id) (container_memory_usage_bytes{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name=\"%s\"} - container_memory_cache{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name=\"%s\"})", podName, podName);
    }

    // 所有容器内存使用情况:
    public static String ALL_POD_MEMORY_USAGE(List<String> namespaceNameList) {
        return String.format("sort_desc(sum(" +
                        "container_memory_usage_bytes{container_label_io_kubernetes_docker_type=\"container\",image!=\"\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"} " +
                        "- container_memory_cache{container_label_io_kubernetes_docker_type=\"container\",image!=\"\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}) " +
                        "by (container_label_io_kubernetes_pod_name))",
                String.join("|", namespaceNameList),
                String.join("|", namespaceNameList));
    }

    // 容器硬盘写入情况:sum without (dc,from,id) (irate(container_fs_writes_bytes_total{container_label_io_kubernetes_docker_type="container",container_label_io_kubernetes_pod_name=~"xxxxxxx",image!="",device!~"/dev/dm.*"}[5m]))
    public static String POD_DISK_WRITES(String podName) {
        return String.format("sum without (dc,from,id) (irate(container_fs_writes_bytes_total{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name=~\"%s\",image!=\"\",device!~\"/dev/dm.*\"}[5m]))", podName);
    }

    // 所有容器硬盘写入情况:
    public static String ALL_POD_DISK_WRITES(List<String> namespaceNameList) {
        return String.format("sort_desc(sum (irate(container_fs_writes_bytes_total{image!=\"\",device!~\"/dev/dm.*\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}[5m])) by (container_label_io_kubernetes_pod_name))",
                String.join("|", namespaceNameList));
    }

    // 容器硬盘读取情况:sum without (dc,from,id) (irate(container_fs_reads_bytes_total{container_label_io_kubernetes_docker_type="container",container_label_io_kubernetes_pod_name=~"xxxxxxx",image!="",device!~"/dev/dm.*"}[5m]))
    public static String POD_DISK_READS(String podName) {
        return String.format("sum without (dc,from,id) (irate(container_fs_reads_bytes_total{container_label_io_kubernetes_docker_type=\"container\",container_label_io_kubernetes_pod_name=~\"%s\",image!=\"\",device!~\"/dev/dm.*\"}[5m]))", podName);
    }

    // 所有容器硬盘读取情况:
    public static String ALL_POD_DISK_READS(List<String> namespaceNameList) {
        return String.format("sort_desc(sum(irate(container_fs_reads_bytes_total{image!=\"\",device!~\"/dev/dm.*\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}[5m]))  by (container_label_io_kubernetes_pod_name))",
                String.join("|", namespaceNameList));
    }

    // 容器网络发送情况:sum(irate(container_network_transmit_bytes_total{container_label_io_kubernetes_pod_name=~"xxxxxxx",image!=""}[5m]))
    public static String POD_NETWORK_TRANSMISSION(String podName) {
        return String.format("sum(irate(container_network_transmit_bytes_total{container_label_io_kubernetes_pod_name=~\"%s\",image!=\"\"}[5m]))", podName);
    }

    // 所有容器网络发送情况:
    public static String ALL_POD_NETWORK_TRANSMISSION(List<String> namespaceNameList) {
        return String.format("sort_desc(sum(irate(container_network_transmit_bytes_total{image!=\"\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}[5m])) by (container_label_io_kubernetes_pod_name))",
                String.join("|", namespaceNameList));
    }

    // 容器网络接收情况:sum (irate(container_network_receive_bytes_total{container_label_io_kubernetes_pod_name=~"xxxxxxx",image!=""}[5m]))
    public static String POD_NETWORK_RECEPTION(String podName) {
        return String.format("sum (irate(container_network_receive_bytes_total{container_label_io_kubernetes_pod_name=~\"%s\",image!=\"\"}[5m]))", podName);
    }

    // 所有容器网络接收情况:
    public static String ALL_POD_NETWORK_RECEPTION(List<String> namespaceNameList) {
        return String.format("sort_desc(sum (irate(container_network_receive_bytes_total{image!=\"\",container_label_io_kubernetes_pod_name!=\"\",container_label_io_kubernetes_pod_namespace=~\"%s\"}[5m])) by (container_label_io_kubernetes_pod_name))",
                String.join("|", namespaceNameList));
    }

    // 节点CPU使用率:(sum by(instance) (irate(node_cpu_seconds_total{ip="xxxxxxx",mode!="idle"}[1m])) / on(instance) group_left sum by (instance)((irate(node_cpu_seconds_total{ip="xxxxxxx"}[1m])))) * 100
    public static String NODE_CPU_USAGE(String nodeIp) {
        return String.format("(sum by(instance) (irate(node_cpu_seconds_total{ip=\"%s\",mode!=\"idle\"}[1m])) / on(instance) group_left sum by (instance)((irate(node_cpu_seconds_total{ip=\"%s\"}[1m])))) * 100", nodeIp, nodeIp);
    }

    // 节点内存使用率:100 - ((node_memory_MemAvailable_bytes{ip="xxxxxxx"} * 100) / node_memory_MemTotal_bytes{ip="xxxxxxx"})
    public static String NODE_MEMORY_USAGE(String nodeIp) {
        return String.format("100 - ((node_memory_MemAvailable_bytes{ip=\"%s\"} * 100) / node_memory_MemTotal_bytes{ip=\"%s\"})", nodeIp, nodeIp);
    }

    // Pod启动数
    public static String POD_START_COUNT(String clusterId, List<String> namespaceNameList) {
        return String.format("sum(kube_pod_status_phase{phase=\"Running\",cluster=\"%s\",namespace=~\"%s\"})", clusterId, String.join("|", namespaceNameList));
    }

    // Pod总数
    public static String POD_COUNT(String clusterId, List<String> namespaceNameList) {
        return String.format("sum(kube_pod_status_phase{cluster=\"%s\",namespace=~\"%s\"})", clusterId, String.join("|", namespaceNameList));
    }

    // Pod启动时间
    public static String POD_START_DATE(String clusterId, List<String> namespaceNameList) {
        return String.format("kube_pod_created{cluster=\"%s\",namespace=~\"%s\"}", clusterId, String.join("|", namespaceNameList));
    }

    // K8S节点信息列表
    public static String NODE_INFO() {
        return "up{job=\"node-exporter\"}";
    }
}
