Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-6438] Define models for account monitor #3609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions server/athenian/api/models/persistentdata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from athenian.api.models import create_base

JSONType = JSONB().with_variant(JSON(), sqlite.dialect.name)


class ShardedByAccount:
"""All the tables contain `account_id` primary key."""
Expand Down Expand Up @@ -106,7 +108,7 @@ class DeployedLabel(Base):

deployment_name = Column(Text(), primary_key=True)
key = Column(Text(), primary_key=True)
value = Column(JSONB().with_variant(JSON(), sqlite.dialect.name))
value = Column(JSONType)


class DeployedComponent(create_time_mixin(created_at=True, updated_at=False), Base):
Expand Down Expand Up @@ -143,7 +145,7 @@ class HealthMetric(Base):
default=lambda: datetime.now(timezone.utc),
server_default=func.now(),
)
value = Column(JSONB().with_variant(JSON(), sqlite.dialect.name), nullable=False)
value = Column(JSONType, nullable=False)


class VitallyAccount(Base, create_time_mixin(created_at=False, updated_at=True)):
Expand All @@ -154,3 +156,20 @@ class VitallyAccount(Base, create_time_mixin(created_at=False, updated_at=True))
name = Column(Text())
mrr = Column(DECIMAL())
health_score = Column(Float())


class AccMonitorCheckLog(Base):
"""The log of the execution of a check in the account monitor."""

__table_args__ = {"schema": "acc_monitor"}
__tablename__ = "check_logs"

created_at = Column(
TIMESTAMP(timezone=True),
primary_key=True,
default=lambda: datetime.now(timezone.utc),
server_default=func.now(),
)
check_name = Column(Text, primary_key=True)
passed = Column(Boolean, nullable=False)
result = Column(JSONType)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Account monitor

Revision ID: 765542234720
Revises: e06c9947fbdb
Create Date: 2023-04-19 08:50:36.182075+00:00

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "765542234720"
down_revision = "e06c9947fbdb"
branch_labels = None
depends_on = None


def upgrade():
name, schema_arg = _name_schema_arg("acc_monitor", "check_logs")

op.create_table(
name,
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column(
"created_at",
sa.TIMESTAMP(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.Column("check_name", sa.Text(), nullable=False),
sa.Column("passed", sa.Boolean(), nullable=False),
sa.Column(
"result",
postgresql.JSONB(astext_type=sa.Text()).with_variant(sa.JSON(), "sqlite"),
nullable=True,
),
sa.PrimaryKeyConstraint("account_id", "created_at", "check_name"),
**schema_arg,
)


def downgrade():
name, schema_arg = _name_schema_arg("acc_monitor", "check_logs")
op.drop_table(name, **schema_arg)


def _name_schema_arg(schema, name):
if op.get_bind().dialect.name == "postgresql":
op.execute(f"CREATE SCHEMA IF NOT EXISTS {schema}")
return name, {"schema": schema}
else:
return f"schema.{name}", {}
4 changes: 3 additions & 1 deletion server/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,9 @@ def persistentdata_db(worker_id) -> str:
PersistentdataBase,
worker_id,
{
"postgresql": "create schema if not exists athenian;",
"postgresql": (
"CREATE SCHEMA IF NOT EXISTS athenian; CREATE SCHEMA IF NOT EXISTS acc_monitor "
),
},
)

Expand Down