{
  "id": "3dc31eb5-261a-4d7c-b014-6e7918698f7c",
  "title": "check_logs",
  "tasks": {
    "log_check": {
      "name": "log_check",
      "input": {
        "query": "fetch logs\n|filter matchesPhrase(content,\"Error\")\n| makeTimeseries by:{dt.entity.host}, interval:1m,  errorCount = count()"
      },
      "action": "dynatrace.automations:execute-dql-query",
      "position": {
        "x": 0,
        "y": 1
      },
      "description": "Make use of Dynatrace Grail data in your workflow.",
      "predecessors": []
    },
    "check_logs": {
      "name": "check_logs",
      "input": {
        "body": {
          "threshold": 0,
          "slidingWindow": 1,
          "useModelCache": true,
          "alertCondition": "ABOVE",
          "extendedOutput": true,
          "timeSeriesData": "fetch logs\n|filter matchesPhrase(content,\"Error\")\n| makeTimeseries by:{dt.entity.host}, interval:1m,  errorCount = count()",
          "violatingSamples": 1,
          "dealertingSamples": 1,
          "generalParameters": {
            "timeframe": {
              "endTime": "now",
              "startTime": "now()-5m"
            },
            "logVerbosity": "TRACING",
            "resolveDimensionalQueryData": true
          },
          "alertOnMissingData": false
        },
        "analyzerName": "dt.statistics.anomaly_detection.StaticThresholdAnomalyDetectionAnalyzer"
      },
      "action": "dynatrace.davis.workflow.actions:davis-analyze",
      "active": true,
      "position": {
        "x": 0,
        "y": 2
      },
      "conditions": {
        "states": {}
      },
      "description": "Execute a customizable AI/ML task using Davis® analyzers",
      "predecessors": []
    },
    "run_javascript_1": {
      "name": "run_javascript_1",
      "input": {
        "script": "import { eventsClient, EventIngestEventType } from \"@dynatrace-sdk/client-classic-environment-v2\";\nimport { execution } from \"@dynatrace-sdk/automation-utils\";\n\nexport default async function ({ executionId }) {\n  // Step 1: Get workflow execution result\n  const exe = await execution(executionId);\n  const checkResult = await exe.result(\"check_logs\");\n\n  // Step 2: Inspect output structure\n  const output = checkResult?.output?.[0];\n  const dimensions = output?.dimensions || [];\n\n  console.log(\"Full dimensions:\", JSON.stringify(dimensions, null, 2));\n\n  // Step 3: Extract potentialAlerts from dimensions\n  const potentialAlertDim = dimensions.find(dim => dim.key === \"potentialAlerts\");\n  console.log(\"Raw potentialAlertDim:\", potentialAlertDim);\n\n  const potentialAlerts = parseInt((potentialAlertDim?.value || \"0\").trim(), 10);\n  console.log(\"Parsed potentialAlerts:\", potentialAlerts);\n\n  // Step 4: If alert count > 0, send a custom event\n  if (potentialAlerts > 0) {\n    const hostDimension = dimensions.find(dim => dim.key === \"dt.entity.host\");\n    const hostId = hostDimension?.value;\n\n    if (hostId) {\n      console.log(`Raising event on host: ${hostId}`);\n\n      await eventsClient.createEvent({\n        body: {\n          eventType: EventIngestEventType.CustomAnnotation,\n          title: \"⚠️ Predicted Disk Capacity Alert\",\n          description: \"Disk issue detected based on timeseries analysis\",\n          entitySelector: `type(HOST),entityId(\"${hostId}\")`,\n          properties: {\n            \"dt.entity.host\": hostId,\n            severity: \"warning\"\n          },\n          source: \"workflow-automation\"\n        }\n      });\n    } else {\n      console.log(\"Host ID not found in dimensions.\");\n    }\n  } else {\n    console.log(\"No potential alerts found.\");\n  }\n};"
      },
      "action": "dynatrace.automations:run-javascript",
      "position": {
        "x": 0,
        "y": 3
      },
      "conditions": {
        "states": {
          "check_logs": "OK"
        }
      },
      "description": "Run custom JavaScript code.",
      "predecessors": [
        "check_logs"
      ]
    }
  },
  "description": "",
  "actor": "03521efe-0110-4c8e-afad-0a1480c9d7ef",
  "owner": "03521efe-0110-4c8e-afad-0a1480c9d7ef",
  "ownerType": "USER",
  "isPrivate": true,
  "trigger": {
    "schedule": {
      "isActive": false,
      "isFaulty": false,
      "trigger": {
        "cron": "*/2 * * * *",
        "type": "cron"
      },
      "rule": null,
      "filterParameters": {},
      "timezone": "America/Chicago",
      "inputs": {}
    }
  },
  "schemaVersion": 3,
  "result": null,
  "input": {},
  "hourlyExecutionLimit": 1000,
  "type": "STANDARD"
}