Skip to content

Commit

Permalink
Feature: added Result.run_id (#141)
Browse files Browse the repository at this point in the history
* added Result.run_id

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added run_id to _Metric._add_to_database

* added migration

* got run_id from envvar

* excluded migrations

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
marqueewinq and pre-commit-ci[bot] authored Sep 15, 2023
1 parent 4ee4908 commit 0c891bf
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ dmypy.json

# OSX hidden files
.DS_Store
.idea
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ RUN apt-get install libpq-dev postgresql-client -y
COPY requirements.txt requirements.txt
RUN pip install -U pip wheel
RUN pip install -r requirements.txt
COPY alembic.ini /code/
COPY migrations /code/migrations
COPY scripts /code/scripts
COPY --from=builder /dist/*.whl /dist/
RUN pip install /dist/*.whl
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ or multiple dataframes.
>>> p.plot_dataset([df1, df2])
```

### Migrations

`insight` populates the results to the Postgres database configured by environment variables. To run migrations against it, simply:

```bash
insight-migrations
```

<div align="center">
<img alt="distribution plots" src="https://user-images.githubusercontent.com/13236749/181318624-95ad8702-5113-4a9e-8dd4-32f4a3dc8c9f.png" width="85%">
</div>
21 changes: 12 additions & 9 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ services:
- postgres
tty: true
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
volumes:
- ./src:/code/src
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: 0.0.0.0
POSTGRES_PORT: 5432
command: ["postgres", "-c", "log_statement=all"]
logging:
options:
max-size: 10m
max-file: "3"
ports:
- "5432:${POSTGRES_PORT}"
- "5432:5432"
volumes:
- "./postgres-data:/var/lib/postgresql/data"
...
35 changes: 29 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
alembic == 1.7.6
numpy == 1.21.6
pandas == 1.3.5
psycopg2 == 2.9.2
SQLAlchemy[mypy] == 1.4.40
synthesized_datasets == 0.6
alembic==1.7.6
contourpy==1.1.0
cycler==0.11.0
fonttools==4.42.1
greenlet==2.0.2
importlib-resources==6.0.1
kiwisolver==1.4.5
Mako==1.2.4
MarkupSafe==2.1.3
matplotlib==3.7.3
mypy==1.5.1
mypy-extensions==1.0.0
numpy==1.21.6
packaging==23.1
pandas==1.3.5
Pillow==10.0.0
psycopg2==2.9.2
pyparsing==3.1.1
python-dateutil==2.8.2
pytz==2023.3.post1
scipy==1.10.1
seaborn==0.12.2
six==1.16.0
SQLAlchemy==1.4.40
sqlalchemy2-stubs==0.0.2a35
synthesized-datasets==0.6
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.16.2
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sonar.projectName=synthesized-insight
sonar.sourceEncoding=UTF-8

sonar.sources=src/insight/
sonar.exclusions=src/insight/alembic/**/*
sonar.tests=tests/

sonar.python.version=3.6, 3.7, 3.8, 3.9
Expand Down
6 changes: 4 additions & 2 deletions src/insight/alembic/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Python script that will apply the migrations up to head
import os
import sys

import alembic.config

here = os.path.dirname(os.path.abspath(__file__))

args = sys.argv[1:]
args = args if len(args) > 0 else ["upgrade", "head"] # default
alembic_args = [
'-c', os.path.join(here, 'alembic.ini'),
'upgrade', 'head'
]
] + args


def main():
Expand Down
33 changes: 33 additions & 0 deletions src/insight/alembic/versions/d2198fd60b0e_added_result_run_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Added Result.run_id
Revision ID: d2198fd60b0e
Revises: 9aca5ae68ff5
Create Date: 2023-09-14 12:25:17.878689
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'd2198fd60b0e'
down_revision = '9aca5ae68ff5'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('result', sa.Column('run_id', sa.VARCHAR(length=50), nullable=False))
op.alter_column('version', 'name',
existing_type=sa.VARCHAR(length=50),
nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('version', 'name',
existing_type=sa.VARCHAR(length=50),
nullable=True)
op.drop_column('result', 'run_id')
# ### end Alembic commands ###
12 changes: 5 additions & 7 deletions src/insight/database/schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from sqlalchemy import FLOAT, INTEGER, TIMESTAMP, VARCHAR, ForeignKey
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy import FLOAT, INTEGER, TIMESTAMP, VARCHAR, Column, ForeignKey
from sqlalchemy.orm import Mapped, declarative_base, relationship
from sqlalchemy.sql import func


class Base(DeclarativeBase):
pass


Base = declarative_base()
mapped_column = Column

class Dataset(Base):
__tablename__ = "dataset"
Expand Down Expand Up @@ -44,6 +41,7 @@ class Result(Base):
version_id = mapped_column(INTEGER, ForeignKey("version.id"))
value = mapped_column(FLOAT)
created_at = mapped_column(TIMESTAMP, default=func.now())
run_id = mapped_column(VARCHAR(50), nullable=True, default=None)

metric: Mapped[Metric] = relationship("Metric")
dataset: Mapped[Dataset] = relationship("Dataset")
Expand Down
8 changes: 6 additions & 2 deletions src/insight/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ def _add_to_database(self,
dataset_rows: Number of rows in the dataset.
dataset_cols: Number of column in the dataset.
category: The category of the metric.
version: The version on which the test was run.
`version` and `run_id` are taken from `VERSION` and `RUN_ID` envvars.
"""
version = os.getenv("VERSION")
version = version if version else "Unversioned"
run_id = os.getenv("RUN_ID")

if model is None or utils is None:
raise ModuleNotFoundError("The database module is not available. Please install it using the command: pip install 'insight[db]'")
Expand All @@ -98,7 +100,9 @@ def _add_to_database(self,
metric_id = utils.get_metric_id(self.name, session, category=category)
version_id = utils.get_version_id(version, session)
dataset_id = utils.get_df_id(dataset_name, session, num_rows=dataset_rows, num_columns=dataset_cols)
result = model.Result(metric_id=metric_id, dataset_id=dataset_id, version_id=version_id, value=value)
result = model.Result(
metric_id=metric_id, dataset_id=dataset_id, version_id=version_id, value=value, run_id=run_id
)
session.add(result)
session.commit()

Expand Down

0 comments on commit 0c891bf

Please sign in to comment.