{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/onclarity-com/json-schema/onclarity-com-admin-aggregate-records-request-schema.json",
  "title": "AdminAggregateRecordsRequest",
  "description": "Admin variant of the aggregate request: explicit project scope.",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/onclarity-com-agent-store-admin-api-openapi.yml#/components/schemas/AdminAggregateRecordsRequest",
  "properties": {
    "group_by": {
      "items": {
        "anyOf": [
          {
            "type": "string"
          },
          {
            "$ref": "#/$defs/GroupBySpec"
          }
        ]
      },
      "type": "array",
      "title": "Group By",
      "description": "0..3 group keys: a field name, or {field, granularity} to bucket a date-time field."
    },
    "metrics": {
      "items": {
        "$ref": "#/$defs/MetricSpec"
      },
      "type": "array",
      "title": "Metrics",
      "description": "1..5 aggregates computed per bucket (default: count)."
    },
    "filters": {
      "additionalProperties": true,
      "type": "object",
      "title": "Filters",
      "description": "Record filters, all AND-ed: {field: condition} where a condition is a value (equals), null (the field is empty), a list (one of), or an object of operators — eq, neq, in, not_in, gt, gte, lt, lte, empty (true|false). neq/not_in keep records whose field is empty. Fields: the collection's scalar fields plus id, created_at, updated_at. A date-time without an offset is read as UTC; a filter carries at most 1000 values in total."
    },
    "sort": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Sort",
      "description": "Order groups by a group_by field or a metric label ('count', 'sum_amount'); default: the first metric."
    },
    "order": {
      "anyOf": [
        {
          "type": "string",
          "enum": [
            "asc",
            "desc"
          ]
        },
        {
          "type": "null"
        }
      ],
      "title": "Order",
      "description": "Sort direction (default desc)."
    },
    "limit": {
      "type": "integer",
      "title": "Limit",
      "description": "Groups per page, 1..100.",
      "default": 50
    },
    "offset": {
      "type": "integer",
      "title": "Offset",
      "description": "Groups to skip — page until offset reaches total.",
      "default": 0
    },
    "project_id": {
      "type": "integer",
      "title": "Project Id",
      "description": "Project the collection belongs to."
    }
  },
  "type": "object",
  "required": [
    "project_id"
  ],
  "$defs": {
    "GroupBySpec": {
      "properties": {
        "field": {
          "type": "string",
          "title": "Field",
          "description": "Field to group by."
        },
        "granularity": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "day",
                "week",
                "month"
              ]
            },
            {
              "type": "null"
            }
          ],
          "title": "Granularity",
          "description": "Date bucket for a date-time field; omit to group by the raw value."
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "field"
      ],
      "title": "GroupBySpec",
      "description": "A group_by entry that buckets a date-time field (UTC; a week starts on Monday)."
    },
    "MetricSpec": {
      "properties": {
        "op": {
          "type": "string",
          "enum": [
            "count",
            "count_distinct",
            "sum",
            "avg",
            "min",
            "max"
          ],
          "title": "Op",
          "description": "Aggregate operation."
        },
        "field": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "title": "Field",
          "description": "Schema field the op applies to."
        }
      },
      "type": "object",
      "required": [
        "op"
      ],
      "title": "MetricSpec",
      "description": "One aggregate to compute per group. ``count`` needs no field; ``sum``/``avg``\nrequire a numeric field; ``count_distinct``/``min``/``max`` any non-JSON field."
    }
  }
}