{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/analysis_rule.schema.json",
  "title": "An analysis rule: input event types, a window, a condition, an output event type. Data, never code.",
  "type": "object",
  "required": ["id", "lane", "inputs", "group_by", "window_s", "condition", "output"],
  "additionalProperties": false,
  "properties": {
    "id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9_.-]*$" },
    "lane": { "enum": ["maintenance", "prediction", "tracking", "kpi"] },
    "about": { "type": "string" },
    "inputs": { "type": "array", "minItems": 1, "items": { "$ref": "common.schema.json#/$defs/event_type_id" } },
    "where": {
      "type": "object",
      "description": "Field equalities an input must meet to count (reason: safety_trip).",
      "additionalProperties": { "type": ["string", "number", "boolean"] }
    },
    "group_by": { "enum": ["asset", "none"] },
    "window_s": { "type": "integer", "minimum": 1 },
    "condition": {
      "type": "object",
      "required": ["count_gte"],
      "additionalProperties": false,
      "properties": { "count_gte": { "type": "integer", "minimum": 1 } }
    },
    "cooldown": {
      "enum": ["reset", "none"],
      "description": "reset: once raised for a group, its count starts again from zero. none: raised on every qualifying event."
    },
    "output": {
      "type": "object",
      "required": ["event_type", "fields"],
      "additionalProperties": false,
      "properties": {
        "event_type": { "$ref": "common.schema.json#/$defs/event_type_id", "description": "A catalogue type marked derived." },
        "fields": { "type": "object", "description": "Field values: a constant, or $count, $window_s, $rule, $device." },
        "propose": {
          "type": "object",
          "required": ["work_order"],
          "additionalProperties": false,
          "properties": {
            "work_order": {
              "type": "object",
              "required": ["task", "priority"],
              "additionalProperties": false,
              "properties": { "task": { "type": "string" }, "priority": { "enum": ["low", "medium", "high", "highest"] } }
            }
          }
        }
      }
    }
  }
}
