Skip to content

Commit

Permalink
feat: implement report backend storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparkier committed Aug 31, 2023
1 parent 970c528 commit 938d056
Show file tree
Hide file tree
Showing 22 changed files with 1,094 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ CREATE TABLE projects (
public boolean NOT NULL DEFAULT false
);

CREATE TABLE reports (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
owner_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
public boolean NOT NULL DEFAULT false
);

CREATE TABLE report_element (
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE,
type text NOT NULL,
data text,
chart_id integer REFERENCES charts(id) ON DELETE CASCADE ON UPDATE CASCADE,
position integer NOT NULL
);

CREATE TABLE organizations (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
Expand Down Expand Up @@ -77,3 +92,23 @@ CREATE TABLE organization_project (
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
editor boolean NOT NULL DEFAULT false
);

CREATE TABLE report_project (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE,
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE user_report (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE,
editor boolean NOT NULL DEFAULT false
);

CREATE TABLE organization_report (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
organization_id integer NOT NULL REFERENCES organizations(id) ON DELETE CASCADE ON UPDATE CASCADE,
report_id integer NOT NULL REFERENCES reports(id) ON DELETE CASCADE ON UPDATE CASCADE,
editor boolean NOT NULL DEFAULT false
);
52 changes: 52 additions & 0 deletions backend/zeno_backend/classes/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Types for Zeno reports."""
from enum import Enum

from zeno_backend.classes.base import CamelModel


class ReportElementType(Enum):
"""Enumeration of possible report types in Zeno.
Attributes:
CHART: Chart element for a report.
TEXT: Text element for a report.
"""

CHART = "CHART"
TEXT = "TEXT"


class Report(CamelModel):
"""Representation of a report in Zeno.
Attributes:
id (int): ID of the report.
name (str): name of the report.
owner_name (str): name of the creater of the report
linked_projects (list[str]): all projects that can be used with the report.
editor (bool): whether the current user can edit the report.
public (bool): whether the report is publically visible.
"""

id: int
name: str
owner_name: str
linked_projects: list[str]
editor: bool
public: bool = False


class ReportElement(CamelModel):
"""Representation of an element of a Zeno report.
Attributes:
type (ReportElementType): what type of element this represents.
data (str | None): any data that the element holds.
chart_id (int | None): id of the chart this element is linked to.
"""

id: int
type: ReportElementType
data: str | None
chart_id: int | None
position: int
25 changes: 25 additions & 0 deletions backend/zeno_backend/database/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def project(project: str):
db.commit()


def report(report_id: int):
"""Deletes a report from Zeno.
Args:
report_id (int): the id of the report to be deleted.
"""
db = Database()
db.connect_execute(
"DELETE FROM reports WHERE id = %s;",
[
report_id,
],
)


def folder(folder: Folder):
"""Deletes a folder from an existing project.
Expand Down Expand Up @@ -141,3 +156,13 @@ def project_org(project: str, organization: Organization):
"AND project_uuid = %s;",
[organization.id, project],
)


def report_element(id: int):
"""Delete an element of a report.
Args:
id (int): ID of the element to be deleted.
"""
db = Database()
db.connect_execute("DELETE FROM report_element WHERE id = %s;", [id])
14 changes: 14 additions & 0 deletions backend/zeno_backend/database/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,20 @@ def system(
db.commit()


def report(name: str, user: User):
"""Adding a report to Zeno.
Args:
name (str): how the report is called.
user (User): user who created the report.
"""
db = Database()
db.connect_execute(
"INSERT INTO reports (name, owner_id, public) VALUES (%s,%s,%s);",
[name, user.id, False],
)


def folder(project: str, name: str):
"""Adding a folder to an existing project.
Expand Down
Loading

0 comments on commit 938d056

Please sign in to comment.