Skip to content

Commit 6366722

Browse files
author
Gaetano Guerriero
committed
[DEV-5532] Define dashboard related DB models
1 parent 86c6258 commit 6366722

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

server/athenian/api/models/state/models.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,81 @@ class Share(create_time_mixin(created_at=True), Base):
484484
created_by = Column(String(), nullable=False)
485485
divine = Column(Boolean, nullable=False, default=False, server_default="false")
486486
data = Column(JSONType, nullable=False)
487+
488+
489+
class TeamDashboard(create_time_mixin(created_at=True, updated_at=True), Base):
490+
"""A dashboard displaying information for a team."""
491+
492+
__tablename__ = "team_dashboards"
493+
494+
id = Column(Integer(), primary_key=True)
495+
team_id = Column(
496+
Integer(),
497+
ForeignKey("teams.id", name="fk_team_dashboard_team", ondelete="CASCADE"),
498+
nullable=False,
499+
comment="The team this dashboard belongs to.",
500+
)
501+
502+
503+
class DashboardChart(create_time_mixin(created_at=True, updated_at=True), Base):
504+
"""A chart showing metric values in a dashboard."""
505+
506+
__tablename__ = "dashboard_charts"
507+
508+
id = Column(Integer(), primary_key=True)
509+
dashboard_id = Column(
510+
Integer(),
511+
ForeignKey("team_dashboards.id", name="fk_chart_dashboard", ondelete="CASCADE"),
512+
nullable=False,
513+
)
514+
position = Column(
515+
Integer(), nullable=False, comment="Position of the chart in the containing dashboard.",
516+
)
517+
518+
metric = Column(String, nullable=False, comment="The metric this chart is displaying.")
519+
520+
time_from = Column(
521+
TIMESTAMP(timezone=True),
522+
nullable=True,
523+
comment="Start of the fixed time interval of the chart",
524+
)
525+
time_to = Column(
526+
TIMESTAMP(timezone=True),
527+
nullable=True,
528+
comment="End of the fixed time interval of the chart",
529+
)
530+
"""`time_from` and `time_to` together represents the fixed time interval of the chart.
531+
532+
If `time_from` and `time_to` are set `time_interval` is NULL.
533+
"""
534+
time_interval = Column(
535+
String,
536+
nullable=True,
537+
comment="The relative time interval of the chart in ISO-8601 format.",
538+
)
539+
"""
540+
"The relative time interval of the chart in ISO-8601 format."
541+
542+
Examples: "P1W", "P10D".
543+
If `time_interval` is set `time_from` and `time_to` are NULL.
544+
"""
545+
546+
name = Column(String, nullable=False)
547+
description = Column(String, nullable=False)
548+
549+
# deferred constraint is useful to update positions easier but it's only available on postgres
550+
__table_args__ = (
551+
UniqueConstraint(
552+
"dashboard_id",
553+
"position",
554+
name="uc_chart_dashboard_id_position",
555+
deferrable=True,
556+
_create_rule=lambda compiler: compiler.dialect.name == "postgresql",
557+
),
558+
UniqueConstraint(
559+
"dashboard_id",
560+
"position",
561+
name="uc_chart_dashboard_id_position",
562+
_create_rule=lambda compiler: compiler.dialect.name != "postgresql",
563+
),
564+
)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""Dashboards
2+
3+
Revision ID: a0c659e45929
4+
Revises: da3a1ca5e029
5+
Create Date: 2022-12-16 12:48:36.243572+00:00
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
# revision identifiers, used by Alembic.
12+
revision = "a0c659e45929"
13+
down_revision = "da3a1ca5e029"
14+
branch_labels = None
15+
depends_on = None
16+
17+
18+
def upgrade():
19+
op.create_table(
20+
"team_dashboards",
21+
sa.Column(
22+
"created_at",
23+
sa.TIMESTAMP(timezone=True),
24+
server_default=sa.func.now(),
25+
nullable=False,
26+
),
27+
sa.Column(
28+
"updated_at",
29+
sa.TIMESTAMP(timezone=True),
30+
server_default=sa.func.now(),
31+
nullable=False,
32+
),
33+
sa.Column("id", sa.Integer(), nullable=False),
34+
sa.Column(
35+
"team_id", sa.Integer(), nullable=False, comment="The team this dashboard belongs to.",
36+
),
37+
sa.ForeignKeyConstraint(
38+
["team_id"], ["teams.id"], name="fk_team_dashboard_team", ondelete="CASCADE",
39+
),
40+
sa.PrimaryKeyConstraint("id"),
41+
)
42+
43+
if op.get_bind().dialect.name == "postgresql":
44+
chart_position_uc = sa.UniqueConstraint(
45+
"dashboard_id", "position", deferrable="True", name="uc_chart_dashboard_id_position",
46+
)
47+
else:
48+
chart_position_uc = (
49+
sa.UniqueConstraint("dashboard_id", "position", name="uc_chart_dashboard_id_position"),
50+
)
51+
52+
op.create_table(
53+
"dashboard_charts",
54+
sa.Column(
55+
"created_at",
56+
sa.TIMESTAMP(timezone=True),
57+
server_default=sa.func.now(),
58+
nullable=False,
59+
),
60+
sa.Column(
61+
"updated_at",
62+
sa.TIMESTAMP(timezone=True),
63+
server_default=sa.func.now(),
64+
nullable=False,
65+
),
66+
sa.Column("id", sa.Integer(), nullable=False),
67+
sa.Column("dashboard_id", sa.Integer(), nullable=False),
68+
sa.Column(
69+
"position",
70+
sa.Integer(),
71+
nullable=False,
72+
comment="Position of the chart in the containing dashboard.",
73+
),
74+
sa.Column(
75+
"metric", sa.String(), nullable=False, comment="The metric this chart is displaying.",
76+
),
77+
sa.Column(
78+
"time_from",
79+
sa.TIMESTAMP(timezone=True),
80+
nullable=True,
81+
comment="Start of the fixed time interval of the chart",
82+
),
83+
sa.Column(
84+
"time_to",
85+
sa.TIMESTAMP(timezone=True),
86+
nullable=True,
87+
comment="End of the fixed time interval of the chart",
88+
),
89+
sa.Column(
90+
"time_interval",
91+
sa.String(),
92+
nullable=True,
93+
comment="The relative time interval of the chart in ISO-8601 format.",
94+
),
95+
sa.Column("name", sa.String(), nullable=False),
96+
sa.Column("description", sa.String(), nullable=False),
97+
sa.ForeignKeyConstraint(
98+
["dashboard_id"],
99+
["team_dashboards.id"],
100+
name="fk_chart_dashboard",
101+
ondelete="CASCADE",
102+
),
103+
sa.PrimaryKeyConstraint("id"),
104+
chart_position_uc,
105+
)
106+
107+
108+
def downgrade():
109+
op.drop_table("dashboard_charts")
110+
op.drop_table("team_dashboards")

0 commit comments

Comments
 (0)