{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.netify.ai/developer/agent/v5/schemas/netify-config-sink-sqlite.json",
  "title": "Netify Sink SQLite Configuration",
  "description": "JSON Schema for the Netify Sink SQLite Plugin configuration.",
  "type": "object",
  "properties": {
    "$schema": {
      "type": "string",
      "description": "Schema reference for editors and validators. Ignored by the plugin."
    },
    "db_path": {
      "type": "string",
      "default": "/tmp",
      "description": "Default directory for database files. Supports Agent variables such as ${path_state_persistent}. The directory must exist.",
      "examples": ["${path_state_persistent}/db"]
    },
    "purge_in_days": {
      "type": "integer",
      "minimum": 0,
      "default": 0,
      "description": "Default age, in days, at which rows are purged. Purging runs at startup and once a day. 0 disables purging on core channels."
    },
    "channels": {
      "type": "object",
      "description": "Channel configurations, keyed by the channel name processor plugins dispatch payloads to.",
      "additionalProperties": {
        "$ref": "#/$defs/channel"
      },
      "examples": [
        {
          "default": {
            "db_name": "stats.db",
            "data_source": "aggregator",
            "purge_in_days": 14,
            "tables": [
              {
                "name": "stats",
                "columns": [
                  { "name": "application_id", "mapped": "detected_application", "type": "INTEGER" },
                  { "name": "tag", "mapped": "tags", "split": 0, "type": "TEXT" }
                ]
              }
            ]
          },
          "core": {
            "db_name": "flows.db",
            "data_source": "core",
            "tables": [
              {
                "name": "flows",
                "type": "flow_dpi_complete",
                "upsert": true,
                "purge_column": "last_seen_at",
                "columns": [
                  { "name": "flow_id", "mapped": "flow.id.xxh64", "type": "TEXT", "primary_key": true, "not_null": true },
                  { "name": "local_ip", "mapped": "flow.local_ip", "type": "TEXT" },
                  { "name": "last_seen_at", "calculated_value": "flow.last_seen_at / 1000", "type": "INTEGER", "index": true }
                ]
              }
            ],
            "flow_metadata": {
              "enable": true,
              "exclude": ["risks"]
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false,
  "$defs": {
    "channel": {
      "type": "object",
      "description": "A database channel: one SQLite database file, and the tables that payloads dispatched to the channel are written to.",
      "properties": {
        "enable": {
          "type": "boolean",
          "default": true,
          "description": "State of the channel. A disabled channel is skipped without being loaded."
        },
        "db_name": {
          "type": "string",
          "minLength": 1,
          "description": "Database file name, in db_path. SQLite keeps <db_name>-wal and <db_name>-shm files beside it.",
          "examples": ["stats.db", "flows.db"]
        },
        "db_path": {
          "type": "string",
          "description": "Channel-specific directory for the database file. Supports Agent variables such as ${path_state_persistent}."
        },
        "data_source": {
          "enum": ["aggregator", "core"],
          "description": "The processor whose payloads the channel receives: 'aggregator' (netify-proc-aggregator interval summaries) or 'core' (netify-proc-core per-event telemetry). It decides the shape of tables."
        },
        "purge_in_days": {
          "type": "integer",
          "minimum": 0,
          "description": "Channel-specific purge age in days."
        },
        "tables": {
          "type": "array",
          "minItems": 1,
          "description": "Table definitions. Their shape depends on data_source."
        },
        "flow_metadata": {
          "$ref": "#/$defs/flowMetadata"
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": { "enable": { "const": false } },
            "required": ["enable"]
          },
          "else": {
            "required": ["db_name", "data_source", "tables"]
          }
        },
        {
          "if": {
            "properties": { "data_source": { "const": "aggregator" } },
            "required": ["data_source"]
          },
          "then": {
            "properties": {
              "tables": {
                "maxItems": 1,
                "items": { "$ref": "#/$defs/aggregatorTable" }
              },
              "flow_metadata": {
                "properties": { "enable": { "const": false } }
              }
            }
          }
        },
        {
          "if": {
            "properties": { "data_source": { "const": "core" } },
            "required": ["data_source"]
          },
          "then": {
            "properties": {
              "tables": {
                "items": { "$ref": "#/$defs/coreTable" }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "flow_metadata": {
                "properties": { "enable": { "const": true } },
                "required": ["enable"]
              }
            },
            "required": ["flow_metadata"]
          },
          "then": {
            "properties": {
              "tables": {
                "contains": {
                  "properties": {
                    "type": { "const": "flow_dpi_complete" },
                    "columns": {
                      "contains": {
                        "properties": { "primary_key": { "const": true } },
                        "required": ["primary_key"]
                      }
                    }
                  },
                  "required": ["type", "columns"]
                }
              }
            }
          }
        }
      ]
    },
    "flowMetadata": {
      "type": "object",
      "description": "Per-flow protocol metadata (ssl, http, dns, conntrack, ...) recorded in a flow_meta table, one row per key, from flow_dpi_complete payloads. Core channels only. Requires a flow_dpi_complete table with a primary_key column, which flow_meta rows reference.",
      "properties": {
        "enable": {
          "type": "boolean",
          "default": false,
          "description": "Record flow metadata."
        },
        "exclude": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Metadata keys to leave out. A key the Agent doesn't emit is ignored.",
          "examples": [["risks"]]
        }
      },
      "additionalProperties": false
    },
    "aggregatorTable": {
      "type": "object",
      "description": "The aggregator channel's table. Each entry of a payload's stats array becomes one row, linked to a <channel>_intervals row. The plugin adds an id primary key and an interval_id foreign key column.",
      "properties": {
        "name": {
          "$ref": "#/$defs/identifier",
          "description": "Table name."
        },
        "columns": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/aggregatorColumn" },
          "description": "Columns, in addition to id and interval_id."
        }
      },
      "required": ["name", "columns"],
      "additionalProperties": false
    },
    "coreTable": {
      "type": "object",
      "description": "A core channel table, written from every payload whose type matches. Several tables may share a type: each is written, in order, in one transaction per payload. Several entries may share a name: the table is created from the first, so later entries must only use its columns.",
      "properties": {
        "name": {
          "$ref": "#/$defs/identifier",
          "description": "Table name."
        },
        "type": {
          "type": "string",
          "minLength": 1,
          "description": "The payload type this table records. Payloads of other types are skipped.",
          "examples": ["flow", "flow_dpi_update", "flow_dpi_complete", "flow_stats", "flow_purge", "agent_status", "interfaces", "endpoints", "interface_stats", "global_stats"]
        },
        "upsert": {
          "type": "boolean",
          "default": false,
          "description": "Write rows as INSERT ... ON CONFLICT(<primary_key>) DO UPDATE, so later events update a row instead of duplicating it. Requires a primary_key column. Only the columns listed are updated, and a missing value overwrites the stored one with NULL."
        },
        "require": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "description": "Payload paths, written like mapped, that must all be present and not null for this table to get a row. A payload missing any of them is skipped for this table only.",
          "examples": [["flow.ssl.version"]]
        },
        "iterate_keys": {
          "type": "boolean",
          "default": false,
          "description": "For payloads that carry a map of objects keyed by interface name (interfaces, endpoints, interface_stats): write one row per map entry, with the key available as _key."
        },
        "purge_column": {
          "$ref": "#/$defs/identifier",
          "description": "One of this table's INTEGER columns holding Unix epoch seconds. Rows older than purge_in_days by this column are deleted. Tables without one are never purged."
        },
        "columns": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/coreColumn" },
          "contains": {
            "properties": { "primary_key": { "const": true } },
            "required": ["primary_key"]
          },
          "minContains": 0,
          "maxContains": 1,
          "description": "Columns. At most one may be a primary_key."
        }
      },
      "required": ["name", "type", "columns"],
      "additionalProperties": false,
      "if": {
        "properties": { "upsert": { "const": true } },
        "required": ["upsert"]
      },
      "then": {
        "properties": {
          "columns": {
            "contains": {
              "properties": { "primary_key": { "const": true } },
              "required": ["primary_key"]
            }
          }
        }
      }
    },
    "aggregatorColumn": {
      "type": "object",
      "description": "An aggregator table column.",
      "properties": {
        "name": {
          "allOf": [
            { "$ref": "#/$defs/identifier" },
            { "not": { "enum": ["id", "interval_id"] } }
          ],
          "description": "Column name. id and interval_id are added by the plugin and can't be used."
        },
        "type": {
          "$ref": "#/$defs/columnType"
        },
        "mapped": {
          "type": "string",
          "minLength": 1,
          "not": { "enum": ["id", "interval_id"] },
          "description": "Field of the stats record to read the value from. Top-level fields only, with no dot paths. Defaults to the column name.",
          "examples": ["detected_application", "tags"]
        },
        "split": {
          "type": "integer",
          "minimum": 0,
          "description": "For TEXT columns: split the value on '.' and keep the element at this index. An index past the end stores an empty string.",
          "examples": [0, 1]
        },
        "calculated_value": {
          "$ref": "#/$defs/calculatedValue"
        },
        "precision": {
          "$ref": "#/$defs/precision"
        },
        "default": {
          "$ref": "#/$defs/columnDefault"
        },
        "not_null": {
          "type": "boolean",
          "default": false,
          "description": "Add NOT NULL. A missing value fails the insert, and nothing from the payload is written."
        },
        "index": {
          "$ref": "#/$defs/index"
        }
      },
      "required": ["name", "type"],
      "additionalProperties": false,
      "dependentSchemas": {
        "calculated_value": { "not": { "required": ["mapped"] } },
        "split": { "properties": { "type": { "const": "TEXT" } } },
        "precision": { "properties": { "type": { "const": "REAL" } } }
      }
    },
    "coreColumn": {
      "type": "object",
      "description": "A core table column.",
      "properties": {
        "name": {
          "$ref": "#/$defs/identifier",
          "description": "Column name. A column named id is never written by the plugin: declare it as an INTEGER primary_key so SQLite assigns it."
        },
        "type": {
          "$ref": "#/$defs/columnType"
        },
        "mapped": {
          "type": "string",
          "minLength": 1,
          "description": "Dot-separated payload path to read the value from, e.g. flow.local_ip. A path without dots is a top-level field, and with iterate_keys, _key is the map key. Defaults to the column name.",
          "examples": ["flow.local_ip", "flow.id.xxh64", "reason", "_key"]
        },
        "calculated_value": {
          "$ref": "#/$defs/calculatedValue"
        },
        "precision": {
          "$ref": "#/$defs/precision"
        },
        "default": {
          "$ref": "#/$defs/columnDefault"
        },
        "not_null": {
          "type": "boolean",
          "default": false,
          "description": "Add NOT NULL. A payload with no value for the column is not written, and the missing path is logged."
        },
        "primary_key": {
          "type": "boolean",
          "default": false,
          "description": "Add PRIMARY KEY. At most one per table. A payload with no value for the column is not written, and the missing path is logged."
        },
        "autoincrement": {
          "type": "boolean",
          "default": false,
          "description": "Add AUTOINCREMENT, and leave the column out of inserts so SQLite assigns it. Requires an INTEGER primary_key."
        },
        "foreign_key": {
          "$ref": "#/$defs/foreignKey"
        },
        "index": {
          "$ref": "#/$defs/index"
        }
      },
      "required": ["name", "type"],
      "additionalProperties": false,
      "dependentSchemas": {
        "calculated_value": { "not": { "required": ["mapped"] } },
        "precision": { "properties": { "type": { "const": "REAL" } } }
      },
      "if": {
        "properties": { "autoincrement": { "const": true } },
        "required": ["autoincrement"]
      },
      "then": {
        "properties": {
          "primary_key": { "const": true },
          "type": { "const": "INTEGER" }
        },
        "required": ["primary_key"]
      }
    },
    "identifier": {
      "type": "string",
      "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
      "description": "An SQL identifier: letters, digits and underscores, not starting with a digit. Written into SQL unquoted."
    },
    "columnType": {
      "enum": ["INTEGER", "REAL", "TEXT"],
      "description": "SQLite column type, which also decides how the value is converted. TEXT stores arrays as their elements joined by spaces, and other non-string values as JSON."
    },
    "calculatedValue": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_.+\\-*/() \\t]+$",
      "description": "Arithmetic expression to compute the value from, instead of mapped: + - * / with the usual precedence, parentheses, numeric literals, and field references written like mapped. Fields must be numbers, booleans or numeric strings. If the expression can't be evaluated (a missing field, division by zero), the column is stored as NULL. INTEGER columns round the result.",
      "examples": ["flow.last_seen_at / 1000", "(cpu_system + cpu_user - cpu_system_prev - cpu_user_prev) / (update_interval * cpu_cores) * 100"]
    },
    "precision": {
      "type": "integer",
      "minimum": 0,
      "description": "For REAL columns: round the value to this many decimal places.",
      "examples": [2]
    },
    "columnDefault": {
      "type": ["number", "boolean", "null"],
      "description": "Written as the column's SQL DEFAULT when the table is created. The plugin writes every column on insert (NULL when the value is missing), so this doesn't affect rows the plugin writes."
    },
    "index": {
      "type": "boolean",
      "default": false,
      "description": "Create the index idx_<table>_<column>. Also applied to a table that already exists. Index foreign keys and purge columns."
    },
    "foreignKey": {
      "type": "object",
      "description": "Add FOREIGN KEY (<column>) REFERENCES <table>(<column>). Foreign keys are enforced.",
      "properties": {
        "table": {
          "$ref": "#/$defs/identifier",
          "description": "Referenced table."
        },
        "column": {
          "$ref": "#/$defs/identifier",
          "description": "Referenced column."
        },
        "on_delete": {
          "enum": ["CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT", "NO ACTION"],
          "description": "ON DELETE action. CASCADE deletes child rows along with their parent, e.g. when the parent is purged."
        }
      },
      "required": ["table", "column"],
      "additionalProperties": false,
      "examples": [{ "table": "flows", "column": "flow_id", "on_delete": "CASCADE" }]
    }
  }
}
