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

11/04/2024 Production Deploy #1388

Merged
merged 316 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
316 commits
Select commit Hold shift + click to select a range
ee65f4e
use unique()
Oct 10, 2024
5a75a68
use unique()
Oct 10, 2024
b771739
fix delete
Oct 10, 2024
4de9ca5
fix delete
Oct 10, 2024
464dff6
fix delete
Oct 10, 2024
3f23549
fix delete
Oct 10, 2024
337b7be
fix delete
Oct 10, 2024
190c3cd
fix delete
Oct 10, 2024
a4f0ff7
fix delete
Oct 10, 2024
c97ea3b
fix delete
Oct 10, 2024
7eb58e9
fix delete
Oct 10, 2024
6114fe0
try to fix test code
Oct 10, 2024
f3a1139
try to fix test code
Oct 10, 2024
5c818b5
try to fix test code
Oct 10, 2024
58d9b2d
try to fix test code
Oct 10, 2024
b385e08
try to fix test code
Oct 10, 2024
97237f8
try to fix test code
Oct 10, 2024
79d666f
try to fix test code
Oct 10, 2024
4302610
try to fix test code
Oct 10, 2024
6da190c
try to fix test code
Oct 11, 2024
3021677
try to fix test code
Oct 11, 2024
b52070f
try to fix test code
Oct 11, 2024
fa5af8e
try to fix test code
Oct 11, 2024
3adedc5
try to fix test code
Oct 11, 2024
ad1ef5b
try to fix test code
Oct 11, 2024
aac64b2
try to fix test code
Oct 11, 2024
c6fee4a
try to fix test code
Oct 11, 2024
b5f7977
fix core daos
Oct 11, 2024
88c9af9
fix core daos
Oct 11, 2024
90b4072
fix core daos
Oct 11, 2024
d8bb71b
fix core daos
Oct 11, 2024
958861d
fix core daos
Oct 11, 2024
c44300a
fix core daos
Oct 11, 2024
84e46f1
fix core daos
Oct 11, 2024
ef6e404
fix core daos
Oct 11, 2024
4f20bfe
fix core daos
Oct 11, 2024
a5eceae
fix core daos
Oct 11, 2024
1f6dece
fix core daos
Oct 11, 2024
54ab96e
fix core daos
Oct 11, 2024
b84ed9c
fix core daos
Oct 11, 2024
7e16eeb
upgrade org and template dao to sqlalchemy 2.0
Oct 11, 2024
daac3bf
upgrade org and template dao to sqlalchemy 2.0
Oct 11, 2024
88910f5
upgrade organization_dao to sqlalchemy 2.0
Oct 14, 2024
9174efe
upgrade organization_dao to sqlalchemy 2.0
Oct 14, 2024
fe85970
upgrade organization_dao to sqlalchemy 2.0
Oct 14, 2024
ca36585
upgrade organization_dao to sqlalchemy 2.0
Oct 14, 2024
eac1122
upgrade organization_dao to sqlalchemy 2.0
Oct 14, 2024
5a48c35
fix template folder dao
Oct 14, 2024
b4a45da
fix template folder dao
Oct 14, 2024
c8a758a
fix template folder dao
Oct 14, 2024
68b9d8a
fix template folder dao
Oct 14, 2024
1eb46b3
fix template folder dao
Oct 14, 2024
ef5a45d
fix template folder dao
Oct 14, 2024
1c42508
fix template folder dao
Oct 14, 2024
758e12c
fix template folder dao
Oct 14, 2024
4a7e4c7
fix template folder dao
Oct 14, 2024
a530bab
update billing dao
Oct 14, 2024
287b7d1
update billing dao
Oct 14, 2024
43e774c
update billing dao
Oct 14, 2024
961f8f8
fix a method
Oct 14, 2024
fb1c2c1
fix a method
Oct 15, 2024
18ef32b
fix a test
Oct 15, 2024
c49bfb9
fix a method
Oct 15, 2024
1fe4ec8
fix a delete query
Oct 15, 2024
c86a0d7
fix rates query
Oct 15, 2024
333cd1d
try scalars to resolve test failure
Oct 15, 2024
223a8f0
revert scalars
Oct 15, 2024
f520262
revert scalars
Oct 15, 2024
579d856
revert scalars
Oct 15, 2024
c2a2dd0
revert scalars
Oct 15, 2024
597f2b0
remove all() from statement
Oct 15, 2024
6db8fcf
remove all() from statement
Oct 15, 2024
b9f1eae
remove all() from statement
Oct 15, 2024
2ef49ac
remove all() from statement
Oct 15, 2024
3b25cfe
remove all() from statement
Oct 15, 2024
028f55e
remove all() from statement
Oct 15, 2024
5f6894e
remove all() from statement
Oct 15, 2024
2cff3fa
remove all() from statement
Oct 15, 2024
9c95e58
remove all() from statement
Oct 15, 2024
f83032c
start on jobs dao
Oct 15, 2024
2919395
start on jobs dao
Oct 15, 2024
13c8418
start on jobs dao
Oct 15, 2024
01675ae
fix paginate
Oct 15, 2024
573098b
fix paginate
Oct 15, 2024
ad60886
try handmade pagination
Oct 15, 2024
17cfa38
try handmade pagination
Oct 15, 2024
935b778
try handmade pagination
Oct 15, 2024
94b8fc2
add prev_num to Pagination
Oct 15, 2024
9c39de4
add next_num to Pagination
Oct 15, 2024
8e3784c
revert pagination for now
Oct 16, 2024
d7700b2
fix test
Oct 16, 2024
7d3900f
fix test
Oct 16, 2024
8b38512
fix pagination maybe
Oct 16, 2024
0182aff
down to line 179
Oct 16, 2024
e8efde3
down to line 179
Oct 16, 2024
8c7aa30
down to line 179
Oct 16, 2024
5409c2a
down to line 179
Oct 16, 2024
965c5c9
everything except extend
Oct 17, 2024
50b074d
merge from main
Oct 17, 2024
f1ecfd5
try scalars
Oct 17, 2024
ead2127
try scalars
Oct 17, 2024
f13fbf8
revert scalrs
Oct 17, 2024
6ea003e
finish jobs_dao?
Oct 17, 2024
a0db2b4
use saclars() for extend
Oct 17, 2024
6c26db6
use saclars() for extend
Oct 17, 2024
f8f4e46
fix test_jobs_dao
Oct 17, 2024
a5f2022
fix inbound_number_dao
Oct 17, 2024
bc235eb
fix inbound_number_dao
Oct 17, 2024
b39d5f5
fix inbound_number_dao
Oct 17, 2024
6dba25a
fix inbound_number_dao
Oct 17, 2024
9a98db1
fix inbound_number_dao
Oct 17, 2024
6bc20ac
revert and try again
Oct 17, 2024
ce007a6
down to line 17
Oct 17, 2024
0015df3
down to line 33
Oct 17, 2024
f4f6312
Merge pull request #1355 from GSA/notify-api-1351
terrazoon Oct 17, 2024
963acac
down to line 44
Oct 17, 2024
4dd718d
merge from main
Oct 17, 2024
bc9ff74
fix update
Oct 17, 2024
c818bac
start InboundSmsDao
Oct 17, 2024
608f1ec
start InboundSmsDao
Oct 17, 2024
672fb36
start InboundSmsDao
Oct 17, 2024
0f1d70f
start InboundSmsDao
Oct 17, 2024
af22cbc
start InboundSmsDao
Oct 17, 2024
dd4bf2a
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
80d0553
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
cfce20f
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
3c3fb8e
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
6f29a0e
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
be56461
fix delete_inbound_sms_older_than_retention and dao_get_inbound_sms_b…
Oct 17, 2024
ea32db8
revert pagination fix
Oct 17, 2024
fdc494e
fix delete
Oct 18, 2024
78aab59
fix first paginate method
Oct 18, 2024
3b651a2
fix first paginate method
Oct 18, 2024
c36a1ba
fix first paginate method
Oct 18, 2024
f207b65
fix first paginate method
Oct 18, 2024
001c16b
fix first paginate method
Oct 18, 2024
cf3e99c
fix first paginate method
Oct 18, 2024
5716c86
fix first paginate method
Oct 18, 2024
fed5a84
fix first paginate method
Oct 18, 2024
f12f6b9
fix first paginate method
Oct 18, 2024
697ea84
fix first paginate method
Oct 18, 2024
c859c5c
fix first paginate method
Oct 18, 2024
73dbca1
fix first paginate method
Oct 18, 2024
5e8efac
fix first paginate method
Oct 18, 2024
a3b2b19
fix first paginate method
Oct 18, 2024
2b1e83c
fix first paginate method
Oct 18, 2024
567a16f
fix first paginate method
Oct 18, 2024
9b965ae
fix first paginate method
Oct 18, 2024
8d80b2b
fix first paginate method
Oct 18, 2024
d8375ec
fix first paginate method
Oct 18, 2024
cf07818
fix complaint_dao
Oct 18, 2024
f633230
fix pagination
Oct 21, 2024
5e3e373
fix complaint_dao
Oct 21, 2024
3d7ab71
fix complaint_dao
Oct 21, 2024
38a2a87
fix test code
Oct 21, 2024
a3a5abc
fix test code
Oct 21, 2024
83251ba
fix test code
Oct 21, 2024
261ea6f
fix commented out code
Oct 21, 2024
4c891de
Nonce stuff added.
xlorepdarkhelm Oct 11, 2024
0b648c9
Fixed tests
xlorepdarkhelm Oct 21, 2024
f77e73e
increase code coverage to 95%
Oct 22, 2024
ac03cde
add provider tasks tests
Oct 22, 2024
571e91b
add provider tasks tests
Oct 22, 2024
749d1ac
add provider tasks tests
Oct 22, 2024
f2dec7e
add provider tasks tests
Oct 22, 2024
4b09a2c
add provider tasks tests
Oct 22, 2024
697c8ed
add provider tasks tests
Oct 22, 2024
b07af91
add provider tasks tests
Oct 22, 2024
01c811e
add provider tasks tests
Oct 22, 2024
85219bf
add provider tasks tests
Oct 22, 2024
3a04836
add provider tasks tests
Oct 22, 2024
205a1da
add provider tasks tests
Oct 22, 2024
5d72b57
add provider tasks tests
Oct 22, 2024
2344516
add provider tasks tests
Oct 22, 2024
c2be180
test read_s3_file
Oct 23, 2024
f359736
ugh
Oct 23, 2024
ed86cd4
try again
Oct 23, 2024
d99508d
try again
Oct 23, 2024
b94f2c9
try again
Oct 23, 2024
b68824c
add test for populate_go_live
Oct 23, 2024
3b12c0d
add test for populate_go_live
Oct 23, 2024
bc5ba1d
add test for populate_go_live
Oct 23, 2024
c88485a
add test for populate_go_live
Oct 23, 2024
5f304bb
add test for populate_go_live
Oct 23, 2024
e6efe80
add test for populate_go_live
Oct 23, 2024
b59a71e
add test for populate_go_live
Oct 23, 2024
430dd37
add test for populate_go_live
Oct 23, 2024
258f280
fix flake8
Oct 23, 2024
641dede
add threadpoolexecutor test
Oct 23, 2024
93ea905
raise code coverage to 94%
Oct 23, 2024
2d22a44
Merge pull request #1365 from GSA/USN-COMPLY-50-Verify_Nonce_for_Invite
ccostino Oct 24, 2024
2a05cb2
code review feedback
Oct 24, 2024
f6e2754
merge from main
Oct 24, 2024
2121c4e
add upload test
Oct 24, 2024
b238230
add upload test
Oct 24, 2024
d73f6fc
fix flake8
Oct 24, 2024
4c72570
change test file name
Oct 24, 2024
7c9963d
change test file name
Oct 24, 2024
a7a3a2d
change test file name
Oct 24, 2024
af07a7b
change test file name
Oct 24, 2024
a0b66f4
change test file name
Oct 24, 2024
4311403
Scrubbing log records with the formatter.
xlorepdarkhelm Oct 24, 2024
3d63ccc
change test file name
Oct 24, 2024
0bc0730
test exception block in get_job_from_s3
Oct 24, 2024
6e78bb4
add more tests
Oct 24, 2024
4120a65
fix flake8
Oct 24, 2024
1986142
add tests for get_job_and_metadata
Oct 24, 2024
f1e851d
fix
Oct 24, 2024
d5cc8b2
Bump werkzeug from 3.0.3 to 3.0.6
dependabot[bot] Oct 26, 2024
10eeb0c
add statistics test
Oct 28, 2024
7a2562a
add organization rest test
Oct 28, 2024
5c51530
fix test
Oct 28, 2024
8421822
add logging test
Oct 28, 2024
987a31a
add schema validation test
Oct 28, 2024
0ded5fa
fix tests
Oct 28, 2024
54cce40
more s3 tests
Oct 28, 2024
851ce23
more s3 tests
Oct 28, 2024
f544e73
Merge pull request #1375 from GSA/notify-api-1326
stvnrlly Oct 28, 2024
a938510
code review feedback
Oct 28, 2024
385f325
merge from main
Oct 28, 2024
616f6be
Merge pull request #1380 from GSA/dependabot/pip/werkzeug-3.0.6
terrazoon Oct 28, 2024
bd7d072
resolve pipaudit error
Oct 28, 2024
2d2661f
Merge pull request #1362 from GSA/notify-api-1320
terrazoon Oct 28, 2024
829d902
code review feedback
Oct 28, 2024
4d3ab78
merge from main
Oct 28, 2024
50b90c9
Merge pull request #1363 from GSA/notify-api-1323
terrazoon Oct 28, 2024
9638e2b
code review feedback
Oct 28, 2024
1c04291
merge from main
Oct 28, 2024
f782f1e
Merge pull request #1360 from GSA/notify-api-1321
terrazoon Oct 28, 2024
9aa8aa8
merge from main
Oct 28, 2024
61cf576
hmmm
Oct 28, 2024
4d276ad
hmmm
Oct 28, 2024
600e593
merge from main
Oct 29, 2024
e5117bb
merge from main
Oct 29, 2024
c85d351
Merge pull request #1364 from GSA/notify-api-1322
stvnrlly Oct 29, 2024
0b7079e
code review feedback
Oct 29, 2024
99c199b
Merge pull request #1376 from GSA/notify-api-1351
ccostino Oct 29, 2024
57df2f6
code review feedback
Oct 29, 2024
c0ed18c
merge from main
Oct 29, 2024
dc59937
Using re.compile().
xlorepdarkhelm Oct 30, 2024
0843134
Black.
xlorepdarkhelm Oct 30, 2024
50812b5
Attempting to fix security vulnerability that was found.
xlorepdarkhelm Oct 30, 2024
c3e249a
Compiling email regex.
xlorepdarkhelm Oct 30, 2024
7c17427
Black
xlorepdarkhelm Oct 30, 2024
a0e8828
Merge pull request #1379 from GSA/API-1328_Logging_Formatter_With_Scrub
ccostino Oct 30, 2024
05e268a
Merge pull request #1374 from GSA/notify-api-1324
terrazoon Oct 30, 2024
625f25f
reduce number of pool connections
Oct 31, 2024
38c566e
Merge pull request #1384 from GSA/reduce_connections
xlorepdarkhelm Nov 1, 2024
5141612
Increase Celery resources
ccostino Nov 4, 2024
165042b
Merge pull request #1387 from GSA/increase-worker-resources
stvnrlly Nov 4, 2024
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
10 changes: 5 additions & 5 deletions .ds.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
"filename": "tests/app/aws/test_s3.py",
"hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747",
"is_verified": false,
"line_number": 29,
"line_number": 40,
"is_secret": false
}
],
Expand Down Expand Up @@ -239,7 +239,7 @@
"filename": "tests/app/dao/test_services_dao.py",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 265,
"line_number": 289,
"is_secret": false
}
],
Expand All @@ -249,15 +249,15 @@
"filename": "tests/app/dao/test_users_dao.py",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 52,
"line_number": 69,
"is_secret": false
},
{
"type": "Secret Keyword",
"filename": "tests/app/dao/test_users_dao.py",
"hashed_secret": "f2c57870308dc87f432e5912d4de6f8e322721ba",
"is_verified": false,
"line_number": 176,
"line_number": 199,
"is_secret": false
}
],
Expand Down Expand Up @@ -384,5 +384,5 @@
}
]
},
"generated_at": "2024-09-27T16:42:53Z"
"generated_at": "2024-10-28T20:26:27Z"
}
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
NOTIFY_E2E_TEST_PASSWORD: ${{ secrets.NOTIFY_E2E_TEST_PASSWORD }}
- name: Check coverage threshold
# TODO get this back up to 95
run: poetry run coverage report -m --fail-under=91
run: poetry run coverage report -m --fail-under=94

validate-new-relic-config:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test: ## Run tests and create coverage report
poetry run coverage run --omit=*/migrations/*,*/tests/* -m pytest --maxfail=10

## TODO set this back to 95 asap
poetry run coverage report -m --fail-under=91
poetry run coverage report -m --fail-under=94
poetry run coverage html -d .coverage_cache

.PHONY: py-lock
Expand Down
24 changes: 6 additions & 18 deletions app/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def get_s3_resource():
return s3_resource


def _get_bucket_name():
return current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]


def list_s3_objects():

bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]
bucket_name = _get_bucket_name()
s3_client = get_s3_client()
# Our reports only support 7 days, but pull 8 days to avoid
# any edge cases
Expand Down Expand Up @@ -476,23 +480,7 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number):

set_job_cache(job_cache, f"{job_id}_personalisation", extract_personalisation(job))

# If we can find the quick dictionary, use it
if job_cache.get(f"{job_id}_personalisation") is not None:
personalisation_to_return = job_cache.get(f"{job_id}_personalisation")[0].get(
job_row_number
)
if personalisation_to_return:
return personalisation_to_return
else:
current_app.logger.warning(
f"Was unable to retrieve personalisation from lookup dictionary for job {job_id}"
)
return {}
else:
current_app.logger.error(
f"Was unable to construct lookup dictionary for job {job_id}"
)
return {}
return job_cache.get(f"{job_id}_personalisation")[0].get(job_row_number)


def get_job_metadata_from_s3(service_id, job_id):
Expand Down
4 changes: 3 additions & 1 deletion app/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
# This is the default but just for doc sake
# there may come a time when increasing this helps
# with job cache management.
max_pool_connections=10,
# max_pool_connections=10,
# Reducing to 4 connections due to BrokenPipeErrors
max_pool_connections=4,
)


Expand Down
79 changes: 2 additions & 77 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
dao_create_or_update_annual_billing_for_year,
set_default_free_allowance_for_service,
)
from app.dao.fact_billing_dao import (
delete_billing_data_for_service_for_day,
fetch_billing_data_for_day,
get_service_ids_that_need_billing_populated,
update_fact_billing,
)
from app.dao.jobs_dao import dao_get_job_by_id
from app.dao.organization_dao import (
dao_add_service_to_organization,
Expand Down Expand Up @@ -63,7 +57,7 @@
TemplateHistory,
User,
)
from app.utils import get_midnight_in_utc, utc_now
from app.utils import utc_now
from notifications_utils.recipients import RecipientCSV
from notifications_utils.template import SMSMessageTemplate
from tests.app.db import (
Expand Down Expand Up @@ -167,6 +161,7 @@ def purge_functional_test_data(user_email_prefix):
delete_model_user(usr)


# TODO maintainability what is the purpose of this command? Who would use it and why?
@notify_command(name="insert-inbound-numbers")
@click.option(
"-f",
Expand All @@ -175,7 +170,6 @@ def purge_functional_test_data(user_email_prefix):
help="""Full path of the file to upload, file is a contains inbound numbers, one number per line.""",
)
def insert_inbound_numbers_from_file(file_name):
# TODO maintainability what is the purpose of this command? Who would use it and why?

current_app.logger.info(f"Inserting inbound numbers from {file_name}")
with open(file_name) as file:
Expand All @@ -195,50 +189,6 @@ def setup_commands(application):
application.cli.add_command(command_group)


@notify_command(name="rebuild-ft-billing-for-day")
@click.option("-s", "--service_id", required=False, type=click.UUID)
@click.option(
"-d",
"--day",
help="The date to recalculate, as YYYY-MM-DD",
required=True,
type=click_dt(format="%Y-%m-%d"),
)
def rebuild_ft_billing_for_day(service_id, day):
# TODO maintainability what is the purpose of this command? Who would use it and why?

"""
Rebuild the data in ft_billing for the given service_id and date
"""

def rebuild_ft_data(process_day, service):
deleted_rows = delete_billing_data_for_service_for_day(process_day, service)
current_app.logger.info(
f"deleted {deleted_rows} existing billing rows for {service} on {process_day}"
)
transit_data = fetch_billing_data_for_day(
process_day=process_day, service_id=service
)
# transit_data = every row that should exist
for data in transit_data:
# upsert existing rows
update_fact_billing(data, process_day)
current_app.logger.info(
f"added/updated {len(transit_data)} billing rows for {service} on {process_day}"
)

if service_id:
# confirm the service exists
dao_fetch_service_by_id(service_id)
rebuild_ft_data(day, service_id)
else:
services = get_service_ids_that_need_billing_populated(
get_midnight_in_utc(day), get_midnight_in_utc(day + timedelta(days=1))
)
for row in services:
rebuild_ft_data(day, row.service_id)


@notify_command(name="bulk-invite-user-to-service")
@click.option(
"-f",
Expand Down Expand Up @@ -472,31 +422,6 @@ def associate_services_to_organizations():
current_app.logger.info("finished associating services to organizations")


@notify_command(name="populate-service-volume-intentions")
@click.option(
"-f",
"--file_name",
required=True,
help="Pipe delimited file containing service_id, SMS, email",
)
def populate_service_volume_intentions(file_name):
# [0] service_id
# [1] SMS:: volume intentions for service
# [2] Email:: volume intentions for service

# TODO maintainability what is the purpose of this command? Who would use it and why?

with open(file_name, "r") as f:
for line in itertools.islice(f, 1, None):
columns = line.split(",")
current_app.logger.info(columns)
service = dao_fetch_service_by_id(columns[0])
service.volume_sms = columns[1]
service.volume_email = columns[2]
dao_update_service(service)
current_app.logger.info("populate-service-volume-intentions complete")


@notify_command(name="populate-go-live")
@click.option(
"-f", "--file_name", required=True, help="CSV file containing live service data"
Expand Down
34 changes: 22 additions & 12 deletions app/dao/annual_billing_dao.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import current_app
from sqlalchemy import select, update

from app import db
from app.dao.dao_utils import autocommit
Expand Down Expand Up @@ -26,42 +27,51 @@ def dao_create_or_update_annual_billing_for_year(


def dao_get_annual_billing(service_id):
return (
AnnualBilling.query.filter_by(
stmt = (
select(AnnualBilling)
.filter_by(
service_id=service_id,
)
.order_by(AnnualBilling.financial_year_start)
.all()
)
return db.session.execute(stmt).scalars().all()


@autocommit
def dao_update_annual_billing_for_future_years(
service_id, free_sms_fragment_limit, financial_year_start
):
AnnualBilling.query.filter(
AnnualBilling.service_id == service_id,
AnnualBilling.financial_year_start > financial_year_start,
).update({"free_sms_fragment_limit": free_sms_fragment_limit})
stmt = (
update(AnnualBilling)
.filter(
AnnualBilling.service_id == service_id,
AnnualBilling.financial_year_start > financial_year_start,
)
.values({"free_sms_fragment_limit": free_sms_fragment_limit})
)
db.session.execute(stmt)
db.session.commit()


def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None):
if not financial_year_start:
financial_year_start = get_current_calendar_year_start_year()

return AnnualBilling.query.filter_by(
stmt = select(AnnualBilling).filter_by(
service_id=service_id, financial_year_start=financial_year_start
).first()
)
return db.session.execute(stmt).scalars().first()


def dao_get_all_free_sms_fragment_limit(service_id):
return (
AnnualBilling.query.filter_by(
stmt = (
select(AnnualBilling)
.filter_by(
service_id=service_id,
)
.order_by(AnnualBilling.financial_year_start)
.all()
)
return db.session.execute(stmt).scalars().all()


def set_default_free_allowance_for_service(service, year_start=None):
Expand Down
32 changes: 23 additions & 9 deletions app/dao/complaint_dao.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import timedelta

from flask import current_app
from sqlalchemy import desc
from sqlalchemy import desc, func, select

from app import db
from app.dao.dao_utils import autocommit
from app.dao.inbound_sms_dao import Pagination
from app.models import Complaint
from app.utils import get_midnight_in_utc

Expand All @@ -15,23 +16,36 @@ def save_complaint(complaint):


def fetch_paginated_complaints(page=1):
return Complaint.query.order_by(desc(Complaint.created_at)).paginate(
page=page, per_page=current_app.config["PAGE_SIZE"]
page_size = current_app.config["PAGE_SIZE"]
total_count = db.session.scalar(select(func.count()).select_from(Complaint))
offset = (page - 1) * page_size
stmt = (
select(Complaint)
.order_by(desc(Complaint.created_at))
.offset(offset)
.limit(page_size)
)
result = db.session.execute(stmt).scalars().all()
pagination = Pagination(result, page=page, per_page=page_size, total=total_count)
return pagination


def fetch_complaints_by_service(service_id):
return (
Complaint.query.filter_by(service_id=service_id)
stmt = (
select(Complaint)
.filter_by(service_id=service_id)
.order_by(desc(Complaint.created_at))
.all()
)
return db.session.execute(stmt).scalars().all()


def fetch_count_of_complaints(start_date, end_date):
start_date = get_midnight_in_utc(start_date)
end_date = get_midnight_in_utc(end_date + timedelta(days=1))

return Complaint.query.filter(
Complaint.created_at >= start_date, Complaint.created_at < end_date
).count()
stmt = (
select(func.count())
.select_from(Complaint)
.filter(Complaint.created_at >= start_date, Complaint.created_at < end_date)
)
return db.session.execute(stmt).scalar() or 0
Loading
Loading