Commit Graph

49 Commits

Author SHA1 Message Date
Alex Janousek
86458fe1a0 Added changes to support extra day in activity chart (#1928) 2025-08-25 13:28:54 -07:00
Kenneth Kehl
58a8b51f59 more input checking 2025-06-26 10:35:46 -07:00
Cliff Hill
dbfd99cb65 Added fields to endpoint.
Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
2024-08-28 16:53:00 -04:00
Cliff Hill
5d5ff72ca2 Adding the new fields for template stats.
Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
2024-08-21 11:10:31 -04:00
Kenneth Kehl
ebdb78e52e reformat up to latest version of black 2024-04-01 15:12:33 -07:00
Kenneth Kehl
1ecb747c6d reformat 2023-08-29 14:54:30 -07:00
Steven Reilly
ff4190a8eb Remove letters-related code (#175)
This deletes a big ol' chunk of code related to letters. It's not everything—there are still a few things that might be tied to sms/email—but it's the the heart of letters function. SMS and email function should be untouched by this.

Areas affected:

- Things obviously about letters
- PDF tasks, used for precompiling letters
- Virus scanning, used for those PDFs
- FTP, used to send letters to the printer
- Postage stuff
2023-03-02 20:20:31 -05:00
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
Pea Tyczynska
95deb5a52f Move DATETIME_FORMAT from app to app.utils
To avoid cyclical import issues
2020-12-18 17:39:35 +00:00
Rebecca Law
8445775be0 Remove unused methods.
A new endpoint to return the last date a template was used which means the old endpoint can be removed.
2020-02-07 15:50:54 +00:00
Rebecca Law
09f798ea14 Fix None type error in last-used endpoint. 2020-02-06 11:39:40 +00:00
Rebecca Law
dec42b06cc Simplify the code in the query.
The date in the notifications table should always be the most recent date for the template.
Removed the template_type param for the query as well.
Simplified the tests.
2020-02-05 16:43:17 +00:00
Rebecca Law
3a32c35dd2 Added a new endpoint to return the last used date for a template.
The existing endpoint returned a whole notification for the last time the template was used. But this only takes into account data in the last week. This new methods allows us to be specific about when the template was last used if ever but looking into the ft_notification_status table as well.
2020-02-05 13:03:54 +00:00
Pea Tyczynska
3ce0024eec Remove unused functions for getting template statistics 2019-01-15 12:15:20 +00:00
Pea Tyczynska
52831813d8 Change template statistics endpoint to use fact_notification_status_dao 2019-01-15 11:55:45 +00:00
Leo Hemsted
d2d267e5d8 rename limit_days to whole_days on template statistics endpoint
and return data for one more day.

we're not really limiting to 7 days - we're returning 7 entire days,
plus whatever time has elapsed since midnight today. I felt it would be
best to rename the variable to `whole_days` to imply that it's not
"limit this data set to seven days", it's "give me at least seven days".

the endpoint is backwards compatible so we can rename the variable on the front-end later
2018-07-10 15:04:35 +01:00
Rebecca Law
db5663e429 Improve the performance of the query to get the last template usage.
By adding the service id, the query performance has improved greatly. It went from 6200ms to 0.04ms.
This should stop the 500s when a template is deleted.
2018-07-02 17:17:38 +01:00
Leo Hemsted
e35c1bde77 ensure tests have test_ prefix
also remove unused code branch

thanks, coverage!
2018-04-30 13:23:02 +01:00
Leo Hemsted
0efa223fb2 rename days_ago to midnight_n_days_ago
also add some more timezone boundary tests and minor code cleanup
2018-04-30 11:50:56 +01:00
Leo Hemsted
310b8eda4c refactor dao_get_template_usage now that limit_days no longer used
doesnt need to have any conditional stuff now - always requires a date
2018-04-30 11:13:21 +01:00
Leo Hemsted
7dc34fc3a4 set 0'd dict in redis if no data exists in redis
to get the data for a day can be reasonably slow (a few hundred
milliseconds), and if someone's viewing a service with no activity we
don't want to do that query seven times every two seconds. So if there
is no data in redis, when we get the data out of the database, we
should put it in redis so we can just grab it from there next time.

This'll happen in two cases:
* redis data is deleted
* the service sent no messages that day

additionally, make sure that we convert nicely from redis' return
values (ascii strings) to unicode keys and integer counts.
2018-04-30 11:13:21 +01:00
Leo Hemsted
85fd7c3869 add new tests for template statistics 2018-04-30 11:13:21 +01:00
Leo Hemsted
9e8b6fd00d refactor template stats endpoint to read from new redis keys
New redis keys are partitioned per service per day. New process is as
follows:

* require a count of days to filter by. Currently admin always gives 7.
* for each day, check and see if there's anything in redis. There won't
  be if either a) redis is/was down or b) the service didn't send any
  notifications that day
  - if there isn't, go to the database and get a count out.
* combine all these stats together
* get the names/template types etc out of the DB at the end.
2018-04-30 11:13:21 +01:00
Leo Hemsted
67019fc5a1 add day filter to template usage dao, and remove NotificationHistory
we should be very careful with when we get data from
NotificationHistory - this should probably only be from scheduled
tasks. `dao_get_template_usage` is only called from the template
statistics rest endpoint, so shouldn't ever hit template history.

also, moved tests out to new file to break up the 2k test file a bit
2018-04-30 11:13:21 +01:00
Leo Hemsted
8e73961f65 add new redis template usage per day key
We've run into issues with redis expiring keys while we try and write
to them - short lived redis TTLs aren't really sustainable for keys
where we mutate the state. Template usage is a hash contained in redis
where we increment a count keyed by template_id each time a message is
sent for that template. But if the key expires, hincrby (redis command
for incrementing a value in a hash) will re-create an empty hash.

This is no good, as we need the hash to be populated with the last
seven days worth of data, which we then increment further. We can't
tell whether the hincrby created the key, so a different approach
entirely was needed:

* New redis key: <service_id>-template-usage-<YYYY-MM-DD>. Note: This
  YYYY-MM-DD is BTC time so it lines up nicely with ft_billing table
* Incremented to from process_notification - if it doesn't exist yet,
  it'll be created then.
* Expiry set to 8 days every time it's incremented to.

Then, at read time, we'll just read the last eight days of keys from
Redis, and sum them up. This works because we're only ever incrementing
from that one place - never setting wholesale, never recreating the
data from scratch. So we know that if the data is in redis, then it is
good and accurate data.

One thing we *don't* know and *cannot* reason about is what no key in
redis means. It could be either of:

* This is the first message that the service has sent today.
* The key was deleted from redis for some reason.

Since we set the TTL to so long, we'll never be writing to a key that
previously expired. But if there is a redis (or operator) error and the
key is deleted, then we'll have bad data - after any data loss we'll
have to rebuild the data.
2018-04-03 16:12:54 +01:00
Leo Hemsted
e692e06b7f add new template stat read pseudocode
we can't use this new code until the new redis keys have been popualted
for the last seven days
2018-04-03 16:12:25 +01:00
Rebecca Law
a308815793 We had an exception today caused by a slow running query.
This PR will optimize this query to use a more efficient index.

- Add notification_type to the dao_get_last_template_usage to optimize the query.
- Tested and analyzed query on production database with very significant results.
 Before:
                                                                                 QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.43..1711.35 rows=1 width=935) (actual time=21186.053..21186.053 rows=0 loops=1)
   ->  Index Scan Backward using ix_notifications_created_at on notifications  (cost=0.43..4607493.80 rows=2693 width=935) (actual time=21186.052..21186.052 rows=0 loops=1)
         Filter: (((key_type)::text <> 'test'::text) AND (template_id = 'xxxxxx'::uuid))
         Rows Removed by Filter: 8244071
 Planning time: 0.112 ms
 Execution time: 21186.082 ms
 After:
                                                     QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
 Limit  (cost=5323.10..5323.10 rows=1 width=935)
   ->  Sort  (cost=5323.10..5323.74 rows=258 width=935)
         Sort Key: created_at DESC
         ->  Index Scan using ix_notifications_template_id on notifications  (cost=0.56..5321.81 rows=258 width=935)
               Index Cond: (template_id = 'xxxxx'::uuid)
               Filter: (((key_type)::text <> 'test'::text) AND (notification_type = 'sms'::notification_type))
 Planning time: 1.102 ms
 Execution time: 0.584 ms
2018-03-16 14:12:59 +00:00
Ken Tsang
23ce36dc48 Update response to return is_precompiled_letter 2018-03-07 23:03:03 +00:00
Ken Tsang
7011b90bd4 Refactor is_precompiled_letter to model 2018-03-07 23:03:03 +00:00
Ken Tsang
28136734e4 Refactor to use is_precompiled_letter in letters/utils.py 2018-03-06 13:04:57 +00:00
Ken Tsang
bca858f4a8 Return precompiled_letter flag rather than hidden 2018-03-06 12:46:06 +00:00
Ken Tsang
564504bf97 Add template hidden field in response 2018-03-06 12:46:06 +00:00
Leo Hemsted
28d5f9b87f flake8 - remove unused imports and ensure they're always at the top of the file 2017-11-28 14:28:01 +00:00
Rebecca Law
bf6e671e96 The logs kept reporting a data error from the redis client. This would happen any time we set the cache with empty cache values.
Added a check for values before setting the cache
2017-08-22 14:59:50 +01:00
Imdad Ahad
f5e38a896c Update the last template usage query to check Notification table:
* Don't check the NotificationHistory table (this can cause a timeout)
* Check template actually exists first
2017-08-07 10:19:21 +01:00
Rebecca Law
53b7ad0961 Moved the cache key to the utils module.
Renamed the dao method.
2017-02-14 14:22:52 +00:00
Rebecca Law
681f52b691 Missed some print statements and unused imports. 2017-02-14 09:59:33 +00:00
Rebecca Law
2346634fec Some code clean up, removed print statements. 2017-02-14 09:54:37 +00:00
Rebecca Law
458adefcb8 Added a redis cache for the template usage stats.
Cache expires every 10 minutes, but will help with the every 2 second query, especially when a job is running.
There is some clean up and qa to do for this yet
2017-02-13 18:47:29 +00:00
Martyn Inglis
4e6da1ba55 Changed template stats for template id
- now returns the most recent notification history row for that template ID.
- contains all the required data for the use cases for that template
2016-08-22 14:35:04 +01:00
Martyn Inglis
b7476a1975 Removed the group by day aspects of template stats. Not needed. Grouped by template only. 2016-08-22 10:38:44 +01:00
Martyn Inglis
9eb559d4b2 Renamed the param to avoid shadowing 2016-08-18 15:29:56 +01:00
Martyn Inglis
ede7d0cbea Removed old endpoint.
Going to handle the migration in the clients.
2016-08-18 14:06:12 +01:00
Martyn Inglis
7a5acea71b New endpoint for template stats to use new query. Breaking change to the returned JSON, so adding on a different url. 2016-08-18 14:01:31 +01:00
Adam Shimali
b33312b855 Change endpoint responses where there are marshalling, unmarshalling
or param errors to raise invalid data exception. That will cause
those responses to be handled in by errors.py, which will log
the errors.

Set most of schemas to strict mode so that marshmallow will raise
exception rather than checking for errors in return tuple from load.

Added handler to errors.py for marshmallow validation errors.
2016-06-15 14:37:51 +01:00
Adam Shimali
3d55c47d1e Rest methods that explicitly return errors by pass the error handlers as
they do not raise exceptions.

Introduced a simple exception that contains error messages and status
code that can be used rather than return json + status code from rest
methods directly.

The handler in errors for this exception can then log the error before
returning json.
2016-06-13 13:50:35 +01:00
Leo Hemsted
e3d9dfad6e add template_statistics endpoint for specific template
`/service/<service_id>/template-statistics/<template_id>`
still requires service-id just to try and keep api tree cleaner
2016-06-07 14:18:42 +01:00
Adam Shimali
e4a5e3890a Corrected error message format 2016-04-04 14:51:56 +01:00
Adam Shimali
0d06be05e1 [WIP] Added dao method and rest endpoint for getting template
statistics by service.

Some cosmetic changes to imports.

Added fix for job rest not correctly returning errors.
2016-04-04 14:38:54 +01:00