# Features GraphQL API

import DocButton from '~/components/webkit/DocButton.vue';
import Tag from '~/components/webkit/Tag.vue'

**GraphQL** features consist of **datasets**, **filtering**, **sorting**, and **pagination**. The features provide easy access to your data, and using and combining the available features creates more personalized and specific queries to request the exact information you need.

The next sections will detail each available feature of GraphQL and how to use them.

---

## Datasets

Azion **GraphQL API** uses defined datasets to indicate what requests you can run through queries and fetches data from **Real-Time Metrics** and **Real-Time Events**. They consist of organized tables informing your data.

Find each available dataset and what they request next.

:::note
Seven datasets are deprecated and were replaced by other datsets:

- **cellsConsoleEvents** for **functionConsoleEvents**
- **edgeFunctionEvents** for **functionEvents**
- **httpEvents** for **workloadEvents**
- **idnsQueriesMetrics** for **edgeDnsQueriesMetrics**
- **idnsQueriesEvents** for **edgeDnsQueriesEvents**
- **l2CacheMetrics** for **tieredCacheMetrics**
- **l2CacheEvents** for **tieredCacheEvents**
:::

### Real-Time Metrics GraphQL API

| Dataset | Description |
| ------- | ----------- |
| dataStreamedMetrics | Sent events of data by [Data Stream](/en/documentation/products/observe/data-stream/) to the clients' endpoint |
| edgeFunctionsMetrics | Events executed by [Functions](/en/documentation/products/build/applications/functions/) |
| httpMetrics | Request events registered by [Applications](/en/documentation/products/build/applications/) and [Firewall](/en/documentation/products/secure/firewall/) |
| httpBreakdownMetrics | Query real-time, detailed, aggregated data on [HTTP request events blocked](/en/documentation/products/guides/query-httpbreakdownmetrics-data-with-graphql/) |
| edgeDnsQueriesMetrics | Query events performed on [Edge DNS](/en/documentation/products/secure/edge-dns/) |
| imagesProcessedMetrics | Image processing events by [Image Processor](/en/documentation/products/build/applications/image-processor/) |
| tieredCacheMetrics | Request events registered by [Tiered Cache](/en/documentation/products/build/applications/cache/tiered-cache/) |
| connectedUsersMetrics | Query the number of users connected to your applications, using [Live Ingest](/en/documentation/products/guides/query-connected-users-data-with-graphql/) |
| botManagerMetrics | Query the number of requests evaluated and the actions performed by [Azion Bot Manager](/en/documentation/products/guides/query-bot-manager-data-with-graphql/) (if subscribed) |
| botManagerBreakdownMetrics | Query the URLs most impacted by bad bots, according to [Azion Bot Manager](/en/documentation/products/guides/query-bot-manager-breakdown-data-with-graphql/) data (if subscribed) |

<DocButton href="/en/documentation/devtools/graphql-api/features/gql-real-time-metrics-fields/" label="Go to Real-Time Metrics GraphQL API fields descriptions" kind="secondary" size="medium" />

### Real-Time Events GraphQL API

| Dataset | Description |
| ------- | ----------- |
| activityHistoryEvents | Events logs from an account on Azion Console regarding activities registered on [Activity History](/en/documentation/products/accounts/activity-history/). It stores data for *2 years* and exhibits data starting from *September 22nd, 2023*. |
| functionConsoleEvents |  Events logs from applications using [Azion Runtime](/en/documentation/runtime/overview/) returned by the Cells Console. |
| dataStreamedEvents | Sent events of data by [Data Stream](/en/documentation/products/observe/data-stream/) to the clients' endpoint. |
| functionEvents | Events executed by [Functions](/en/documentation/products/build/applications/functions/). |
| workloadEvents | Request events registered by [Applications](/en/documentation/products/build/applications/) and [Firewall](/en/documentation/products/secure/firewall/). |
| edgeDnsQueriesEvents | Query events performed on [Edge DNS](/en/documentation/products/secure/edge-dns/). |
| imagesProcessedEvents |  Image processing events by [Image Processor](/en/documentation/products/build/applications/image-processor/). |
| tieredCacheEvents | Request events registered by [Tiered Cache](/en/documentation/products/build/applications/cache/tiered-cache/). |
| telemetryDeviceInfoEvents | Events related to the device being used for access and recorded by **Azion Mobile SDK**, both at the hardware and software levels. |
| telemetrySensorsEvents | Events related to the device's sensors recorded by **Azion Mobile SDK**, such as touchscreen interactions and gyroscope information. |

<DocButton href="/en/documentation/devtools/graphql-api/features/gql-real-time-events-fields/" label="Go to Real-Time Events GraphQL API fields descriptions" kind="secondary" size="medium" />

### Billing GraphQL API

:::note
The Billing GraphQL API is only available for *Online Sales* accounts.
:::

| Dataset | Description |
| ------- | ----------- |
| balanceFinancialEntry | Logs regarding types of financial entries and monetary values |
| paymentsClientDebt | Logs regarding client's debts and payments and monetary values |
| billDetail | Logs regarding detailed billing data per product, metric, and region for each billing period |

<DocButton href="/en/documentation/devtools/graphql-api/features/gql-billing-fields/" label="Go to Billing GraphQL API fields descriptions" kind="secondary" size="medium" />

### Accounting GraphQL API

| Dataset | Description |
| ------- | ----------- |
| accountingDetail | Logs regarding accounted data for products and its metrics |

<DocButton href="/en/documentation/devtools/graphql-api/features/gql-accounting-fields/" label="Go to Accounting GraphQL API fields descriptions" kind="secondary" size="medium" />

### Consumption GraphQL API

| Dataset | Description |
| ------- | ----------- |
| `workloadConsumptionMetrics` | Logs regarding accounted data for products usage and their metrics. |

<DocButton href="/en/documentation/devtools/graphql-api/features/gql-consumption-fields/" label="Go to Consumption GraphQL API fields descriptions" kind="secondary" size="medium" />

---

:::tip
You can also run an **Introspection Query** to consult metadata on the Metrics and Events datasets. Metadata provide what each dataset of the GraphQL APIs supports and specific information: name, description, and format. Find out more on the [How to query metadata with GraphQL API](/en/documentation/products/guides/graphql-metadata/) guide.
:::

You can request both the *raw* model, using the **Events** datasets, and the *aggregated* model, using the **Metrics** datasets.

<DocButton href="/en/documentation/devtools/graphql-api/queries/#raw-data" label="go to raw queries reference" kind="secondary" size="medium" /> <br></br>

<DocButton href="/en/documentation/devtools/graphql-api/queries/#aggregated-data" label="go to aggregated queries reference" kind="secondary" size="medium" /> <br></br>

<DocButton href="/en/documentation/products/guides/graphql-aggregated-data/" label="go to How to query aggregated data guide" kind="secondary" size="medium" />

---

## Filtering

With filtering parameters, responses returned by your queries can be more accurate to your set of data. You can use filtering with any available field in the dataset you're consulting.

As requesting complex or a large amount of data can cause responses to get noisy and complicate its use, filtering queries helps with getting exact and direct data from your requests. For example, if you're using the following query to request `httpMetrics`:

```graphql
query HttpQuery {
  httpMetrics(
    limit: 10
    filter: {
      tsRange: {begin:"2022-10-20T10:10:10", end:"2022-10-23T10:10:10"}
    }
  ) 
  {	
    ts
    geolocCountryName
  }
}
```

You can filter the query by requesting data specific to the `geolocCountryName` field:

```graphql
query HttpQuery {
  httpMetrics(
    limit: 10
    filter: {
      tsRange: {begin:"2022-10-20T10:10:10", end:"2022-10-23T10:10:10"}
      geolocCountryName: "Brazil"
    }
  ) 
  {	
    ts
    geolocCountryName
  }
}
```

You can feel free to update your request to use a field of your interest within the fields of the dataset you're consulting.

You can also filter using multiple `AND` and `OR` parameters. Make sure you define all the fields you're filtering for inside the parameter, like in the following example:

```graphql
query totalImagesProcessedRequests {
  imagesProcessedMetrics(
    aggregate: {sum: requests}
    limit: 100
    filter: {
      tsRange: {begin:"2023-03-20T19:52:00", end:"2023-03-20T20:52:00"}
      or: {
          status:304
          statusRange: {begin: 200, end: 299}
      }
    }
    groupBy:[ts]
    orderBy:[ts_ASC]
  )
  {
    ts
    sum
  }
}
```

---

## Sorting 

The sorting feature lets you organize and sort the received data of a dataset according to the event's order. For example, if you're receiving the `host` field data as a response to your API request, you can sort the data to receive it in:

- An *ascending order (ASC)*
- A *descending order (DESC)*

Whenever you use the `orderBy` property, you must add either the `ASC` or the `DESC` specification.

For example, to use the ascending order sorting feature, you need to add `orderBy` in your query and the field you want to sort + `ASC`:

```graphql
{
    orderBy: [host_ASC]
}
```

To sort the data according to a *descending order (DESC)*, you need to add the field you want to sort + `DESC`:

```graphql
{
    orderBy: [ts_DESC]
}
```

Say you're using this query with `DESC`:

```graphql
query SumBytesSentByHost {
  httpMetrics(
    limit: 1000
    filter: {
      tsRange: {begin:"2023-01-01T17:03:00", end:"2023-06-01T18:05:00"}
    }
    aggregate: {sum: bytesSent}
    groupBy: [host]
    orderBy: [sum_DESC]
  ) 
  {        
    host
    sum
  }
}
```

You'll get a response similar to this:

```graphql
{
    "data": {
        "httpMetrics": [
            {
                "host": "g1sdetynmxe0ao.map.azionedge.net",
                "sum": 606226
            },
            {
                "host": "uaykhefjdk9or.map.azionedge.net",
                "sum": 583059
            },
            {
                "host": "wz0ywpod397zk.map.azionedge.net",
                "sum": 567633
            },
            {
                "host": "zi1435nbhec7.map.azionedge.net",
                "sum": 96002
            }
        ]
    }
}
```

And if you're using this query with `ASC`:

```graphql
query AvgRequesTimeByHost {
  httpMetrics(
    limit: 1000
    filter: {
      tsRange: {begin:"2023-01-01T17:03:00", end:"2023-06-01T18:05:00"}
    }
    aggregate: {avg:requestTime}
    groupBy: [ts, host]
    orderBy: [avg_ASC]
  ) 
  {        
    ts
    host
    avg
  }
}
```

You'll get a response similar to this:

```graphql
{
    "data": {
        "httpMetrics": [
            {
                "ts": "2023-04-21T00:00:00Z",
                "host": "zipo145nbhc7.map.azionedge.net",
                "avg": 0.04871428571428572
            },
            {
                "ts": "2023-04-13T00:00:00Z",
                "host": "g1snmxepa0ao.map.azionedge.net",
                "avg": 0.561
            },
            {
                "ts": "2023-04-11T00:00:00Z",
                "host": "g1syinmxe2ao.map.azionedge.net",
                "avg": 4.101833333333333
            },
            {
                "ts": "2023-04-11T00:00:00Z",
                "host": "wz1yd307zk.map.azionedge.net",
                "avg": 8.705666666666668
            },
            {
                "ts": "2023-05-22T00:00:00Z",
                "host": "uaifjdk6or.map.azionedge.net",
                "avg": 31.493818181818185
            }
        ]
    }
}
```

---

## Pagination

**Pagination** is a feature designed to help you decide where you want your results to begin from and how many results you want to see. Currently, Azion **GraphQL API** uses *offset and limit pagination* to provide the feature.

Using pagination can be useful when you get a large number of data in response to your API request. You can use the feature by setting **offsets** and **limits**. This way, the API knows it needs to return data within the specific range you've set.

The **offsets** parameter sets the number of records you want to skip in your data response, and the **limits** parameter sets the number of results you want to receive.

:::note
Setting offset and limit parameters isn't mandatory. In case you don't set the parameters, GraphQL API automatically sets the **offset** for **0** and the **limit** for **10**.
:::

See the following example on how to set offset and limit parameters:

```graphql
query HttpQuery {
  httpMetrics(
    offset: 15
    limit: 30
    filter: {
      tsRange: {begin:"2022-10-20T10:10:10", end:"2022-10-23T10:10:10"}
    }
  ) 
  {	
    ts
    requestMethod
  }
}
```

The offset is set for 15, meaning your response will start with the 16th result, and the limit is set for 30, meaning your response will give you a total of 30 results. In this case, you'll receive a response from the 16th result until the 45th result.

:::note
If your data is constantly updated, using pagination may cause missing or duplicate data when multiple requests are run using this feature.
:::

---

## Resampling

Available for: <Tag severity="info">Real-Time Metrics GraphQL API</Tag>

The **Resample** feature allows you to resample your data to display a smaller amount of data points being exhibited on charts. It works according to the rule you pass in the `function` field.

You can use it to complement the limit you add to your query, as the default data points in charts may not be the ideal number for your analysis.

:::note
If an extrapolation of data occurs, the API's backend will overwrite the value you passed and run the closests calculation possible according to the available data.
:::

The query *must contain* the `function` field and its properties as well as have the `groupBy` field containing the `ts` variable.

By specifying `sum`, you'll receive a total sum of the data points in that interval. By specifying `mean`, you'll receive a calculation of *sum + division*.

See an example:

```graphql
query httpRequestCount {
  httpMetrics(
    limit: 10000,
    resample: {
      function: sum
      points: 100
    }
    filter: {
      tsRange: {
        begin:"2023-08-16T20:17:00", end:"2023-09-17T21:16:00"
      }
    },
    groupBy: [ts]
    orderBy: [ts_ASC]
  ) 
  {        
    ts
   	dataTransferredIn
  	dataTransferredOut
    dataTransferredTotal
  }
}
```

Say your original query result, without resampling, returned *1,456 logs*. Since you queried for a resample of *100 points*, the calculation the API does is `1,456 // 100 = 14,56 minutes interval`. Since the API only uses integers (whole numbers), it'll round up the result to an interval of 14 minutes between each point returned.

However, the API doesn't discard the remaining `0,56 minutes`. It keeps adding them until the number becomes an integer and them sums it to the original requested points.

In the example used, it returned the original `100` requested points plus the reamaining `4` points, resulting in a total of `104 points` for the resampled query.

:::note
If you use a query with resampling, but your data already returns an amount of points that are accessible to charts, such as `30 points`, the API will ignore the resampling request and return all points.
:::