{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ringsaturn/xue/blob/main/docs/schemas/synop-index.schema.json",
  "title": "SynopIndex",
  "description": "synop/<YYYY>/<MM>/<DD>/synop.<round>/index.json, schema v2 (docs/synop.md section 4); v1 is synop-index-v1.schema.json. Each file's name, the networks' and elements' uniqueness, the element table's order and that a v2 index carries no stations are the readers' own checks.",
  "type": "object",
  "required": ["schemaVersion", "issued", "generated", "networks", "elements", "sources"],
  "properties": {
    "schemaVersion": {"type": "integer", "const": 2},
    "issued": {"$ref": "#/$defs/Round"},
    "generated": {"$ref": "#/$defs/Timestamp"},
    "networks": {"type": "array", "items": {"$ref": "#/$defs/SynopNetwork"}},
    "elements": {"$ref": "#/$defs/Elements"},
    "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/SynopElementInfo"}
    },
    "sources": {"type": "array", "items": {"$ref": "#/$defs/SynopSource"}},
    "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"
    },
    "Round": {
      "description": "The round: a UTC minute that is a multiple of ten.",
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d0:00Z$",
      "format": "date-time"
    },
    "Elements": {
      "description": "The element order of the stations rows.",
      "type": "array",
      "items": {"type": "string", "pattern": "^[a-z][a-z0-9]*$"}
    },
    "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 beside the index, and 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}
      }
    },
    "Seconds": {
      "description": "A period, seconds.",
      "type": ["integer", "null"],
      "minimum": 60,
      "maximum": 86400
    },
    "SynopNetwork": {
      "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", "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},
        "prPeriod": {"$ref": "#/$defs/Seconds"},
        "wsPeriod": {"$ref": "#/$defs/Seconds"},
        "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"}
      }
    },
    "SynopElementInfo": {
      "description": "An element's unit, how its value stands for its time, and the seconds it covers: a number, network (the network entry's wsPeriod or prPeriod), or null for an instantaneous value.",
      "type": "object",
      "required": ["unit", "cell"],
      "properties": {
        "unit": {"type": "string", "minLength": 1},
        "cell": {"enum": ["point", "mean", "sum", "max", "min"]},
        "period": {
          "anyOf": [
            {"type": "integer", "minimum": 1},
            {"const": "network"},
            {"type": "null"}
          ]
        }
      }
    },
    "SynopSource": {
      "description": "One source's outcome (docs/synop.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"},
        "reports": {"type": "integer", "minimum": 0},
        "snapshots": {"type": "integer", "minimum": 0},
        "missing": {"type": "integer", "minimum": 0},
        "files": {"type": "integer", "minimum": 0},
        "unplaced": {"type": "integer", "minimum": 0},
        "sha256": {"type": ["string", "null"], "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}$"}
      }
    }
  }
}
