Add portal database layer
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.pytest_cache/
|
||||||
|
.env
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
from app.db.database import get_connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_apps():
|
||||||
|
con = get_connection()
|
||||||
|
|
||||||
|
rows = con.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
language,
|
||||||
|
version,
|
||||||
|
status,
|
||||||
|
COALESCE(memory, '') AS memory,
|
||||||
|
COALESCE(cpus, '') AS cpus,
|
||||||
|
updated_at
|
||||||
|
FROM apps
|
||||||
|
ORDER BY id
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def get_app(app_id: str):
|
||||||
|
con = get_connection()
|
||||||
|
|
||||||
|
row = con.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
language,
|
||||||
|
version,
|
||||||
|
status,
|
||||||
|
COALESCE(memory, '') AS memory,
|
||||||
|
COALESCE(cpus, '') AS cpus,
|
||||||
|
updated_at
|
||||||
|
FROM apps
|
||||||
|
WHERE id = ?
|
||||||
|
""",
|
||||||
|
(app_id,),
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return dict(row)
|
||||||
|
|
||||||
|
|
||||||
|
def get_deployments(limit: int = 100):
|
||||||
|
con = get_connection()
|
||||||
|
|
||||||
|
rows = con.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
app_id,
|
||||||
|
kind,
|
||||||
|
status,
|
||||||
|
started_at,
|
||||||
|
finished_at,
|
||||||
|
returncode
|
||||||
|
FROM deployments
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(limit,),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def get_deployment(deployment_id: int):
|
||||||
|
con = get_connection()
|
||||||
|
|
||||||
|
row = con.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
app_id,
|
||||||
|
kind,
|
||||||
|
status,
|
||||||
|
started_at,
|
||||||
|
finished_at,
|
||||||
|
returncode,
|
||||||
|
stdout,
|
||||||
|
stderr
|
||||||
|
FROM deployments
|
||||||
|
WHERE id = ?
|
||||||
|
""",
|
||||||
|
(deployment_id,),
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return dict(row)
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import sqlite3
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
DB_FILE = Path("/opt/appfactory/data/appfactory/appfactory.db")
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection():
|
||||||
|
con = sqlite3.connect(DB_FILE)
|
||||||
|
con.row_factory = sqlite3.Row
|
||||||
|
return con
|
||||||
Reference in New Issue
Block a user