async function configureHighCardinalityAnomalyDetection()

in monitoring-stack-cdk/resources/custom-resources/kibana-config/kibanaConfigurer.js [159:276]


async function configureHighCardinalityAnomalyDetection(domainName) {
  const detectorName = "agent-call-quality-detector"
  const createDetector = {
    method: 'POST',
    headers: {
      "accept": "application/json, text/plain, */*",
      "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
      "cache-control": "no-cache",
      "content-type": "application/json;charset=UTF-8",
      "kbn-version": "7.9.1",
      "pragma": "no-cache",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      host: domainName
    },
    path: '_plugin/kibana/api/anomaly_detectors/detectors',
    body: Buffer.from(JSON.stringify({
      "name": detectorName,
      "description":"Detects network metric anomalies for individual agents that may impact call quality.",
      "timeField":"doc.timestamp",
      "indices":["softphonestreamstats*"],
      "filterQuery": { match_all: { boost: 1 } },
      "uiMetadata":{"filterType":"simple_filter","filters":[],"features":{}},
      "detectionInterval":{"period":{"interval":2,"unit":"MINUTES"}},
      "windowDelay":{"period":{"interval":1,"unit":"MINUTES"}},
      featureAttributes: [
        {
          featureName: 'round-trip-time',
          featureEnabled: true,
          importance: 1,
          aggregationQuery: {
              round_trip_time: {
                  avg: {
                      field: "doc.roundTripTimeMillis"
                  }
              }
          }
        },
        {
          featureName: 'jitter',
          featureEnabled: true,
          importance: 1,
          aggregationQuery: {
              jitter: {
                  avg: {
                      field: "doc.jitterBufferMillis"
                  }
              }
          }
        },
        {
          featureName: 'packet-loss',
          featureEnabled: true,
          importance: 1,
          aggregationQuery: {
              packet_loss: {
                  avg: {
                      field: "doc.packetsLost"
                  }
              }
          }
        }
      ],
      categoryField: [ 'doc.agent.keyword' ],
      ui_metadata : {
      "features" : {
          "jitter" : {
            "aggregationBy" : "avg",
            "aggregationOf" : "doc.jitterBufferMillis",
            "featureType" : "simple_aggs"
          },
          "packet-loss" : {
            "aggregationBy" : "avg",
            "aggregationOf" : "doc.packetsLost",
            "featureType" : "simple_aggs"
          },
          "round-trip-time" : {
            "aggregationBy" : "avg",
            "aggregationOf" : "doc.roundTripTimeMillis",
            "featureType" : "simple_aggs"
          }
        }
      }
    }))
  }

  const detectorInfo = JSON.parse(await sendRequest(createDetector, domainName));
  if(!detectorInfo.ok) {
    throw new Error(detectorInfo.error);
  }
  console.log(detectorInfo);
  
  const startDetector = {
    method: 'POST',
    headers: {
      "accept": "application/json, text/plain, */*",
      "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
      "cache-control": "no-cache",
      "content-type": "application/json;charset=UTF-8",
      "kbn-version": "7.9.1",
      "pragma": "no-cache",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      host: domainName
    },
    path: `_plugin/kibana/api/anomaly_detectors/detectors/${detectorInfo.response.id}/start`,
    body: Buffer.from(JSON.stringify({
      detectorId: detectorInfo.response.id
    }))
  }
  
  const enableDetectorResult = JSON.parse(await sendRequest(startDetector, domainName));
  console.log(JSON.stringify(enableDetectorResult));
  if(!enableDetectorResult.ok)
    throw new Error(enableDetectorResult.error)
}