Metadata-Version: 2.4
Name: qaechi-sink
Version: 1.0.0
Summary: Load finished Qaechi jobs, values and evidence into PostgreSQL, Supabase, MySQL, MongoDB or a webhook.
Author: Taeyong Kang
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/qaechi/qaechi-sink
Project-URL: Documentation, https://qaechi.github.io/modules/
Project-URL: Issues, https://github.com/qaechi/qaechi-sink/issues
Project-URL: Changelog, https://github.com/qaechi/qaechi-sink/blob/main/CHANGELOG.md
Keywords: qaechi,kafka,sink,postgresql,supabase,mysql,mongodb,webhook
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: qaechi-contracts<2,>=1
Requires-Dist: httpx<1,>=0.29
Provides-Extra: kafka
Requires-Dist: confluent-kafka<3,>=2.16.2; extra == "kafka"
Provides-Extra: postgres
Requires-Dist: psycopg[binary]<4,>=3.3.9; extra == "postgres"
Provides-Extra: mysql
Requires-Dist: pymysql<2,>=1.2.4; extra == "mysql"
Provides-Extra: mongodb
Requires-Dist: pymongo<5,>=4.20.2; extra == "mongodb"
Requires-Dist: dnspython<3,>=2.9.2; extra == "mongodb"
Provides-Extra: all
Requires-Dist: qaechi-sink[kafka,mongodb,mysql,postgres]; extra == "all"
Provides-Extra: dev
Requires-Dist: qaechi-sink[all]; extra == "dev"
Requires-Dist: pytest<10,>=8; extra == "dev"
Requires-Dist: ruff==0.16.*; extra == "dev"
Dynamic: license-file

# qaechi-sink

Load finished [Qaechi](https://qaechi.github.io/) jobs into the database you already run. The sink
consumes result events from Kafka, fetches the values with their evidence from the parser, and
writes them idempotently to **PostgreSQL** (Supabase, Neon, Amazon RDS), **MySQL** (MariaDB,
Aurora), **MongoDB** (Atlas, DocumentDB) or an **HTTPS webhook**.

[한국어](README.ko.md) | [How the modules fit together](https://qaechi.github.io/modules/)

```text
qaechi.parse.results ──▶ qaechi-sink ──GET /results/{id}──▶ qaechi-parser
                              │
                              └──upsert──▶ PostgreSQL | MySQL | MongoDB | https://your-app
```

## Run it

```bash
pip install "qaechi-sink[kafka,postgres]"      # or mysql, mongodb; the webhook needs no extra

export QAECHI_KAFKA_BOOTSTRAP_SERVERS=kafka:9092
export QAECHI_PARSER_URL=http://parser:3100
export QAECHI_INTERNAL_TOKEN=...               # the parser's token
export QAECHI_SINK_TARGET=postgresql://app:secret@db:5432/app

qaechi-sink check                              # connect and create the tables
qaechi-sink run
```

Run one or more replicas; they share the consumer group `qaechi-sink`.

## Targets

| `QAECHI_SINK_TARGET` | Extra | What is written |
|---|---|---|
| `postgresql://...` (Supabase: add `?sslmode=require`) | `postgres` | Tables `qaechi_extractions` and `qaechi_extraction_values` |
| `mysql://...` (`?ssl=true` for managed services) | `mysql` | The same two tables |
| `mongodb://...` or `mongodb+srv://...` | `mongodb` | Collection `qaechi_extractions`, one document per job with the values embedded |
| `https://...` | none | One signed `POST` per job |

Tables are created on first start (`qaechi-sink check` does it explicitly). Rename them with
`QAECHI_SINK_TABLE_PREFIX`. On Supabase, enable Row Level Security on both tables before exposing
them through the Supabase API.

### Tables

`qaechi_extractions`: one row per job, the latest outcome.

| Column | |
|---|---|
| `job_id` | primary key |
| `document_id`, `doc_type` | what was parsed |
| `status` | `succeeded` or `failed` |
| `result_id`, `route`, `groundedness`, `error`, `attempts`, `finished_at` | from the result event |
| `extraction` | the full result as JSON, including notes and payload |

`qaechi_extraction_values`: one row per value, ready to query.

| Column | |
|---|---|
| `job_id`, `path` | primary key. `path` is `fields.total`, `lines[0].amount` or `total` |
| `label` | line label for `lines[i]` |
| `value_text`, `value_number` | the value; numbers also in `value_number` |
| `page`, `quote` | the evidence |
| `confidence`, `verified` | `verified` = it has a quote and the quote was found in the source |

```sql
-- Latest verified totals per document
SELECT e.document_id, v.value_number AS total, v.page, v.quote
FROM qaechi_extractions e JOIN qaechi_extraction_values v USING (job_id)
WHERE v.path = 'fields.total' AND v.verified AND e.status = 'succeeded';
```

### Webhook

Each job is one `POST` with the job summary, the full extraction and the flattened values.

| Header | |
|---|---|
| `X-Qaechi-Signature` | `sha256=` HMAC-SHA256 of the raw body with `QAECHI_SINK_WEBHOOK_SECRET`. Check it |
| `Idempotency-Key` | the job ID. A job can arrive more than once; deduplicate on it |
| `traceparent` | W3C trace context when available |

Answer `2xx` when stored. `408`, `429` and `5xx` are retried; other `4xx` go to the sink DLQ.
Only `https://` is accepted (`http://` only for localhost).

## Delivery

At least once, made safe by upserts: a redelivered event rewrites the same rows. A failed write is
retried with backoff (`QAECHI_SINK_MAX_RETRIES`, default 5) and then copied to `qaechi.sink.dlq`
with the reason in the `qaechi-error` header. The offset is committed only after the write or the
DLQ copy, so nothing is lost when the database is down.

## Settings

| Variable | Default | |
|---|---|---|
| `QAECHI_SINK_TARGET` | | where to write (required) |
| `QAECHI_KAFKA_BOOTSTRAP_SERVERS` | | brokers (required) |
| `QAECHI_PARSER_URL` | | parser API to fetch values from. Unset = status only |
| `QAECHI_INTERNAL_TOKEN` | | the parser's internal token |
| `QAECHI_SINK_INCLUDE_FAILED` | `true` | also record failed jobs |
| `QAECHI_SINK_TABLE_PREFIX` | `qaechi_` | table and collection prefix |
| `QAECHI_SINK_WEBHOOK_SECRET` | | HMAC key for webhook signatures |
| `QAECHI_SINK_MAX_RETRIES` | `5` | retries before the DLQ |
| `QAECHI_KAFKA_TOPIC_RESULTS` | `qaechi.parse.results` | |
| `QAECHI_SINK_TOPIC_DLQ` | `qaechi.sink.dlq` | |
| `QAECHI_SINK_GROUP_ID` | `qaechi-sink` | |
| `QAECHI_KAFKA_CONF_*` | | any librdkafka setting, e.g. SASL for Amazon MSK |

Another database? Implement the four-method `Target` protocol in `qaechi.sink.targets` and pass it
to `qaechi.sink.runner.run(target=...)`.

## License

Apache-2.0. See [LICENSE](LICENSE).
