Commit Graph

118 Commits

Author SHA1 Message Date
Katie Smith
da8eaaed44 Update letter data for usage-for-all-services report
Usage for all services is a platform admin report that groups letters by
postage. We want it to show `europe` and `rest-of-world` letters under a
single category of `international`, so this updates the query to do
that and to order appropriately.
2020-07-14 10:22:30 +01:00
Pea Tyczynska
9c4205c7c6 Remove statsd decorators from dao functions
This done so that we do not use statsd on our http endpoint.
We decided we do not need metrics that this gave us. If we
change our minds, we will add Prometheus-friendly decorators
instead in the future.
2020-07-07 18:02:24 +01:00
Rebecca Law
7b0a3c68cd Fix bug on organisation-usage page.
The dict is initialised for all live services, but if the organisation has trial mode services they cause a key error.
2020-02-27 13:52:02 +00:00
Rebecca Law
c91f37ff4c Change the updates to only look at today, and not yesterday. 2020-02-26 17:38:20 +00:00
Rebecca Law
f7a564a17c Add more realistic test
Add statsd
Fix imports
2020-02-26 11:21:33 +00:00
Rebecca Law
67c64a8715 Format the response to a more managable list.
Add a sort order
2020-02-25 17:34:03 +00:00
Rebecca Law
a2d18f8598 Update the organsition usage endpoint to use the new query.
This endpoint may need to change, but we'd like to see how this performs, so we'll test this with a real data set. Then come back to make sure the format is correct and check for missing tests for the endpoint,
2020-02-25 09:29:50 +00:00
Rebecca Law
b1b457eea0 Only return the usage data for live services.
The list of trail mode services is only for platform admins, therefore the usage isn't needed for that page.
2020-02-24 14:23:05 +00:00
Rebecca Law
18f272dc2b Add queries to handle returning usage for all services associated to a given organisation. 2020-02-24 11:28:42 +00:00
Rebecca Law
49533d7792 Fix typo in function name 2020-02-24 11:26:16 +00:00
Rebecca Law
009dcd0860 Update the fetch_monthly_billing_for year to only update ft_billing for the notification types the service as permission to send to. 2020-02-20 16:08:57 +00:00
Rebecca Law
ca010ac4cb Check service has permission to send notification type.
At the moment the check_permission boolean is always false.
Will set to true for usage pages
2020-02-20 13:27:39 +00:00
Leo Hemsted
0f6f2f1b91 split up _query_for_billing_data into three separate queries
the queries all return lots of columns, but each query has columns it
doesn't care about. eg emails don't have billable units or international
flag, letters don't have international flag, sms don't have a page count
etc. additionally, the query was grouping on things that never change,
like service id and notification type.

by making all of these literals (as in `select 1 as foo`) we see times
that are over 50% quicker for gov.uk email service.

Note: One of the tests changed because previously it involved emails and
sms with statuses that they could never be (eg returned-letter)
2020-02-19 13:12:01 +00:00
Rebecca Law
291c6d6dc9 Add statsd annotations for the fact table queries. 2020-02-18 14:33:17 +00:00
Leo Hemsted
d457db4164 make has_delete_task_run non-optional
just to ensure people think about the value of it when using the function
2019-12-03 14:19:14 +00:00
Leo Hemsted
d83827579e make ft billing nightly task only look at one table
follows same logic as the create_nightly_notification_status task, see previous commit
for logic
2019-12-03 14:19:13 +00:00
Leo Hemsted
913cf5e12d work out which table to get notification status data from
previously we checked notifications table, and if the results were
zero, checked the notification history table to see if there's data
in there. When we know that data isn't in notifications, we're still
checking. These queries take half a second per service, and we're
doing at least ten for each of the five thousand services we have in
notify. Most of these services have no data in either table for any
given day, and we can reduce the amount of queries we do by only
checking one table.

Check the data retention for a service, and then if the date is older
than the retention, get from history table.

NOTE: This requires that the delete tasks haven't run yet for the day!
If your retention is three days, this will look in the Notification
table for data from three days ago - expecting that shortly after the
task finishes, we'll delete that data.
2019-11-29 15:27:56 +00:00
Rebecca Law
db0d45966f Fix query to populate ft_billing table.
The group by for the query was wrong which would result in 2 rows with different totals but the same unique key, so the second row would update the first row. Meaning we had incorrect numbers for the billing data.
Because some of the data had null for the sent_by column, the select would turn the Null --> dvla, but that same function was not used in the group by. So any time we had missing sent_by data we would end up with 2 rows where one would overwrite the other.
2019-11-15 10:23:48 +00:00
Rebecca Law
e64ae321cf The sheet count was not calculated properly (it should be billable_units/notifications_sent).
And it turns out the sheet count is not required for the report. This PR takes out the columns to resolve the group by error.
2019-09-03 13:16:08 +01:00
Leo Hemsted
93e631221a use dates rather than datetimes when comparing with bst_date
bst_date is a date field. Comparing dates with datetimes in postgres
gets confusing and dangerous. See this example, where a date evaluates
as older than midnight that same day.

```
notification_api=# select '2019-04-01' >= '2019-04-01 00:00';
 ?column?
----------
 f
(1 row)
```

By only using dates everywhere, we reduce the chance of these bugs
happening
2019-09-02 11:56:56 +01:00
Leo Hemsted
5975ae2383 remove unneccessary duped lines
as per pr comments
2019-08-30 16:49:58 +01:00
Leo Hemsted
7313dbeb86 normalise join patterns across billing queries
select from service, join to org and ft_billing
2019-08-30 12:18:52 +01:00
Leo Hemsted
6f420cf066 explicitly join tables
from service, join organisation, the free_allowance_remainder subquery
and the ft_billing table. Being explicit reduces confusion about what
tables we're joining and how we're constraining those joins

also remove references to AnnualBilling since we've already got the
free sms allowance from the free_allowance_remainder subquery
2019-08-30 12:18:52 +01:00
Leo Hemsted
48e96f253b ensure fetch_sms_free_allowance_remainder always returns
if there are no rows for a service in ft_billing, we should still
return their allowance (with 0 fragments used).

To do this, we need to build the query starting from AnnualBilling and
joining onto FactBilling, rather than the other way round. Also, we
need to account for the possibility of the sums being null by
coalescing them to 0
2019-08-30 12:18:40 +01:00
Leo Hemsted
99a008e908 explicitly join annual_billing and remove reference to service.id
service.id isn't used in this query (both tables already have
service_id), and explicitly joining makes what we're doing more obvious
2019-08-30 12:18:00 +01:00
Leo Hemsted
741d75c3a9 use func.greatest rather than case statement
simplifies the query quite a bit
2019-08-30 12:18:00 +01:00
Leo Hemsted
b7e8f1baa2 rename which_financial_year to get_financial_year_for_datetime
also fix bug on the very second of rollover, march 31st 23:00:00, and
add tests
2019-08-30 12:18:00 +01:00
Rebecca Law
1c94d6d24a Added command to populate data for annual billing based on last years values. 2019-08-30 12:17:59 +01:00
Rebecca Law
1a5763feb6 Compare result tuple rather than each element in tuple (just to make it a little shorter)
Change order of columns
2019-08-30 12:17:59 +01:00
Rebecca Law
3b16950aee Final touches on tests and query for itemised letter billing data. 2019-08-30 12:17:59 +01:00
Rebecca Law
236d111c22 Update queries for data model change (removal of organisation_to_service)
New query to get letter breakdown.
2019-08-30 12:17:59 +01:00
Rebecca Law
a52c65ea29 Remove the delete query when updating the ft_billing. It's in the wrong place and we also should not need it. 2019-07-18 16:24:06 +01:00
Rebecca Law
ed611f982c We found a problem with the report tasks that populate the fact tables (or statistic tables). It is possible that the notification status can change for notifications after 4 days.
This PR updates those queries to look in either Notification or NotificationHistory. Since the data does not exist in both tables at the same time we can do with and not worry about the data retention.
The query will iterate over each service, then each notification type and query the data if no results then try the history table.
2019-07-18 15:29:54 +01:00
Leo Hemsted
9f1f858997 update fact_billing_dao::get_rate to use dates not datetimes
update unit tests too
2019-04-03 14:52:41 +01:00
Leo Hemsted
6f41f6c7d7 use db models instead of tuples when referring to rate objects
makes it less confusing
2019-04-03 13:08:53 +01:00
Leo Hemsted
1dc084be54 fix nightly ft stats tables task to respect BST
the create_nightly_notification_status task runs at 00:30am UK time,
however this means that in summer datetime.today() will return the
wrong date as the server (which runs on UTC) will run the task at
23:30 (populating the wrong row in the table).

fix this to use nice tz aware functions
2019-04-02 15:15:07 +01:00
Katie Smith
ff06d120e8 Bump notifications-utils to 3.7.0
Bumped notifications-utils to 3.7.0. Version 3.7.0 includes the
`convert_utc_to_bst` and `convert_bst_to_utc` functions and the
`LETTER_PROCESSING_DEADLINE` constant, so these have been removed from
this repo and anywhere using these has now been updated to get these
from `notifications-utils`.

Also bumped pytest by a patch version to bring in a bug fix.
2018-11-26 12:53:39 +00:00
Rebecca Law
8832c6d82f Update billing to ignore failed letters. 2018-10-29 14:02:40 +00:00
Katie Smith
236bbc5f28 Return postage from the monthly-usage endpoint
We were already returning the month, notification_type, billing_units
and rate from the /monthly-usage billing endpoint. This adds in the
postage too so that we can display postage details on the usage page of
admin.
2018-10-03 13:07:46 +01:00
Katie Smith
71d28035dd Update ft_billing with real postage data
* Changed update_fact_billing DAO function to update the table with the
real data for postage instead of hard-coding in 'second'.
* Added a test for the create nightly billing task to test that rows
with different postage are being inserted correctly.
2018-09-28 16:32:18 +01:00
Katie Smith
6727f0e0f5 Update ft_billing DAO functions to use postage
* Updated the 'fetch_billing_data_for_day' DAO function to take postage into
account
* Updated the 'update_fact_billing' DAO function to insert postage for
new rows. When updating rows which are identical apart from the postage, the
original row will be kept. (This behaviour will change once postage is
added to the primary key - at this point, upserting will add a new row.)
* Also changed some fixtures / test set up functions to take postage
into account
2018-09-28 13:52:17 +01:00
Pea Tyczynska
165b65612e Change how rates are filtered by date to equal or greater 2018-09-25 15:10:16 +01:00
Pea Tyczynska
b3191dbc2a Filter letter rates by post_class in get_rate
Also adjust existing tests.
2018-09-25 15:10:15 +01:00
Rebecca Law
ff2334937c Last night we had some letter notifications that were in permanent failure and had page count = 0.
This causes the create-nighlty-billing task to fail.
This will make sure that 0 is returned in this case.
2018-07-31 11:04:48 +01:00
Pea Tyczynska
ca2b350a99 Remove references to monthly_billing table from api 2018-07-30 11:07:42 +01:00
Leo Hemsted
a826f6e924 make rebuild-ft-billing-for-day remove unused rows
if the billing data was incorrect and needs to be rebuilt, we should
remove old rows. Previously we were only upserting new rows, but old
no-longer-relevant rows were staying in the database. This commit
makes the flask command remove *all* rows for that service and day,
before inserting the rows from the original notification data after.

This commit doesn't change the existing nightly task, nor does it
change the upsert that happens upon viewing the usage page. In normal
usage, there should never be a case where the number of billable units
for a rate decreases. It should only ever increase, thus, never need to
be deleted
2018-07-26 19:10:30 +01:00
Katie Smith
c6c118fea1 Update created_at column of ft_billing to be non-nullable
`created_at` was added previously and made nullable temporarily. This
commit now populates the column, ensures that it will always have a
value, and makes `created_at` non-nullable.
2018-05-23 11:09:13 +01:00
Rebecca Law
1567e9bfe5 In order to correctly select the rate for letters you need the billable_unit or page count to use to match on the letter_rates.
A new column is added to the query to populate ft_billing which is only populated for letter.
2018-05-22 17:35:21 +01:00
Rebecca Law
e5584348ef Add rate to the primary key of ft_billing.
Use the primary key constraint in the upsert statement (easier to read than listing all the columns)
2018-05-21 14:38:25 +01:00
Rebecca Law
c2b888a6af Make sure the date does not include time 2018-05-21 11:51:06 +01:00