Skip to content

Commit

Permalink
feat: reports data structure and frontend V1 (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Cabrera <[email protected]>
  • Loading branch information
Sparkier and cabreraalex authored Sep 14, 2023
1 parent 4f5a28b commit fb8c73e
Show file tree
Hide file tree
Showing 52 changed files with 2,741 additions and 581 deletions.
52 changes: 44 additions & 8 deletions backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ CREATE TABLE projects (
public boolean NOT NULL DEFAULT false
);

CREATE TABLE charts (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
name text NOT NULL,
type text NOT NULL,
parameters text NOT NULL
);

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_elements (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
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 All @@ -34,14 +58,6 @@ CREATE TABLE folders (
name text NOT NULL
);

CREATE TABLE charts (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
project_uuid text NOT NULL REFERENCES projects(uuid) ON DELETE CASCADE ON UPDATE CASCADE,
name text NOT NULL,
type text NOT NULL,
parameters text NOT NULL
);

CREATE TABLE slices (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
Expand Down Expand Up @@ -77,3 +93,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
);
1 change: 1 addition & 0 deletions backend/zeno_backend/classes/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Chart(CamelModel):
| RadarParameters
| HeatmapParameters
)
project_uuid: str | None = None


class ParametersEncoder(json.JSONEncoder):
Expand Down
64 changes: 64 additions & 0 deletions backend/zeno_backend/classes/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""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 | None = None
type: ReportElementType
position: int
data: str | None = None
chart_id: int | None = None


class ReportResponse(CamelModel):
"""Response for a report in Zeno.
Attributes:
report (Report): the report itself.
report_elements (list[ReportElement]): all elements of the report.
"""

report: Report
report_elements: list[ReportElement]
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_elements WHERE id = %s;", [id])
30 changes: 30 additions & 0 deletions backend/zeno_backend/database/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from zeno_backend.classes.chart import Chart, ParametersEncoder
from zeno_backend.classes.filter import PredicatesEncoder
from zeno_backend.classes.project import Project
from zeno_backend.classes.report import ReportElement
from zeno_backend.classes.slice import Slice
from zeno_backend.classes.tag import Tag
from zeno_backend.classes.user import Organization, User
Expand Down Expand Up @@ -316,6 +317,35 @@ 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 report_element(report_id: int, element: ReportElement):
"""Adding a report element to a Zeno Report.
Args:
report_id (int): the id of the report the element is added to.
element (ReportElement): the element to be added to the report.
"""
db = Database()
db.connect_execute(
"INSERT INTO report_elements (report_id, type, data, chart_id, position)"
" VALUES (%s,%s,%s,%s,%s);",
[report_id, element.type, element.data, element.chart_id, element.position],
)


def folder(project: str, name: str) -> int | None:
"""Adding a folder to an existing project.
Expand Down
Loading

0 comments on commit fb8c73e

Please sign in to comment.