const commonInfo = {
    // baseUrl: 'http://132.126.207.131:35690/hudi_services/service_web',
    baseUrl: '/hudi_services/service_web',
    clusters: {
        sync: {
            'b12': 'default',
        },
        sync_names() {
            return Object.keys(this.sync).join(',')
        },
        compaction: {
            'b12': 'default',
            'b1': 'datalake',
            'b5': 'ten_iap.datalake',
            'a4': 'ten_iap.datalake',
        },
        compaction_names() {
            return Object.keys(this.sync).join(',')
        },
    },
    loki: {
        grafanaUrl: 'http://132.126.207.125:35700',
        hudi: {
            datasource: 'f648174e-7593-45cf-8fe8-8f8d5cf0fdde',
        },
        service: {
            datasource: 'b6fee51c-facd-4261-a0eb-8c69a975fba3',
        }
    },
}

/**
 * Queries
 *  {datasource, queryMap}
 */
function generateLokiPanel(queries) {
    return {
        LWF: {
            queries: [
                ...queries.map(item => {
                    let name = item['name']
                    let datasource = item['datasource']
                    let queryMap = item['queryMap']
                    let query = Object.keys(queryMap)
                        .sort()
                        .map(key => `${key}="${queryMap[key]}"`)
                    let match = "\\d{4}-(?P<time>\\d{2}-\\d{2}\\s*\\d{2}:\\d{2}:\\d{2}).+#@#\\s*(?P<content>[\\w\\W]+)"
                    let format = "{{.time}} [{{.host}}] [{{.level}}] [{{.app}}] {{.content}}"
                    // language=TEXT
                    let expression = `{${query.join(",")}}\n| regexp "${match.replaceAll('\\', '\\\\')}"\n| line_format \`${format}\``
                    return {
                        refId: name,
                        expr: expression,
                        queryType: "range",
                        datasource: {
                            type: "loki",
                            uid: datasource
                        },
                        editorMode: "code"
                    }
                })
            ],
            range: {
                from: "now-1h",
                to: "now"
            }
        }
    }
}

function generateQuery(name, datasource, queryMap) {
    return {
        name: name,
        datasource: datasource,
        queryMap: queryMap,
    }
}

function generateLokiUrl(baseUrl, queries) {
    return `${baseUrl}/explore?panes=${encodeURIComponent(JSON.stringify(generateLokiPanel(queries)))}&schemaVersion=1&orgId=1`
}

function targetHudiSyncLokiUrlByAlias(flinkJobId) {
    return generateLokiUrl(
        commonInfo.loki.grafanaUrl,
        [
            generateQuery(
                "Hudi 运行日志",
                commonInfo.loki.hudi.datasource,
                {"flink_job_id": flinkJobId}
            )
        ]
    )
}

function targetHudiCompactionLokiUrlByAlias(alias) {
    return generateLokiUrl(
        commonInfo.loki.grafanaUrl,
        [
            generateQuery(
                "Hudi 运行日志",
                commonInfo.loki.hudi.datasource,
                {"alias": alias}
            )
        ]
    )
}

function targetYarnApplicationLokiUrlByAppId(applicationId) {
    return generateLokiUrl(
        commonInfo.loki.grafanaUrl,
        [
            generateQuery(
                "Hudi 运行日志",
                commonInfo.loki.hudi.datasource,
                {"app_id": applicationId}
            )
        ]
    )
}

function serviceLogByAppName(name) {
    return generateLokiUrl(
        commonInfo.loki.grafanaUrl,
        [
            generateQuery(
                "Service 运行日志",
                commonInfo.loki.service.datasource,
                {"app": name}
            )
        ]
    )
}

function serviceLogByAppNameAndHost(name, host) {
    return generateLokiUrl(
        commonInfo.loki.grafanaUrl,
        [
            generateQuery(
                "Service 运行日志",
                commonInfo.loki.service.datasource,
                {"app": name, "host": host}
            )
        ]
    )
}
