Skip to content

Commit

Permalink
feat: 🚀 new srvic legislaturperiode
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Ruck <[email protected]>
  • Loading branch information
Manuel Ruck committed Sep 7, 2024
1 parent 12c11b7 commit 53e92d7
Show file tree
Hide file tree
Showing 19 changed files with 722 additions and 52 deletions.
229 changes: 177 additions & 52 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions services/legislaturperiode/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://platform.openai.com/organization/api-keys
OPENAI_API_KEY=
# Secret key to authenticate the requests
NON_NAMED_VOTES_AI_SECRET=CHANGE_ME
# Port to run the server on
PORT=3005
# MongoDB connection string
DB_URL=mongodb://localhost:27017/bundestagio
# Log level for pino
PINO_LOG_LEVEL=info
# Allowed domains for CORS
ALLOWED_DOMAINS=https://dserver.bundestag.de
1 change: 1 addition & 0 deletions services/legislaturperiode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
19 changes: 19 additions & 0 deletions services/legislaturperiode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.

## 1.1.0 (2024-08-17)


* add missing dependency ([fca4dcf](https://github.com/demokratie-live/democracy-development/commit/fca4dcf37cfbeb5064d0fd7a6e592ef1018ac3eb))
* does not fail anymore if recources are not available ([22761fd](https://github.com/demokratie-live/democracy-development/commit/22761fdd8e8e6b8d4d312052fd75617ef845de04))
* ensure vectorStore is ready ([ef3ed76](https://github.com/demokratie-live/democracy-development/commit/ef3ed761237c38fefd3134662d9a90b826bb958f))
* **garden:** 🧑‍🌾 add garden to non-named-votes-ai ([1be3780](https://github.com/demokratie-live/democracy-development/commit/1be3780aa5bfb347917e991b88b9792b7ebd1cab))
* rename SECRET name ([e921136](https://github.com/demokratie-live/democracy-development/commit/e9211361c5af6bf962a4153deb436c0ad8c41002))
* **sec:** 🛡️ Leakage of information in logger message ([0d27136](https://github.com/demokratie-live/democracy-development/commit/0d27136a47f346a8ab1f8c2eef5c9a392a00d638))


### 🚀 Feature

* 🚀 add non-named-votes-ai service ([bf5c7e7](https://github.com/demokratie-live/democracy-development/commit/bf5c7e7373fa640295f2cb9addd87cdca31ac538))
* add delete all endpoint ([6554322](https://github.com/demokratie-live/democracy-development/commit/6554322def2cd428315a535c3d6c0ba1d945708c))
66 changes: 66 additions & 0 deletions services/legislaturperiode/FLOW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Flow Charts

Dieser Service liefert den Textabschnitt, in dem über eine **nicht namentliche Abstimmung** abgestimmt wird. Dafür benötigt er eine **URL** zu einem PDF mit einem Plenarsaalprotokoll, den **Titel** der Gesetzgebung oder des Antrags und optional die **Dokumentennummer**.

## High Level

```mermaid
flowchart TD
Start([Input: '´plenaarsaalprotokoll-url, titel, dokumentennummer])
Error([Es konnte kein text gefunden werden, da das Plenarsaalprotokoll nicht gefunden wurde])
Output([Output: Abstimmungstext der nicht namentlichen abstimmung])
Start --> |Ok|Output
Start --> |Fehler|Error
```

## In Deep

```mermaid
flowchart TD
Start([Start mit '´plenaarsaalprotokoll-url, titel, dokumentennummer])
RequestDb([Datenbankabfrage: Plenarsaalprotokoll 'url'])
AssistantExists{Assistent existiert?}
CreateAssistant([Assistent erstellen])
UploadFile([Datei bei openAI hochladen])
CreateVectorStore([VectorStore erstellen])
CheckOpenAI{VectorStore bei openAI verfügbar?}
UseExistingVectorStore([Bestehenden VectorStore verwenden])
CreateThread([Neuen Thread erstellen])
StartThread([Thread mit Assistant starten])
SaveEntry([Assistent und VectorStore in Datenbank speichern])
GetMessages([Antwort abrufen])
FilterMessage([Antwort zwischen ### herausfiltern])
DeleteThread([Thread löschen])
ReturnMessage([Antwort zurückgeben])
Error([Fehler: Kein Plenarsaalprotokoll vorhanden])
ReturnError([Fehlerantwort])
Start --> RequestDb
RequestDb -->|Eintrag vorhanden| AssistantExists
RequestDb -->|Kein Eintrag| Error
Error --> ReturnError
AssistantExists --> |Ja| CheckOpenAI
AssistantExists --> |Nein| CreateAssistant
CreateAssistant --> CreateThread
CheckOpenAI -->|Ja| UseExistingVectorStore
CheckOpenAI -->|Nein| UploadFile
UseExistingVectorStore --> CreateThread
UploadFile --> CreateVectorStore
CreateVectorStore --> CreateThread
CreateThread --> StartThread
StartThread --> SaveEntry
SaveEntry --> GetMessages
GetMessages --> FilterMessage
FilterMessage --> DeleteThread
DeleteThread --> ReturnMessage
```
64 changes: 64 additions & 0 deletions services/legislaturperiode/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
kind: Build
name: legislaturperiode
type: container
source:
path: ../../
include:
- ./**/*
- ./infra/Dockerfile
spec:
dockerfile: ./infra/Dockerfile
buildArgs:
NODE_VERSION: 18.18.2
SERVICE: legislaturperiode
SERVICE_PATH: services/legislaturperiode

---
kind: Deploy
name: legislaturperiode
type: kubernetes
dependencies: [build.legislaturperiode, deploy.mongo]

varfiles:
- path: .env.example
- path: .env
optional: true

spec:
files: [./manifests/*] # <--- Tell Garden what manifests to use

defaultTarget: # <--- This tells Garden what "target" to use for logs, code syncing and more
kind: Deployment
name: legislaturperiode

sync:
paths:
- containerPath: /services/legislaturperiode/src
sourcePath: src
mode: one-way
overrides:
- command: [pnpm, dev]

patchResources:
- name: legislaturperiode
kind: Deployment
patch:
spec:
template:
spec:
containers:
- name: legislaturperiode
image: ${actions.build.legislaturperiode.outputs.deploymentImageId}
env:
- name: PORT
value: '3005'
- name: DB_URL
value: mongodb://democracy-mongo:27017/bundestagio
- name: OPENAI_API_KEY
value: ${var.OPENAI_API_KEY}
- name: NON_NAMED_VOTES_AI_SECRET
value: ${var.NON_NAMED_VOTES_AI_SECRET}
- name: PINO_LOG_LEVEL
value: ${var.PINO_LOG_LEVEL}
- name: ALLOWED_DOMAINS
value: ${var.ALLOWED_DOMAINS}
31 changes: 31 additions & 0 deletions services/legislaturperiode/manifests/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: non-named-votes-ai
labels:
app: non-named-votes-ai
spec:
replicas: 1
selector:
matchLabels:
app: non-named-votes-ai
template:
metadata:
labels:
app: non-named-votes-ai
spec:
containers:
- name: non-named-votes-ai
image: democracy/non-named-votes-ai:pr-592
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3005
name: http
protocol: TCP
resources:
limits:
cpu: '1'
memory: 1Gi
requests:
cpu: 10m
memory: 90Mi
19 changes: 19 additions & 0 deletions services/legislaturperiode/manifests/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: non-named-votes-ai
labels:
app: non-named-votes-ai
spec:
ingressClassName: nginx
rules:
- host: 'non-named-votes-ai.${var.hostname}'
http:
paths:
- backend:
service:
name: non-named-votes-ai
port:
number: 80
path: /
pathType: Prefix
15 changes: 15 additions & 0 deletions services/legislaturperiode/manifests/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: non-named-votes-ai
name: non-named-votes-ai
spec:
type: ClusterIP
ports:
- name: http
port: 80
protocol: TCP
targetPort: 3005
selector:
app: non-named-votes-ai
26 changes: 26 additions & 0 deletions services/legislaturperiode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "legislaturperiode",
"version": "1.1.0",
"description": "",
"main": "build/index.js",
"scripts": {
"dev": "tsx --env-file .env --watch src/index.ts",
"build": "tsup-node",
"start": "node build/index.js"
},
"keywords": [],
"author": "",
"license": "Apache-2.0",
"dependencies": {
"pino": "^9.3.2",
"pino-pretty": "^11.2.2",
"surrealdb.js": "1.0.0-beta.9"
},
"devDependencies": {
"@types/node": "^22.1.0",
"tsconfig": "workspace:*",
"tsup": "catalog:",
"tsup-config": "workspace:*",
"tsx": "^4.11.0"
}
}
104 changes: 104 additions & 0 deletions services/legislaturperiode/src/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { LegislaturperiodeBase } from './types';

export const legislaturperiodenDump: LegislaturperiodeBase[] = [
{
end: new Date('1953-10-06T00:00:00.000Z'),
number: 1,
start: new Date('1949-09-07T00:00:00.000Z'),
},
{
end: new Date('1957-10-15T00:00:00.000Z'),
number: 2,
start: new Date('1953-10-06T00:00:00.000Z'),
},
{
end: new Date('1961-10-17T00:00:00.000Z'),
number: 3,
start: new Date('1957-10-15T00:00:00.000Z'),
},
{
end: new Date('1965-10-19T00:00:00.000Z'),
number: 4,
start: new Date('1961-10-17T00:00:00.000Z'),
},
{
end: new Date('1969-10-20T00:00:00.000Z'),
number: 5,
start: new Date('1965-10-19T00:00:00.000Z'),
},
{
end: new Date('1972-09-22T00:00:00.000Z'),
number: 6,
start: new Date('1969-10-20T00:00:00.000Z'),
},
{
end: new Date('1976-12-14T00:00:00.000Z'),
number: 7,
start: new Date('1972-09-22T00:00:00.000Z'),
},
{
end: new Date('1980-12-04T00:00:00.000Z'),
number: 8,
start: new Date('1976-12-14T00:00:00.000Z'),
},
{
end: new Date('1983-03-29T00:00:00.000Z'),
number: 9,
start: new Date('1980-12-04T00:00:00.000Z'),
},
{
end: new Date('1987-02-18T00:00:00.000Z'),
number: 10,
start: new Date('1983-03-29T00:00:00.000Z'),
},
{
end: new Date('1990-12-20T00:00:00.000Z'),
number: 11,
start: new Date('1987-02-18T00:00:00.000Z'),
},
{
end: new Date('1994-11-10T00:00:00.000Z'),
number: 12,
start: new Date('1990-12-20T00:00:00.000Z'),
},
{
end: new Date('1998-10-26T00:00:00.000Z'),
number: 13,
start: new Date('1994-11-10T00:00:00.000Z'),
},
{
end: new Date('2002-10-17T00:00:00.000Z'),
number: 14,
start: new Date('1998-10-26T00:00:00.000Z'),
},
{
end: new Date('2005-10-18T00:00:00.000Z'),
number: 15,
start: new Date('2002-10-17T00:00:00.000Z'),
},
{
end: new Date('2009-10-27T00:00:00.000Z'),
number: 16,
start: new Date('2005-10-18T00:00:00.000Z'),
},
{
end: new Date('2013-10-22T00:00:00.000Z'),
number: 17,
start: new Date('2009-10-27T00:00:00.000Z'),
},
{
end: new Date('2017-10-24T00:00:00.000Z'),
number: 18,
start: new Date('2013-10-22T00:00:00.000Z'),
},
{
end: new Date('2021-10-26T00:00:00.000Z'),
number: 19,
start: new Date('2017-10-24T00:00:00.000Z'),
},
{
end: new Date('2025-10-22T00:00:00.000Z'),
number: 20,
start: new Date('2021-10-26T00:00:00.000Z'),
},
];
31 changes: 31 additions & 0 deletions services/legislaturperiode/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Surreal from 'surrealdb.js';
import { log } from './logger';

let db: Surreal | undefined;

export async function initDb(): Promise<Surreal | undefined> {
if (db) return db;
db = new Surreal();
if (!process.env.DB_URL) {
log.error('SURREALDB_URL is not set');
throw new Error('SURREALDB_URL is not set');
}
try {
await db.connect(process.env.DB_URL);
await db.use({ namespace: process.env.NAMESPACE, database: process.env.DATABASE });
return db;
} catch (err) {
log.error('Failed to connect to SurrealDB:', err);
throw err;
}
}

export async function closeDb(): Promise<void> {
if (!db) return;
await db.close();
db = undefined;
}

export function getDb(): Surreal | undefined {
return db;
}
Loading

0 comments on commit 53e92d7

Please sign in to comment.