Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions website/pages/en/subgraphs/developing/creating/advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ dataSources: ...

## Timeseries and Aggregations

Timeseries and aggregations enable your subgraph to track statistics like daily average price, hourly total transfers, etc.
Prerequisites:

This feature introduces two new types of subgraph entity. Timeseries entities record data points with timestamps. Aggregation entities perform pre-declared calculations on the Timeseries data points on an hourly or daily basis, then store the results for easy access via GraphQL.
- Subgraph specVersion must be ≥1.1.0.

Timeseries and aggregations enable your subgraph to track statistics like daily average price, hourly total transfers, and more.

This feature introduces two new types of subgraph entity. Timeseries entities record data points with timestamps. Aggregation entities perform pre-declared calculations on the timeseries data points on an hourly or daily basis, then store the results for easy access via GraphQL.

### Example Schema

Expand All @@ -49,11 +53,19 @@ type Stats @aggregation(intervals: ["hour", "day"], source: "Data") {
}
```

### Defining Timeseries and Aggregations
### How to Define Timeseries and Aggregations

Timeseries entities are defined with `@entity(timeseries: true)` in the GraphQL schema. Every timeseries entity must:

- have a unique ID of the int8 type
- have a timestamp of the Timestamp type
- include data that will be used for calculation by aggregation entities.

Timeseries entities are defined with `@entity(timeseries: true)` in schema.graphql. Every timeseries entity must have a unique ID of the int8 type, a timestamp of the Timestamp type, and include data that will be used for calculation by aggregation entities. These Timeseries entities can be saved in regular trigger handlers, and act as the “raw data” for the Aggregation entities.
These Timeseries entities can be saved in regular trigger handlers, and act as the “raw data” for the aggregation entities.

Aggregation entities are defined with `@aggregation` in schema.graphql. Every aggregation entity defines the source from which it will gather data (which must be a Timeseries entity), sets the intervals (e.g., hour, day), and specifies the aggregation function it will use (e.g., sum, count, min, max, first, last). Aggregation entities are automatically calculated on the basis of the specified source at the end of the required interval.
Aggregation entities are defined with `@aggregation` in the GraphQL schema. Every aggregation entity defines the source from which it will gather data (which must be a timeseries entity), sets the intervals (e.g., hour, day), and specifies the aggregation function it will use (e.g., sum, count, min, max, first, last).

Aggregation entities are automatically calculated on the basis of the specified source at the end of the required interval.

#### Available Aggregation Intervals

Expand Down Expand Up @@ -81,10 +93,6 @@ Aggregation entities are defined with `@aggregation` in schema.graphql. Every ag
}
```

Note:

To use Timeseries and Aggregations, a subgraph must have a spec version ≥1.1.0. Note that this feature might undergo significant changes that could affect backward compatibility.

[Read more](https://github.com/graphprotocol/graph-node/blob/master/docs/aggregations.md) about Timeseries and Aggregations.

## Non-fatal errors
Expand Down