> For the complete documentation index, see [llms.txt](https://spaceone-dev.gitbook.io/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://spaceone-dev.gitbook.io/api-reference/statistics-v1/untitled.md).

# Stat API

## Statistic query format

| NO | KEY                | TYPE   |       REQUIRED       | DESC                                         |
| -- | ------------------ | ------ | :------------------: | -------------------------------------------- |
| 1  | **resource\_type** | string | :white\_check\_mark: | Main resource type to select with grouping   |
| 2  | **query**          | dict   | :white\_check\_mark: | Aggregate for grouping and its specification |
| 3  | **join**           | list   |          :x:         | Joining resource which implements            |
| 4  | **formulas**       | list   |          :x:         | calculation for formula which stands         |

| Full specification of Stat query |
| -------------------------------- |

{% tabs %}
{% tab title="SKELETON" %}

```javascript
{
    "resource_type": ...,
    "query": {...},
    "join": [...],
    "formulas": [...]
}
```

{% endtab %}

{% tab title="query" %}

```javascript
{
    "resource_type": "identity.ServiceAccount",
    "query": {
        "aggregate": {
            "group": {
                "keys": [
                    {
                        "key": "provider",
                        "name": "provider"
                    },
                    {
                        "key": "service_account_id",
                        "name": "service_account_id"
                    },
                    {
                        "key": "name",
                        "name": "service_account_name"
                    }
                ]
            }
        },
        "sort": {
            "name": "resource_count",
            "desc": true
        },
        "limit": 5
    } 
}
```

### q**uery** specification : *(type: dict)*

| NO | KEY                | TYPE   |       REQUIRED       | DESC                                          |
| -- | ------------------ | ------ | :------------------: | --------------------------------------------- |
| 1  | **resource\_type** | string | :white\_check\_mark: | Main resource type to select with grouping    |
| 2  | **query**          | dict   | :white\_check\_mark: | query for grouping statistic main select item |

{% hint style="info" %}
**resource\_type :** "<**service**>.<**resource**>"\
&#x20;:point\_right: identity.Project\
&#x20;:point\_right: inventory.Server
{% endhint %}

{% hint style="info" %}
**query:**&#x20;

refer to [statistic query specification](/api-reference/common-v1/statistics-query.md#basic-statistic-query-format)
{% endhint %}
{% endtab %}

{% tab title="join" %}

```javascript
{
    "resource_type": ...,
    "query": ...,
    "join": [
        {
            "keys": [
                "service_account_id"   
            ],
            "resource_type": "inventory.Server",
            "query": {
                "aggregate": {
                    "unwind": [
                        {
                            "path": "collection_info.service_accounts"
                        }
                    ],
                    "group": {
                        "keys": [
                            {
                                "key": "service_account_id",
                                "name": "service_account_id"
                            }
                        ],
                        "fields": [
                            {
                                "operator": "count",
                                "name": "server_count"
                            }
                        ]
                    }
                }
            }
        },
        {
            "keys": [
                "service_account_id"
            ],
            "type": "RIGHT",
            "resource_type": "inventory.CloudService",
            "query": {
                "aggregate": {
                    "unwind": [
                        {
                            "path": "collection_info.service_accounts"
                        }
                    ],
                    "group": {
                        "keys": [
                            {
                                "key": "service_account_id",
                                "name": "service_account_id"
                            }
                        ],
                        "fields": [
                            {
                                "operator": "count",
                                "name": "cloud_service_count"
                            }
                        ]
                    }
                }
            }
        },
        {
            "keys": [
                "service_account_id"
            ],
            "type": "RIGHT",
            "resource_type": "secret.Secret",
            "query": {
                "aggregate": {
                    "group": {
                        "keys": [
                            {
                                "key": "service_account_id",
                                "name": "service_account_id"
                            }
                        ],
                        "fields": [
                            {
                                "operator": "count",
                                "name": "secret_count"
                            }
                        ]
                    }
                }
            }
        }
    ],
    "formulas": ...
}
```

{% hint style="warning" %}
**Note:**\
\&#xNAN;***join***  has a dependencies on main resource\_type, and query\
must pair with resource\_type & query
{% endhint %}

## **join** specification *( type: list )*

| NO | KEY                | TYPE   |       REQUIRED       | DESC                                                                                                              |
| -- | ------------------ | ------ | :------------------: | ----------------------------------------------------------------------------------------------------------------- |
| 1  | **keys**           | list   |          :x:         | Primary columns or keys to join with main resource of query and index join will be performed if keys hasn't given |
| 2  | **type**           | string |          :x:         | <p>Type of join in SQL </p><ul><li>LEFT   (default) </li><li>RIGHT </li><li>INNER</li><li>OUTER </li></ul>        |
| 3  | **resource\_type** | string | :white\_check\_mark: | Resource type to join with                                                                                        |
| 4  | **query**          | dict   | :white\_check\_mark: | Select query for joining with primary resources                                                                   |

{% hint style="info" %}
**join\_key:**&#x20;

refer to below  images for each type's concept
{% endhint %}

![](/files/-M6czShfrKXydy6OQsGm)

{% hint style="info" %}
**query:**&#x20;

refer to [statistic query specification](/api-reference/common-v1/statistics-query.md#basic-statistic-query-format)  &#x20;
{% endhint %}
{% endtab %}

{% tab title="formulas" %}

```javascript
{
    "resource_type": ...,
    "query": ...,
    "join": ...,
    "formulas": [
        {
            "name": "resource_count",
            "formula": "server_count + cloud_service_count"
        }
    ]
}
```

{% hint style="warning" %}
**Note:**\
\&#xNAN;***formulas***  has a dependencies on main resource\_type, and query\
must pair with resource\_type & query and join(if needed)
{% endhint %}

### **formula** specification : *( type: list )*

| NO | KEY      | TYPE   |       REQUIRED       | DESC                                           |
| -- | -------- | ------ | :------------------: | ---------------------------------------------- |
| 1  | name     | string | :white\_check\_mark: | name of variable that stores performed formula |
| 2  | formula  | string | :white\_check\_mark: | algebraic formulas to calculate given form     |
| 3  | operator | string |          :x:         | optional values in EVAL(default), QUERY        |

### formula use case

```
{
    "resource_type": ...,
    "query": ...,
    "join": ...,
    "formulas": [
        {
            "name": "total",
            "formula": "server_count + cloud_service_count"
        },
        { 
            "formula": "total > 0",
            "operator": "QUERY"
        }
    ]
}
```

{% hint style="info" %}
**Operator** is **OPTIONAL;** \
Value **EVAL**(default) will be automatically set if there's no operator has not specifically declared in formula parameters.

Value **Query** has to be set to perform selective result as above within formulas.

Refers to\
\- [pandas.eval](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.eval.html)\
\- [pandas.DataFrame.query](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html)
{% endhint %}
{% endtab %}
{% endtabs %}
