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

Get number of unit personnel #1417

Merged
merged 7 commits into from
May 29, 2023
Merged
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
4 changes: 4 additions & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions dds_web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions dds_web/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -697,13 +697,13 @@ def reporting_units_and_users():
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,
)
Expand Down
2 changes: 1 addition & 1 deletion dds_web/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
40 changes: 40 additions & 0 deletions migrations/versions/879b99e7f212_unit_personnel_added.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""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.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 ###
16 changes: 8 additions & 8 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.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(
[row.researchuser_count, row.unituser_count, row.superadmin_count]
assert row.total_user_count != sum(
[row.researchuser_count, row.unit_personnel_count, row.superadmin_count]
)

# Verify that there are no reporting rows
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down