{
  "openapi": "3.1.0",
  "info": {
    "title": "Chronicle observation query API",
    "version": "1.0.0"
  },
  "paths": {
    "/api/v1/series/{seriesId}/observations": {
      "get": {
        "operationId": "queryObservations",
        "summary": "Query raw observations or aggregate into time buckets",
        "parameters": [
          {
            "name": "seriesId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            }
          },
          {
            "name": "from",
            "in": "query",
            "required": true,
            "description": "Inclusive ISO 8601 instant with timezone offset; maximum microsecond precision.",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "to",
            "in": "query",
            "description": "Exclusive end, defaulting once to server current time. Returned in the response.",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "granularity",
            "in": "query",
            "description": "Requires aggregation. Positive 1\u20139999 with s/m/h/d/w/mo. mo is calendar months; other units are fixed UTC durations.",
            "schema": {
              "type": "string",
              "pattern": "^[1-9][0-9]{0,3}(s|m|h|d|w|mo)$"
            },
            "example": "1h"
          },
          {
            "name": "aggregation",
            "in": "query",
            "description": "Requires granularity. avg is sample-weighted, last uses latest observed time with UUID tie-break.",
            "schema": {
              "type": "string",
              "enum": [
                "avg",
                "min",
                "max",
                "sum",
                "last"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Chronological raw observations or nonempty buckets. Bucket timestamps are bucket starts and may precede from for a partial first bucket. No interpolation or zero filling.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "seriesId",
                    "from",
                    "to",
                    "points"
                  ],
                  "properties": {
                    "seriesId": {
                      "type": "string"
                    },
                    "from": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "to": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "granularity": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "aggregation": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "points": {
                      "type": "array",
                      "maxItems": 10000,
                      "items": {
                        "type": "object",
                        "required": [
                          "timestamp",
                          "value",
                          "sampleCount"
                        ],
                        "properties": {
                          "timestamp": {
                            "type": "string",
                            "format": "date-time"
                          },
                          "value": {
                            "type": "number"
                          },
                          "sampleCount": {
                            "type": "integer",
                            "minimum": 1
                          },
                          "eventId": {
                            "type": [
                              "string",
                              "null"
                            ],
                            "format": "uuid",
                            "description": "Present for raw observations; null for aggregates."
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters",
            "content": {
              "application/problem+json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "integer"
                    },
                    "detail": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Unknown series",
            "content": {
              "application/problem+json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "integer"
                    },
                    "detail": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "422": {
            "description": "More than 10000 points; narrow the range or use coarser buckets",
            "content": {
              "application/problem+json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "integer"
                    },
                    "detail": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Query failed, including database errors/timeouts"
          }
        }
      }
    },
    "/api/v1/series/{seriesId}/observations/stream": {
      "get": {
        "operationId": "streamObservations",
        "summary": "Raw snapshot followed by durable observation SSE",
        "description": "Initial snapshot at a committed cursor; replay using Last-Event-ID or cursor query. No to/granularity/aggregation allowed. CapturedAt is not a valid-time cutoff. See contracts/observation-stream.md.",
        "parameters": [
          {
            "in": "path",
            "name": "seriesId",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "in": "query",
            "name": "from",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "in": "header",
            "name": "Last-Event-ID",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]+$"
            }
          },
          {
            "in": "query",
            "name": "cursor",
            "schema": {
              "type": "integer",
              "format": "int64",
              "minimum": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "snapshot event with raw points and watermark, then observation point events with durable IDs; comment heartbeats. Resume emits only observations after the cursor.",
            "content": {
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters or cursor"
          },
          "404": {
            "description": "Unknown series"
          },
          "422": {
            "description": "Initial snapshot exceeds 10000 points"
          },
          "503": {
            "description": "Live connection capacity exceeded"
          }
        }
      }
    }
  }
}
