Commit Graph

11 Commits

Author SHA1 Message Date
Ben Thorner
99b2b4642e Fix incorrect chargeable_units column
This was added to migrate away from the vague "billing_units" field,
but without fixing the inconsistent data behind it:

- For emails and letters, "billing_units" was just the number sent.

- For SMS, "billing_units" really was the chargeable_units.

To avoid confusion we need two fields to represent the original mix
of data - this exposes "notifications_sent" in both APIs.
2022-04-26 18:18:36 +01:00
Ben Thorner
2bdaeabbaa Add "charged_units" to usage APIs 2022-04-26 17:56:17 +01:00
Ben Thorner
2999fa6714 Add "free_chargeable_units" to service usage APIs
This represents the number of chargeable_units that were actually
free due to the free allowance - they won't be included in "cost".
Although the existing calculations in Admin [^1][^2] will still be
correct with a change in SMS rates - it's cost that's the problem
- it makes sense to have all the knowledge about calculating usage
consistently in these two APIs.

[^1]: 474d7dfda8/app/main/views/dashboard.py (L490)
[^2]: c63660d56d/app/main/views/dashboard.py (L350)
2022-04-26 13:24:17 +01:00
Ben Thorner
ff32000180 Add "cost" field to monthly usage API
This starts to replace the calculation in Admin [^1] and, similar
to the yearly API, also correctly attributes free allowance when
we have a rate change during a month.

[^1]: 474d7dfda8/app/templates/views/usage.html (L98)
2022-04-26 13:24:16 +01:00
Ben Thorner
e276e8a15c Use new functions for monthly usage API
This starts work towards replacing the manual free allowance and
cost calculations currently done in Admin.
2022-04-26 13:24:15 +01:00
Ben Thorner
106da583ea Add costs to each row in yearly usage API
This will replace the manual calculation in Admin [^1] for SMS and
also in API [^2] for letters.

Doing the calculation here also means we correctly attribute free
allowance to the earliest rows in the billing table - Admin doesn't
know when a given rate was applied so can't do this with the data
currently returned from the API.

Since the calculation now depends on annual billing, we need to
change all the tests to make sure a suitable row exists.

Note about "OVER" clause
========================

Using "rows=" ("ROWS BETWEEN") makes more sense than "range=" as
we want the remainder to be incremental within each group in a
"GROUP BY" clause, as well as between groups i.e

  # ROWS BETWEEN (arbitrary numbers to illustrate)
  date=2021-04-03, units=3, cost=3.29
  date=2021-04-03, units=2, cost=4.17
  date=2021-04-04, units=2, cost=5.10

  vs.

  # RANGE BETWEEN
  date=2021-04-03, units=3, cost=4.17
  date=2021-04-03, units=2, cost=4.17
  date=2021-04-04, units=2, cost=5.10

See [^3] for more details and examples.

[^1]: https://github.com/alphagov/notifications-admin/blob/master/app/templates/views/usage.html#L60
[^2]: 072c3b2079/app/billing/billing_schemas.py (L37)
[^3]: https://learnsql.com/blog/difference-between-rows-range-window-functions/
2022-04-26 13:24:14 +01:00
Ben Thorner
0af791e417 Prepare to switch to "chargeable_units" in API
This is so we can migrate from "billing_units" to this new field in
the Admin app, without breaking anything in between.
2022-04-26 13:24:13 +01:00
Ben Thorner
4cca01a8cb Remove duplicate edge case test for monthly usage
This doesn't change the structural behaviour of the API and can be
tested just as well at a lower level.
2022-04-26 13:24:09 +01:00
Ben Thorner
fe6afd18d6 Refactor tests for monthly usage API
These are now consistent with the yearly usage API tests.
2022-04-26 13:11:32 +01:00
Ben Thorner
4d3c604faf Move edge case usage API tests down to DAO level
These tests weren't checking anything structural about the APIs
beyond what's covered by the other tests. They represent edge
cases that we can check at a lower level instead.

It was also unclear what these tests were actually testing, as
the term "all cases" is vague. Looking at the test data, there
are variations in rates, multipliers and billable units for SMS
and letters, which I've summarised as "variable rates".

Note: I've removed part of the test data - for the first class
letter rate - as it's not clearly adding anything.
2022-04-26 13:09:25 +01:00
Ben Thorner
8e74280e84 Remove test file to match file under test
This makes it easier to see at a glance that this file is testing
the endpoints vs. lower level code.
2022-04-21 18:36:36 +01:00