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