Skip to content

Commit c8585de

Browse files
docs: describe customization options for writers
1 parent c501b50 commit c8585de

File tree

4 files changed

+292
-12
lines changed

4 files changed

+292
-12
lines changed

docs/usage/core.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Overview
2+
3+
[![PyPI](https://img.shields.io/pypi/v/garf-core?logo=pypi&logoColor=white&style=flat-square)](https://pypi.org/project/garf-core)
4+
[![Downloads PyPI](https://img.shields.io/pypi/dw/garf-core?logo=pypi)](https://pypi.org/project/garf-core/)
5+
6+
`garf-core` contains the base abstractions are used by an implementation for a concrete reporting API.
7+
8+
These abstractions are designed to be as modular and simple as possible:
9+
10+
* [`BaseApiClient`](api-client.md) - an interface for connecting to APIs.
11+
* [`BaseParser`](parsers.md) - an interface to parse results from the API.
12+
* [`ApiReportFetcher`](fetcher.md) - responsible for fetching and parsing data from reporting API.
13+
* [`GarfReport`](reports.md) - contains data from API in a format that is easy to write and interact with.
14+
* `QuerySpecification` - parsed SQL-query into various elements.
15+
* [`BaseQuery`](queries.md#queries-as-python-objects) - protocol for all class based queries.
16+
17+
## Installation
18+
19+
/// tab | pip
20+
```bash
21+
pip install garf-core
22+
```
23+
///
24+
25+
/// tab | uv
26+
```bash
27+
uv add garf-core
28+
```
29+
///

docs/usage/fetcher.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ApiReportFetcher
22

3+
ApiReportFetcher is reponsible for getting report from an API based on provided query.
4+
35
## Initialization
46

57
### Api Client

docs/usage/writers.md

Lines changed: 254 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Write GarfReport
1+
# Writing GarfReport
22

3-
`garf-io` handles reading queries and writing `GarfReport` to various local/remote storages.
3+
[![PyPI](https://img.shields.io/pypi/v/garf-io?logo=pypi&logoColor=white&style=flat-square)](https://pypi.org/project/garf-io)
4+
[![Downloads PyPI](https://img.shields.io/pypi/dw/garf-io?logo=pypi)](https://pypi.org/project/garf-io/)
5+
6+
`garf-io` library is reponsible for writing [`GarfReport`](reports.md) to various local/remote storages.
47

5-
Currently it supports writing data to the following destination:
68

79
| CLI identifier | Writer Class | Options |
810
|------------| ---------------- | -------- |
@@ -13,10 +15,6 @@ Currently it supports writing data to the following destination:
1315
| `sqldb` | SqlAlchemyWriter | `connection-string`, `if-exists=fail|replace|append` |
1416
| `sheets` | SheetsWriter | `share-with`, `credentials-file`, `spreadsheet-url`, `is_append=True|False`|
1517

16-
Each of writer also support two options for dealing with arrays:
17-
18-
* `WRITER.array-handling` - arrays handling method: "strings" (default) - store arrays as strings (items combined via a separator, e.g. "item1|item2"), "arrays" - store arrays as arrays.
19-
* `WRITER.array-separator` - a separator symbol for joining arrays as strings, by default '|'.
2018

2119
## Installation
2220

@@ -97,6 +95,60 @@ writer.write(sample_report, 'query')
9795
```
9896
///
9997

98+
#### Format
99+
100+
For `console` writer you can specify the output format:
101+
102+
* `table` - rich table (default).
103+
* `json` - JSON.
104+
* `jsonl` - JSON lines
105+
106+
/// tab | cli
107+
```bash hl_lines="3"
108+
garf query.sql --source API_SOURCE \
109+
--output console \
110+
--console.format=json
111+
```
112+
///
113+
114+
/// tab | python
115+
```python hl_lines="7"
116+
from garf_core import report
117+
from garf_io.writers import console_writer
118+
119+
# Create example report
120+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
121+
122+
writer = console_writer.ConsoleWriter(format='json')
123+
writer.write(sample_report, 'query')
124+
```
125+
///
126+
127+
#### Page size
128+
129+
If you're using `console` writer with `table` format option, you can specify
130+
`page_size` parameter to print N rows to the console.
131+
132+
/// tab | cli
133+
```bash hl_lines="3"
134+
garf query.sql --source API_SOURCE \
135+
--output console \
136+
--console.page-size=100
137+
```
138+
///
139+
140+
/// tab | python
141+
```python hl_lines="7"
142+
from garf_core import report
143+
from garf_io.writers import console_writer
144+
145+
# Create example report
146+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
147+
148+
writer = console_writer.ConsoleWriter(page_size=100)
149+
writer.write(sample_report, 'query')
150+
```
151+
///
100152

101153
### CSV
102154

@@ -122,6 +174,34 @@ writer.write(sample_report, 'query')
122174
```
123175
///
124176

177+
#### Destination folder
178+
179+
For `csv` writer you can specify the local or remote folder to store results.
180+
I.e. if you want to write results to Google Cloud Storage bucket `gs://PROJECT_ID/bucket`,
181+
you need to provide `destination_folder` parameter.
182+
183+
184+
/// tab | cli
185+
```bash hl_lines="3"
186+
garf query.sql --source API_SOURCE \
187+
--output csv \
188+
--csv.destination-folder=gs://PROJECT_ID/bucket
189+
```
190+
///
191+
192+
/// tab | python
193+
```python
194+
from garf_core import report
195+
from garf_io.writers import csv_writer
196+
197+
# Create example report
198+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
199+
200+
writer = csv_writer.CsvWriter(destination_folder='gs://PROJECT_ID/bucket/')
201+
writer.write(sample_report, 'query')
202+
```
203+
///
204+
125205
### JSON
126206

127207
`json` writer allows you to save `GarfReport` as JSON or JSONL file to local or remote storage.
@@ -146,6 +226,62 @@ writer.write(sample_report, 'query')
146226
```
147227
///
148228

229+
#### Destination folder
230+
231+
You can specify the local or remote folder to store results.
232+
I.e. if you want to write results to Google Cloud Storage bucket `gs://PROJECT_ID/bucket`,
233+
you need to provide `destination_folder` parameter.
234+
235+
236+
/// tab | cli
237+
```bash hl_lines="3"
238+
garf query.sql --source API_SOURCE \
239+
--output json \
240+
--json.destination-folder=gs://PROJECT_ID/bucket
241+
```
242+
///
243+
244+
/// tab | python
245+
```python
246+
from garf_core import report
247+
from garf_io.writers import json_writer
248+
249+
# Create example report
250+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
251+
252+
writer = json_writer.JsonWriter(destination_folder='gs://PROJECT_ID/bucket/')
253+
writer.write(sample_report, 'query')
254+
```
255+
///
256+
257+
#### Format
258+
259+
You can specify the output format:
260+
261+
* `json` - JSON (default)
262+
* `jsonl` - JSON lines
263+
264+
/// tab | cli
265+
```bash hl_lines="3"
266+
garf query.sql --source API_SOURCE \
267+
--output json \
268+
--json.format=jsonl
269+
```
270+
///
271+
272+
/// tab | python
273+
```python hl_lines="7"
274+
from garf_core import report
275+
from garf_io.writers import json_writer
276+
277+
# Create example report
278+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
279+
280+
writer = json_writer.JsonWriter(format='jsonl')
281+
writer.write(sample_report, 'query')
282+
```
283+
///
284+
149285
### BigQuery
150286

151287
!!! important
@@ -178,6 +314,110 @@ writer.write(sample_report, 'query')
178314
```
179315
///
180316

317+
#### Project
318+
319+
By default reports are saved to `GOOGLE_CLOUD_PROJECT`.
320+
You can overwrite it with `project` parameter.
321+
322+
/// tab | cli
323+
```bash hl_lines="3"
324+
garf query.sql --source API_SOURCE \
325+
--output bq \
326+
--bq.project=PROJECT_ID
327+
```
328+
///
329+
330+
/// tab | python
331+
```python hl_lines="7"
332+
from garf_core import report
333+
from garf_io.writers import bigquery_writer
334+
335+
# Create example report
336+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
337+
338+
writer = bigquery_writer.BigQueryWriter(project="PROJECT_ID")
339+
writer.write(sample_report, 'query')
340+
```
341+
///
342+
343+
#### Dataset
344+
345+
By default reports are saved to `garf` dataset.
346+
You can overwrite it with `dataset` parameter.
347+
348+
/// tab | cli
349+
```bash hl_lines="3"
350+
garf query.sql --source API_SOURCE \
351+
--output bq \
352+
--bq.dataset=DATASET
353+
```
354+
///
355+
356+
/// tab | python
357+
```python hl_lines="7"
358+
from garf_core import report
359+
from garf_io.writers import bigquery_writer
360+
361+
# Create example report
362+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
363+
364+
writer = bigquery_writer.BigQueryWriter(dataset="DATASET")
365+
writer.write(sample_report, 'query')
366+
```
367+
///
368+
369+
#### Location
370+
371+
By default reports are saved to `US` location.
372+
You can overwrite it with `location` parameter.
373+
374+
/// tab | cli
375+
```bash hl_lines="3"
376+
garf query.sql --source API_SOURCE \
377+
--output bq \
378+
--bq.location=LOCATION
379+
```
380+
///
381+
382+
/// tab | python
383+
```python hl_lines="7"
384+
from garf_core import report
385+
from garf_io.writers import bigquery_writer
386+
387+
# Create example report
388+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
389+
390+
writer = bigquery_writer.BigQueryWriter(location="LOCATION")
391+
writer.write(sample_report, 'query')
392+
```
393+
///
394+
395+
#### Write disposition
396+
397+
By default reports overwrite any existing data.
398+
You can overwrite it with [`write_disposition`](https://cloud.google.com/bigquery/docs/reference/auditlogs/rest/Shared.Types/BigQueryAuditMetadata.WriteDisposition) parameter.
399+
400+
/// tab | cli
401+
```bash hl_lines="3"
402+
garf query.sql --source API_SOURCE \
403+
--output bq \
404+
--bq.write_disposition=DISPOSITION
405+
```
406+
///
407+
408+
/// tab | python
409+
```python hl_lines="7"
410+
from garf_core import report
411+
from garf_io.writers import bigquery_writer
412+
413+
# Create example report
414+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
415+
416+
writer = bigquery_writer.BigQueryWriter(write_disposition="DISPOSITION")
417+
writer.write(sample_report, 'query')
418+
```
419+
///
420+
181421
### Google Sheets
182422

183423
!!! important
@@ -243,3 +483,10 @@ writer = sqldb_writer.SqlAlchemyWriter(
243483
writer.write(sample_report, 'query')
244484
```
245485
///
486+
487+
## Configuration
488+
489+
Each of writer also support two options for dealing with arrays:
490+
491+
* `WRITER.array-handling` - arrays handling method: "strings" (default) - store arrays as strings (items combined via a separator, e.g. "item1|item2"), "arrays" - store arrays as arrays.
492+
* `WRITER.array-separator` - a separator symbol for joining arrays as strings, by default '|'.

mkdocs.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ nav:
6060
- CLI: get-started/cli.md
6161
- Server: get-started/server.md
6262
- Usage:
63-
- Queries: usage/queries.md
64-
- API Clients: usage/api-client.md
65-
- Fetchers: usage/fetcher.md
66-
- Parsers: usage/parsers.md
67-
- Report: usage/reports.md
63+
- Core:
64+
- usage/core.md
65+
- Queries: usage/queries.md
66+
- API Clients: usage/api-client.md
67+
- Fetchers: usage/fetcher.md
68+
- Parsers: usage/parsers.md
69+
- Report: usage/reports.md
6870
- Writers: usage/writers.md
6971
- Executors:
7072
- usage/executors.md

0 commit comments

Comments
 (0)