Commit Graph

147 Commits

Author SHA1 Message Date
Leo Hemsted
c7c47a029f remove slow join from template-last-use query 2017-04-10 17:33:31 +01:00
Rebecca Law
7ba9b1d261 Update the update statements with the updated_at and sent_at timestamps. 2017-04-07 15:21:27 +01:00
Rebecca Law
2d9a449d1e Changed styling 2017-04-07 14:36:00 +01:00
Rebecca Law
61d228e797 Remove test filter 2017-04-07 11:22:03 +01:00
Rebecca Law
50a5bedcbf Refactor update to notifcations to be a bulk update. This is much better for performance. 2017-04-07 10:59:12 +01:00
Rebecca Law
d36eec652d Removed unused code in services_dao, which led to looking some of the queries.
By removing the join to services for the fetch_stats_by_date_range_for_all_services and dao_fetch_todays_stats_for_all_services
queries we get a 30% and 25% performance improvement.
2017-03-01 17:12:29 +00:00
Imdad Ahad
eafe8269ef Simplify dao method and update tests and fixtures 2017-02-24 13:39:58 +00:00
Imdad Ahad
60f63a30db Add dao method to get count of slow delivery notifications by provider 2017-02-24 12:23:39 +00:00
Rebecca Law
da07f9dde8 Performance improvement to the template usage query. 2017-02-02 09:45:06 +00:00
Chris Hill-Scott
45b505f379 Split out handling of BST queries for reuse 2017-01-31 14:16:35 +00:00
Chris Hill-Scott
56ba653f48 Add endpoint for breakdown of activity by month
This endpoint will eventualy replace the weekly breakdown one. By month
for a given financial year is better, because it gives us consistency
with the breakdown of financial usage (and eventually consistency with
the template usage).

The code to do this is a bit convoluted, in order to fill out the counts
for months and statuses where we don’t have notifications.

This will make the admin side of this easier, because we can rely on
there always being numbers available. The admin side will deal with
summing the statuses (eg `temporary-failure` > `failed`) because this
is presentational.

This commit also modifies the usage count to use `.between()` for
consistency.
2017-01-31 14:16:34 +00:00
Imdad Ahad
cc7bf45766 Move notification retrieval logic into perf platform client and dont filter by status 2017-01-27 16:39:01 +00:00
Rebecca Law
c87d0a37f3 Refactor the get_midnight functions to return the date in UTC but for the localised time.
So in June when we are in BST June 16, 00:00 BST => June 15 23:00 UTC
2017-01-27 15:57:25 +00:00
Imdad Ahad
b6cc5b00ba Add dao method to get sms and email notification counts for a date range and previous day 2017-01-27 12:22:36 +00:00
Rebecca Law
cefa00b3fa Merge branch 'master' into fix-billable-units-query
Conflicts:
	tests/app/dao/test_notification_dao.py
2017-01-10 14:24:08 +00:00
Rebecca Law
ffa54821d9 The previous commit was failing the test in the docker container.
My local version of postgres is 9.6 and docker is using 9.5 (so I am ahead).
However, we found a more simple solution that works for both versions.
2017-01-10 11:21:33 +00:00
Rebecca Law
a1d8ca9364 Use a variable for the massive month date conversion.
Document the massive date function.
Document the get_april_fools function.
2017-01-09 15:34:24 +00:00
Rebecca Law
88e1e58f24 Fix the get_april_fools method to use March 31 23:00 as the timestamp.
The financial year start April 1, 00:00 BST and our dates are stored as UTC.
Added a test for get_april_fools.
Added some test more test data for get_billable_unit_count_per_month.
2017-01-09 14:54:25 +00:00
Rebecca Law
17767dfa62 Calculating the billing units per month was taking too long for a services with a lot of data.
The query now does the sum per month.
2017-01-09 11:13:24 +00:00
Rebecca Law
c6df84a8f3 Merge branch 'master' into do-not-write-test-data-to-the-history-table
Conflicts:
	app/dao/notifications_dao.py
2016-12-21 09:57:17 +00:00
Leo Hemsted
a197754906 add new provider_details_history table
* to be used for auditing changes to provider details
* not hooked in yet
* also made provider_details.active non-nullable. set all existing null
  provider_details.active to FALSE. none are set false on live so
  should hopefully be fairly uncontroversial
* refactored NotificationHistory.from_notification and update from noti
  to share with provider_details_history
2016-12-19 17:13:10 +00:00
Martyn Inglis
0f37824b0c Ensure updates on a research mode service or test key don't touch the history table
- note this is an unexpectedly big change.
- When we create a service we pass the service id to the persist method. This means that we don't have the service available to check if in research mode.
- All calling methods (expecting the one where we use the notify service) have the service available. So rather than reload it I changed the method signature to pass the service, not the ID to persist.
- Touches a few places.

Note this means that the update or create methods will fall over on a null service. But this seems correct.

Goes back to the story which we need to play to make the service available as the API user so that the need to load and pass around services is minimised.
2016-12-19 16:45:18 +00:00
Martyn Inglis
35aa888658 When creating a notification don't create a history row if:
1) research mode service
2) test mode key

Stop test data getting into history and subsequently into stats and so on.
2016-12-19 13:57:06 +00:00
Rebecca Law
40dca7f9c7 Changed the timeout notications update query to set notifications still in created to a technical-failure and notifications still in sending or pending are set to a temporary-failure 2016-12-16 11:40:58 +00:00
Rebecca Law
a6da1ac864 Update the timeout notifications to use technical-failure rather than temporary failure 2016-12-14 16:18:43 +00:00
Imdad Ahad
431ec8acad Retrieve notifications by reference:
* Use get_notifications instead (references may not be unique)
2016-12-12 18:04:20 +00:00
Imdad Ahad
d4d733f997 Add dao method to get notification by reference with tests 2016-12-07 14:08:22 +00:00
Paul Craig
4082d38c0c Test invalid older_than, template_types, and bad ids
Come up with some simple tests in the routes, just to see we get
back what we expect as errors.
2016-11-28 15:56:59 +00:00
Paul Craig
df7450698c Test returning notifications by "?status=failed"
Check that all failure states are returned by asking for
notifications of type "failure".
2016-11-25 15:26:40 +00:00
Paul Craig
effbd315e0 Create 'v2' get notifications route
Return multiple notifications for a service.
Choosing a page_size or a page_number is no longer allowed.
Instead, there is a `next` link included with will return the
next {default_page_size} notifications in the sequence.

Query parameters accepted are:

 - template_type: filter by specific template types
 - status: filter by specific statuses
 - older_than: return a chronological list of notifications older
   than this one. The notification with the id that is passed in
   is _not_ returned.

Note that both `template_type` and `status` can accept multiple
parameters.  Thus it is possible to call
`/v2/notifications?status=created&status=sending&status=delivered`
2016-11-25 15:23:03 +00:00
Paul Craig
f1ea39d4c0 Simplify _filter_query() function 2016-11-25 11:23:20 +00:00
Rebecca Law
8cf2fc72a8 - Refactor version 1 of post notificaitons to use the common persist_notificaiton and send_notification_to_queue methods.
- It would be nice to refactor the send_sms and send_email tasks to use these common functions as well, that way I can get rid of the new Notifications.from_v2_api_request method.
- Still not happy with the format of the errors. Would like to find a happy place, where the message is descript enough that we do not need external documentation to explain the error. Perhaps we still only need documentation to explain the trial mode concept.
2016-10-28 17:10:00 +01:00
Chris Hill-Scott
826eaaf5b3 Remove unecessary brackets when returning tuple 2016-10-04 13:05:15 +01:00
Chris Hill-Scott
219d5943c1 Return iterator from query for speed 2016-10-04 13:04:42 +01:00
Chris Hill-Scott
d352c0eed9 Really fix the timezones
Two main changes:

- uses `astimezone` instead of `replace` because `replace` doesn’t
  handle daylight savings time correctly [1]
- create the notifications one second before midnight in BST, because
  midnight is actually counted as being start of the _next_ day, month,
  etc

1. http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/#add-timezone-localize
2016-10-04 13:00:37 +01:00
Chris Hill-Scott
76d5f14952 Filter unbillable notifications before calculating
Notifications with a `billable_units` count of `0` wont have any effect
on the result, but including them in the query will slow down the
grouping and summing of the results because it’ll have to loop over more
rows.
2016-10-04 09:51:41 +01:00
Chris Hill-Scott
7abe40b506 Make billing year aware of British Summer Time
April 1st is in British summer time, ie 1hr ahead of UTC. The database
stores everything in UTC, so for accurate comparisions we need to make
sure that `get_financial_year()` returns a UTC, datetime-aware
timestamp that is 1hr ahead of midnight.

This also means that when we group notifications by month, the months
need to be in BST. So the line between one year and another is actually
01:00 on April 1st, _not_ 00:00 on April 1st.

There’s no way we’ve found to do this in SQLAlchemy or raw Postgres,
especially because we don’t store the timestamps with a timezone in the
database.

So the grouping and summing of the notifications has to be done in
Python.
2016-10-04 09:08:27 +01:00
Chris Hill-Scott
6a5e947220 Add DAO for getting billable units/financial year
In order to invoice people we need to know how many text message
fragments they’ve sent per month.

This should be per (government) financial year, ie April 1st to April
1st because we’ll only ever show a page for one year (because the
250,000 allowance is topped up at the start of every financial year).

This commit only does the DAO bit, not the REST bit.
2016-10-03 15:50:54 +01:00
Chris Hill-Scott
f9f3bb8370 Make DAO optionally return test key notifications
Developers need visibility of what their integration is doing within
the app. This includes notifications sent with a test key.

This commit adds an optional, defaults-to-false parameter to include
notifications sent from a test API key when getting notifications.
2016-09-23 10:37:26 +01:00
Chris Hill-Scott
b36d0ff552 Make indentation more sensible
Starting arguments on their own line and putting the closing parenthesis
on it’s own line because any subsequent changes to the arguments diff
cleanly (ie without touching any other lines).
2016-09-23 09:43:25 +01:00
Martyn Inglis
7c16294b75 Exclude test key notifications from:
- get all notifications by service
- template usage
- most recently used templates

Ensures that the dashboard shows no test key data. Supplements: https://github.com/alphagov/notifications-api/pull/677 which excludes CSV data. This branches from that so is dependant.
2016-09-16 13:47:09 +01:00
Martyn Inglis
9f02e94f38 Ensure that querying for notifications does not return CSV data unless:
- It's explicitly asked for AND
- you are using a LIVE key

Team and Test keys CANNOT retrieve CSV/Job derived notifications
2016-09-15 15:59:02 +01:00
minglis
c67c819f0a Merge pull request #671 from alphagov/statsd-time-taken-to-be-delivered
Statsd time taken to be delivered
2016-09-14 14:53:01 +01:00
Rebecca Law
339d33723b Added updated_at to the update query 2016-09-13 17:35:23 +01:00
Rebecca Law
cb51c69503 Update the timeout_notifications scheduled tasks.
We found that if the notifications were in created or pending they are not purged from notifications.
- New bulk update method to set all notificaitons with:
  -  a status = created|sending|pending to temporary-failure
  - and is older then today minus SENDING_NOTIFICATIONS_TIMEOUT_PERIOD (in seconds)
- the scheduled task to timeout notifications use the new bulk update query.
- the task will be more efficient
2016-09-13 16:42:53 +01:00
Martyn Inglis
8541f6adcd Change DAO to return the notification from an update, not Boolean
- tests reflect this
2016-09-13 12:29:40 +01:00
Martyn Inglis
dde22bc58b Implemented deleted notification if SQS write fails 2016-09-08 16:00:18 +01:00
Martyn Inglis
893164ae40 Removed contented updates the notifications stats table
- As before this is now driven from the notifications history table

- Removed from updates and create
- Signatures changes to removed unused params hits many files
- Also potential issue around rate limiting - we used to get the number sent per day from the stats table - which was a single row lookup, now we have to count this. This applies to EVERY API CALL. Probably not a good thing and should be addressed urgently.
2016-08-25 11:55:38 +01:00
Martyn Inglis
708f566c24 Removed updates to the provider stats table
- again these new come from the notifications history table
- We update this when we sent a notification, so removed from celery tasks
- tests removed also
2016-08-25 10:33:12 +01:00
Martyn Inglis
84ea173ced Removed updates to templates statistics
- on create notification we updated the templates stats to record the usage.
- this is now based on notification history
- this update and associated tests are now removed,
2016-08-25 10:12:39 +01:00