From ade034c8fb3961b9fde68dcf3f791074949aabc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:11:36 +0200 Subject: [PATCH 1/7] added unit personnel count --- dds_web/__init__.py | 4 ++-- dds_web/commands.py | 10 +++++----- dds_web/database/models.py | 2 +- tests/test_commands.py | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dds_web/__init__.py b/dds_web/__init__.py index f3c34b64e..337903d9f 100644 --- a/dds_web/__init__.py +++ b/dds_web/__init__.py @@ -275,7 +275,7 @@ def load_user(user_id): set_expired_to_archived, delete_invites, quarterly_usage, - reporting_units_and_users, + collect_stats, monitor_usage, ) @@ -290,7 +290,7 @@ def load_user(user_id): app.cli.add_command(set_expired_to_archived) app.cli.add_command(delete_invites) app.cli.add_command(quarterly_usage) - app.cli.add_command(reporting_units_and_users) + app.cli.add_command(collect_stats) app.cli.add_command(monitor_usage) # Make version available inside jinja templates: diff --git a/dds_web/commands.py b/dds_web/commands.py index 004b9c832..d0f2eac28 100644 --- a/dds_web/commands.py +++ b/dds_web/commands.py @@ -668,9 +668,9 @@ def quarterly_usage(): raise -@click.command("reporting-units-and-users") +@click.command("stats") @flask.cli.with_appcontext -def reporting_units_and_users(): +def collect_stats(): """ At the start of every month, get number of units and users. Should be run on the 1st of each month, at around 00:01. @@ -692,18 +692,18 @@ def reporting_units_and_users(): error_body: str = ( f"The cronjob 'reporting' experienced issues. Please see logs. Time: {current_time}." ) - + # New reporting row - numbers are automatically set try: unit_count = Unit.query.count() researchuser_count = ResearchUser.query.count() - unituser_count = UnitUser.query.count() + unit_personnel_count = UnitUser.query.filter_by(is_admin=False).count() superadmin_count = SuperAdmin.query.count() total_user_count = User.query.count() new_reporting_row = Reporting( unit_count=unit_count, researchuser_count=researchuser_count, - unituser_count=unituser_count, + unit_personnel_count=unit_personnel_count, superadmin_count=superadmin_count, total_user_count=total_user_count, ) diff --git a/dds_web/database/models.py b/dds_web/database/models.py index 049558b1d..ade3ceb90 100644 --- a/dds_web/database/models.py +++ b/dds_web/database/models.py @@ -1080,6 +1080,6 @@ class Reporting(db.Model): date = db.Column(db.DateTime(), unique=True, nullable=False, default=datetime.date.today) unit_count = db.Column(db.Integer, unique=False, nullable=False) researchuser_count = db.Column(db.Integer, unique=False, nullable=False) - unituser_count = db.Column(db.Integer, unique=False, nullable=False) + unit_personnel_count = db.Column(db.Integer, unique=False, nullable=False) superadmin_count = db.Column(db.Integer, unique=False, nullable=False) total_user_count = db.Column(db.Integer, unique=False, nullable=False) diff --git a/tests/test_commands.py b/tests/test_commands.py index 059a98516..3aaddf53c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -439,11 +439,11 @@ def verify_reporting_row(row, time_date): assert row.date.date() == datetime.date(time_date) assert row.unit_count == Unit.query.count() assert row.researchuser_count == ResearchUser.query.count() - assert row.unituser_count == UnitUser.query.count() + assert row.unit_personnel_count == UnitUser.query.count() assert row.superadmin_count == SuperAdmin.query.count() assert row.total_user_count == User.query.count() assert row.total_user_count == sum( - [row.researchuser_count, row.unituser_count, row.superadmin_count] + [row.researchuser_count, row.unit_personnel_count, row.superadmin_count] ) # Verify that there are no reporting rows From 0c51f7e98278819c49108a21c364557127c1e17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:15:44 +0200 Subject: [PATCH 2/7] fixed test --- tests/test_commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3aaddf53c..8f514a468 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -439,10 +439,10 @@ def verify_reporting_row(row, time_date): assert row.date.date() == datetime.date(time_date) assert row.unit_count == Unit.query.count() assert row.researchuser_count == ResearchUser.query.count() - assert row.unit_personnel_count == UnitUser.query.count() + assert row.unit_personnel_count == UnitUser.query.filter_by(is_admin=False).count() assert row.superadmin_count == SuperAdmin.query.count() assert row.total_user_count == User.query.count() - assert row.total_user_count == sum( + assert row.total_user_count != sum( [row.researchuser_count, row.unit_personnel_count, row.superadmin_count] ) From bf158c5209529fc710c156fb8740e0e860011b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:16:22 +0200 Subject: [PATCH 3/7] migration --- .../879b99e7f212_unit_personnel_added.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 migrations/versions/879b99e7f212_unit_personnel_added.py diff --git a/migrations/versions/879b99e7f212_unit_personnel_added.py b/migrations/versions/879b99e7f212_unit_personnel_added.py new file mode 100644 index 000000000..45d47517e --- /dev/null +++ b/migrations/versions/879b99e7f212_unit_personnel_added.py @@ -0,0 +1,29 @@ +"""unit_personnel_added + +Revision ID: 879b99e7f212 +Revises: b976f6cda95c +Create Date: 2023-05-29 07:51:05.491352 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = '879b99e7f212' +down_revision = 'b976f6cda95c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + # op.add_column('reporting', sa.Column('unit_personnel_count', sa.Integer(), nullable=False)) + op.alter_column(table_name='reporting', column_name='unituser_count', nullable=False, new_column_name='unit_personnel_count', type_=sa.Integer()) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('reporting', 'unit_personnel_count', nullable=False, new_column_name='unituser_count', type_=sa.Integer()) + # ### end Alembic commands ### From 87c9bdb6bd0fe06631ddd1389ad858671b4d1d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:20:35 +0200 Subject: [PATCH 4/7] sprint log --- SPRINTLOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SPRINTLOG.md b/SPRINTLOG.md index b9ad9bf5f..dc9459df9 100644 --- a/SPRINTLOG.md +++ b/SPRINTLOG.md @@ -241,3 +241,7 @@ _Nothing merged in CLI during this sprint_ - Documentation: Minor update of Technical Overview ((#1411)[https://github.com/ScilifelabDataCentre/dds_web/pull/1411]) - Documentation: Account roles and their permissions ((#1412)[https://github.com/ScilifelabDataCentre/dds_web/pull/1412]) + +# 2023-05-26 - 2023-06-09 + +- Command: Save number of Unit Personnel instead of total number of unit users ([#1417](https://github.com/ScilifelabDataCentre/dds_web/pull/1417)) From 5773e73b18ff17322ab807a2f8154f7b4c63900f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:22:46 +0200 Subject: [PATCH 5/7] black --- dds_web/commands.py | 2 +- .../879b99e7f212_unit_personnel_added.py | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dds_web/commands.py b/dds_web/commands.py index d0f2eac28..28aada83a 100644 --- a/dds_web/commands.py +++ b/dds_web/commands.py @@ -692,7 +692,7 @@ def collect_stats(): error_body: str = ( f"The cronjob 'reporting' experienced issues. Please see logs. Time: {current_time}." ) - + # New reporting row - numbers are automatically set try: unit_count = Unit.query.count() diff --git a/migrations/versions/879b99e7f212_unit_personnel_added.py b/migrations/versions/879b99e7f212_unit_personnel_added.py index 45d47517e..abfe5539f 100644 --- a/migrations/versions/879b99e7f212_unit_personnel_added.py +++ b/migrations/versions/879b99e7f212_unit_personnel_added.py @@ -10,8 +10,8 @@ from sqlalchemy.dialects import mysql # revision identifiers, used by Alembic. -revision = '879b99e7f212' -down_revision = 'b976f6cda95c' +revision = "879b99e7f212" +down_revision = "b976f6cda95c" branch_labels = None depends_on = None @@ -19,11 +19,23 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # op.add_column('reporting', sa.Column('unit_personnel_count', sa.Integer(), nullable=False)) - op.alter_column(table_name='reporting', column_name='unituser_count', nullable=False, new_column_name='unit_personnel_count', type_=sa.Integer()) + op.alter_column( + table_name="reporting", + column_name="unituser_count", + nullable=False, + new_column_name="unit_personnel_count", + type_=sa.Integer(), + ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('reporting', 'unit_personnel_count', nullable=False, new_column_name='unituser_count', type_=sa.Integer()) + op.alter_column( + "reporting", + "unit_personnel_count", + nullable=False, + new_column_name="unituser_count", + type_=sa.Integer(), + ) # ### end Alembic commands ### From 3b31455b492966bafcafea5a2478dc17d173975b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:23:37 +0200 Subject: [PATCH 6/7] remove row from migration version --- migrations/versions/879b99e7f212_unit_personnel_added.py | 1 - 1 file changed, 1 deletion(-) diff --git a/migrations/versions/879b99e7f212_unit_personnel_added.py b/migrations/versions/879b99e7f212_unit_personnel_added.py index abfe5539f..881d9bdd0 100644 --- a/migrations/versions/879b99e7f212_unit_personnel_added.py +++ b/migrations/versions/879b99e7f212_unit_personnel_added.py @@ -18,7 +18,6 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - # op.add_column('reporting', sa.Column('unit_personnel_count', sa.Integer(), nullable=False)) op.alter_column( table_name="reporting", column_name="unituser_count", From f43f5d3d7f68e638dbae0d3b1376709a3bf87d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ina=20Od=C3=A9n=20=C3=96sterbo?= Date: Mon, 29 May 2023 10:48:11 +0200 Subject: [PATCH 7/7] fixed wrong import --- tests/test_commands.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 8f514a468..68deab355 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -30,7 +30,7 @@ set_expired_to_archived, delete_invites, quarterly_usage, - reporting_units_and_users, + collect_stats, ) from dds_web.database import models from dds_web import db @@ -430,7 +430,7 @@ def test_quarterly_usage(client, cli_runner): # reporting units and users -def test_reporting_units_and_users(client, cli_runner, fs: FakeFilesystem): +def test_collect_stats(client, cli_runner, fs: FakeFilesystem): """Test that the reporting is giving correct values.""" from dds_web.database.models import Unit, UnitUser, ResearchUser, SuperAdmin, User, Reporting @@ -454,7 +454,7 @@ def verify_reporting_row(row, time_date): with freezegun.freeze_time(first_time): # Run scheduled job now with mock.patch.object(flask_mail.Mail, "send") as mock_mail_send: - result: click.testing.Result = cli_runner.invoke(reporting_units_and_users) + result: click.testing.Result = cli_runner.invoke(collect_stats) assert not result.exception, "Raised an unwanted exception." assert mock_mail_send.call_count == 0 @@ -468,7 +468,7 @@ def verify_reporting_row(row, time_date): # Run scheduled job now with mock.patch.object(flask_mail.Mail, "send") as mock_mail_send: # with pytest.raises(Exception) as err: - result: click.testing.Result = cli_runner.invoke(reporting_units_and_users) + result: click.testing.Result = cli_runner.invoke(collect_stats) assert result.exception, "Did not raise exception." assert "Duplicate entry" in str(result.exception) assert mock_mail_send.call_count == 1 @@ -478,7 +478,7 @@ def verify_reporting_row(row, time_date): with freezegun.freeze_time(second_time): # Run scheduled job now with mock.patch.object(flask_mail.Mail, "send") as mock_mail_send: - result: click.testing.Result = cli_runner.invoke(reporting_units_and_users) + result: click.testing.Result = cli_runner.invoke(collect_stats) assert not result.exception, "Raised an unwanted exception." assert mock_mail_send.call_count == 0