gitscope
https://dev.kronoz.win/r/marzneshin.git
app/marznode/database.py · v0.7.2
Source
1 f6a32ea Dawsh 2y ago from app.db import crud, GetDB
2 f6a32ea Dawsh 2y ago from app.models.node import NodeStatus
3 f6a32ea Dawsh 2y ago
4 f6a32ea Dawsh 2y ago
5 f6a32ea Dawsh 2y ago class MarzNodeDB:
6 f6a32ea Dawsh 2y ago def list_users(self):
7 f6a32ea Dawsh 2y ago with GetDB() as db:
8 d377c4c Dawsh 2y ago relations = crud.get_node_users(db, self.id)
9 f6a32ea Dawsh 2y ago users = dict()
10 f6a32ea Dawsh 2y ago for rel in relations:
11 f6a32ea Dawsh 2y ago if not users.get(rel[0]):
12 01cdf2e Dawsh 2y ago users[rel[0]] = dict(
13 01cdf2e Dawsh 2y ago username=rel[1], id=rel[0], key=rel[2], inbounds=[]
14 01cdf2e Dawsh 2y ago )
15 f6a32ea Dawsh 2y ago users[rel[0]]["inbounds"].append(rel[3].tag)
16 f6a32ea Dawsh 2y ago return list(users.values())
17 f6a32ea Dawsh 2y ago
18 f5c168d Mardin 2y ago def store_backends(self, backends):
19 f5c168d Mardin 2y ago inbounds = [
20 f5c168d Mardin 2y ago inbound for backend in backends for inbound in backend.inbounds
21 f5c168d Mardin 2y ago ]
22 f6a32ea Dawsh 2y ago with GetDB() as db:
23 f5c168d Mardin 2y ago crud.ensure_node_backends(db, backends, self.id)
24 f5c168d Mardin 2y ago crud.ensure_node_inbounds(db, inbounds, self.id)
25 f6a32ea Dawsh 2y ago
26 f6a32ea Dawsh 2y ago def set_status(self, status: NodeStatus, message: str | None = None):
27 f6a32ea Dawsh 2y ago with GetDB() as db:
28 f6a32ea Dawsh 2y ago crud.update_node_status(db, self.id, status, message)