Skip to content

Commit 1360f83

Browse files
authored
Add aggregators (#35)
* fix: better syntax for sort * feat: upgrade postgrest and allow aggregates * feat: add aggregators * refactor: loop for parameters descriptions * feat: add tests for aggregators * fix: restore previous behaviour * fix: lint * docs: update changelog * docs: update readme * docs: add missing hint types * refactor: remove default __id side sort to allow sort with aggregation * refactor: return 400 if argument could not be parsed, stricter than before * refactor: adapt tests * fix: lint
1 parent f5afe16 commit 1360f83

File tree

8 files changed

+263
-114
lines changed

8 files changed

+263
-114
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Current (in progress)
44

5-
- Nothing yet
5+
- Handle queries with aggregators [#35](https://github.com/datagouv/api-tabular/pull/35)
66

77
## 0.2.1 (2024-11-21)
88

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ curl http://localhost:8005/api/resources/aaaaaaaa-1111-bbbb-2222-cccccccccccc/da
112112
}
113113
```
114114

115-
This endpoint can be queried with the following operators as query string (replacing `column_name` with the name of an actual column):
115+
This endpoint can be queried with the following operators as query string (replacing `column_name` with the name of an actual column), if the column type allows it (see the swagger for each column's allowed parameter):
116116

117117
```
118118
# sort by column
@@ -142,7 +142,26 @@ column_name__strictly_less=value
142142
143143
# strictly greater
144144
column_name__strictly_greater=value
145+
146+
# group by values
147+
column_name__groupby
148+
149+
# count values
150+
column_name__count
151+
152+
# mean / average
153+
column_name__avg
154+
155+
# minimum
156+
column_name__min
157+
158+
# maximum
159+
column_name__max
160+
161+
# sum
162+
column_name__sum
145163
```
164+
> NB : passing an aggregation operator (`count`, `avg`, `min`, `max`, `sum`) returns a column that is named `<column_name>__<operator>` (for instance: `?birth__groupby&score__sum` will return a list of dicts with the keys `birth` and `score__sum`).
146165
147166
For instance:
148167
```shell
@@ -185,6 +204,31 @@ returns
185204
}
186205
```
187206

207+
With filters and aggregators (filtering is always done **before** aggregation, no matter the order in the parameters):
208+
```shell
209+
curl http://localhost:8005/api/resources/aaaaaaaa-1111-bbbb-2222-cccccccccccc/data/?decompte__groupby&birth__less=1996&score__avg
210+
```
211+
i.e. `decompte` and average of `score` for all rows where `birth<="1996"`, grouped by `decompte`, returns
212+
```json
213+
{
214+
"data": [
215+
{
216+
"decompte": 55,
217+
"score__avg": 0.7123333333333334
218+
},
219+
{
220+
"decompte": 27,
221+
"score__avg": 0.6068888888888889
222+
},
223+
{
224+
"decompte": 23,
225+
"score__avg": 0.4603333333333334
226+
},
227+
...
228+
]
229+
}
230+
```
231+
188232
Pagination is made through queries with `page` and `page_size`:
189233
```shell
190234
curl http://localhost:8005/api/resources/aaaaaaaa-1111-bbbb-2222-cccccccccccc/data/?page=2&page_size=30

api_tabular/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ async def resource_data(request):
9696

9797
try:
9898
sql_query = build_sql_query_string(query_string, page_size, offset)
99-
except ValueError:
100-
raise QueryException(400, None, "Invalid query string", "Malformed query")
99+
except ValueError as e:
100+
raise QueryException(400, None, "Invalid query string", f"Malformed query: {e}")
101101

102102
resource = await get_resource(request.app["csession"], resource_id, ["parsing_table"])
103103
response, total = await get_resource_data(request.app["csession"], resource, sql_query)

0 commit comments

Comments
 (0)