Back to Nightingale

TDengine Queries

aiagent/skill/embedded/builtin/query-datasource/datasources/tdengine.md

9.1.13.1 KB
Original Source

TDengine Queries

  • plugin_type: tdengine
  • Query language: SQL (TDengine dialect)
  • Use case: Time-series queries

TDengine has dedicated metadata query endpoints, different from the generic SQL datasources.


Query Database List

POST /api/n9e/tdengine-databases
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "tdengine",
  "datasource_id": 1
}

Query Table List

POST /api/n9e/tdengine-tables
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "tdengine",
  "datasource_id": 1,
  "db": "power",
  "is_stable": false
}
FieldDescription
dbDatabase name
is_stablefalse=regular table, true=super table (stable)

Query Table Columns

POST /api/n9e/tdengine-columns
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "tdengine",
  "datasource_id": 1,
  "db": "power",
  "table": "meters"
}

Run a Time-Series Query

POST /api/n9e/ds-query
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "tdengine",
  "datasource_id": 1,
  "query": [
    {
      "query": "SELECT _wstart AS ts, AVG(current) AS value FROM power.meters WHERE ts >= $from AND ts < $to INTERVAL($interval)",
      "from": "2024-04-01T00:00:00Z",
      "to": "2024-04-02T00:00:00Z",
      "interval": 60,
      "interval_unit": "s",
      "keys": {
        "metricKey": "value",
        "labelKey": "location",
        "timeFormat": ""
      }
    }
  ]
}

Query Parameters

FieldTypeRequiredDescription
querystringYesTDengine SQL query, supports $from, $to, $interval variables
fromstringYesStart time, ISO 8601 format
tostringYesEnd time, ISO 8601 format
intervalintNoSampling interval value
interval_unitstringNoInterval unit: s (seconds), m (minutes), h (hours)
keys.metricKeystringNoNumeric column name
keys.labelKeystringNoLabel/grouping column name
keys.timeFormatstringNoTime format

Time Variables

VariableDescription
$fromStart time
$toEnd time
$intervalSampling interval, e.g. 60s

Common SQL Examples

RequirementSQL
Aggregate average by intervalSELECT _wstart AS ts, AVG(current) AS value FROM power.meters WHERE ts >= $from AND ts < $to INTERVAL($interval)
Group by labelSELECT _wstart AS ts, location, AVG(current) AS value FROM power.meters WHERE ts >= $from AND ts < $to PARTITION BY location INTERVAL($interval)
Latest valueSELECT LAST(current) AS value, location FROM power.meters GROUP BY location

Considerations

  • Dedicated API: Metadata queries (databases/tables/columns) use the dedicated /tdengine-* endpoints, not the generic /db-* endpoints
  • Super table: Set is_stable: true when querying a stable (super table)
  • INTERVAL: TDengine's INTERVAL() clause is used for time-window aggregation; combine it with _wstart to get the window start time