{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ringsaturn/xue/blob/main/docs/schemas/aq-index.schema.json",
  "title": "AqIndex",
  "description": "aq/<YYYY>/<MM>/<DD>/aq.<round>/index.json (docs/aq.md section 4). Each file's name, the networks' and elements' uniqueness and the element table's order are the readers' own checks.",
  "type": "object",
  "required": ["schemaVersion", "issued", "generated", "networks", "elements", "sources"],
  "properties": {
    "schemaVersion": {"type": "integer", "const": 1},
    "issued": {
      "description": "The round: a UTC minute that is a multiple of five.",
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d[05]:00Z$",
      "format": "date-time"
    },
    "generated": {"$ref": "#/$defs/Timestamp"},
    "networks": {"type": "array", "items": {"$ref": "#/$defs/AqNetwork"}},
    "elements": {
      "description": "The element order of every stations file's rows.",
      "type": "array",
      "items": {"type": "string", "pattern": "^[a-z][a-z0-9]*$"}
    },
    "elementTable": {
      "description": "One entry per element of elements, in the same order; absent in a round from before the table.",
      "type": ["object", "null"],
      "additionalProperties": {"$ref": "#/$defs/AqElementInfo"}
    },
    "sources": {"type": "array", "items": {"$ref": "#/$defs/AqSource"}},
    "builder": {"$ref": "#/$defs/Builder"}
  },
  "$defs": {
    "Timestamp": {
      "description": "ISO 8601 UTC, to the second, with a Z suffix.",
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$",
      "format": "date-time"
    },
    "CrcFile": {
      "description": "A file beside the index: its name, length and zlib CRC32.",
      "type": "object",
      "required": ["path", "byteLength", "crc32"],
      "properties": {
        "path": {"type": "string", "minLength": 1},
        "byteLength": {"type": "integer", "minimum": 0},
        "crc32": {"type": "string", "pattern": "^[0-9a-f]{8}$"}
      }
    },
    "StationsFile": {
      "description": "The network's stations file, with its row count.",
      "type": "object",
      "required": ["path", "byteLength", "crc32", "count"],
      "properties": {
        "path": {"type": "string", "minLength": 1},
        "byteLength": {"type": "integer", "minimum": 0},
        "crc32": {"type": "string", "pattern": "^[0-9a-f]{8}$"},
        "count": {"type": "integer", "minimum": 0}
      }
    },
    "GasReference": {
      "description": "The reference conditions the network's gases were converted from µg/m³ at.",
      "type": "object",
      "required": ["temperature", "pressure"],
      "properties": {
        "temperature": {"description": "Kelvin.", "type": "number", "minimum": 200, "maximum": 400},
        "pressure": {"description": "Pascals.", "type": "number", "minimum": 50000, "maximum": 120000}
      }
    },
    "AqNetwork": {
      "description": "One network with a station in the window; its files are always <id>.jsonl and <id>.stations.json.",
      "type": "object",
      "required": ["id", "name", "cadence", "avgPeriod", "preliminary", "attribution", "license", "url", "file", "stations"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z][a-z0-9]*(-[a-z0-9]+)*$"},
        "name": {"type": "string", "minLength": 1},
        "cadence": {"description": "Seconds between a station's reports.", "type": "integer", "minimum": 60, "maximum": 86400},
        "avgPeriod": {"description": "Seconds each value averages over, ending at its time.", "type": "integer", "minimum": 60, "maximum": 86400},
        "preliminary": {"type": "boolean"},
        "gasReference": {"anyOf": [{"$ref": "#/$defs/GasReference"}, {"type": "null"}]},
        "attribution": {"type": "string", "minLength": 1},
        "license": {"type": "string", "minLength": 1},
        "licenseUrl": {"type": ["string", "null"], "pattern": "^https://"},
        "url": {"type": "string", "minLength": 1},
        "latest": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "format": "date-time"},
        "file": {"$ref": "#/$defs/CrcFile"},
        "stations": {"$ref": "#/$defs/StationsFile"}
      }
    },
    "AqElementInfo": {
      "description": "How an element's value stands for its time.",
      "type": "object",
      "required": ["unit", "cell"],
      "properties": {
        "unit": {"type": "string", "minLength": 1},
        "cell": {"enum": ["point", "mean", "sum", "max", "min"]},
        "period": {
          "description": "Seconds the value covers, ending at its time; network for the network's avgPeriod; null for an instantaneous value.",
          "anyOf": [{"type": "integer", "minimum": 1}, {"const": "network"}, {"type": "null"}]
        }
      }
    },
    "AqSource": {
      "description": "One source's outcome (docs/aq.md section 7). That ok false carries error, and that ids are unique, are the readers' own checks.",
      "type": "object",
      "required": ["id", "ok"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z][a-z0-9-]*$"},
        "ok": {"type": "boolean"},
        "error": {"type": "string"},
        "fetched": {"$ref": "#/$defs/Timestamp"},
        "url": {"type": "string"},
        "files": {"type": "integer", "minimum": 0},
        "missing": {"type": "integer", "minimum": 0},
        "reports": {"type": "integer", "minimum": 0},
        "unplaced": {"type": "integer", "minimum": 0},
        "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
      }
    },
    "Builder": {
      "description": "The code that built the round.",
      "type": ["object", "null"],
      "required": ["name"],
      "properties": {
        "name": {"const": "xuebuild"},
        "revision": {"type": ["string", "null"], "pattern": "^[0-9a-f]{7,40}$"}
      }
    }
  }
}
