Problem statement
Complete the implementation of CustomerAnalyticsService to rank customers by what they have spent, and to pin a ranking so that the pinned board keeps saying what it said when it was taken.
Sample Data
The customer directory is pre-seeded with seven customers, one of which is inactive. Here is an example of a customer record:
{
"id": 1,
"name": "Aurora Labs",
"active": true,
"email": "billing@auroralabs.example",
"accountManager": "R. Vance",
"creditLimit": 25000.00,
"internalNote": "renewal due in March"
}
The order log is pre-seeded with fourteen orders, twelve of them paid and two refunded, each carrying an id, a customerId, an amount and a status. One of them carries a customerId that no customer in the directory holds.
Service
The Ranking
CustomerAnalyticsService is handed an ICustomerDirectory, an IOrderLog and an IBoardStore. Reading a source's Rows walks whatever that source holds at that moment and adds one to its EnumerationCount, which starts at 0.
Both Rank and Pin build the ranking the same way:
1. Read the orders and keep only the ones whose status is paid. A refunded order counts for nothing.
2. Read the customers and keep only the ones that are active.
3. Match each kept customer with the kept orders that carry its id as their customerId. An order whose customerId belongs to no customer in the directory reaches no entry, and a customer that no kept order points at is still ranked.
4. Turn each kept customer into one entry holding its id as customerId, its name, orderCount, the number of matched orders, totalSpend, the sum of their amounts, and averageOrderValue, that total divided by that count.
5. Order the entries by totalSpend, from the highest down to the lowest, and order two entries on the same total by name, from A to Z.
6. Keep the first count entries and drop the rest.
7. Number what is left: the first entry has rank 1, the second 2, and so on. Two entries never share a rank, however equal their totals are.
Building a ranking reads the customer directory once and the order log once, however many customers are ranked.
averageOrderValue is rounded to two decimal places, and a value sitting exactly halfway between two hundredths is rounded away from zero, so an average of 20.025 is reported as 20.03. A customer with no matched orders reports 0 for its orderCount, its totalSpend and its averageOrderValue, which puts it below every customer that spent anything.
An entry carries those six values and nothing else. A customer's active, email, accountManager, creditLimit and internalNote never leave the directory.
CustomerAnalyticsService
Implement the three methods of CustomerAnalyticsService:
- Rank:
- Accept a count and build the ranking over the two sources as they stand at that moment.
- Return an empty ranking when count is less than 1.
- Return every ranked customer and pad nothing when count is larger than the number of them.
- Treat neither an empty directory nor an empty order log as an error: a directory holding no customers gives an empty ranking, and an order log holding no orders gives every active customer at 0.
- Pin:
- Accept a count and settle the ranking's result then and there, so the entries the pinned board carries are the ones the two sources held at that moment and nothing that happens to either source afterwards can reach them. A board handed the query itself rather than its result is the bug this repository exists to fix: it is read again on every later look, and it answers with whatever the sources hold by then.
- Keep the settled board through IBoardStore.Save, in place of any board pinned before it, and return it.
- Read both sources while pinning, so both enumeration counts are higher after a pin than they were before it.
- PinnedBoard:
- Return the board that was pinned, exactly as it was pinned, or null when none has been.
- Leave the pinned board untouched when a ranking is asked for after the pin, so the board reads back the same before and after it.
- Read neither source again, so both enumeration counts are the same after the read as they were before it.
Sample Requests & Responses
GET /api/analytics/top-customers?count=9
The ranking holds six customers, so all six come back and nothing is padded. Dunmore Supply is ranked even though both of its orders were refunded, and Tamarind Studio is ranked even though it never ordered anything.
Response code: 200
[
{ "rank": 1, "customerId": 1, "name": "Aurora Labs", "orderCount": 4, "totalSpend": 900.00, "averageOrderValue": 225.00 },
{ "rank": 2, "customerId": 2, "name": "Borealis Freight", "orderCount": 3, "totalSpend": 100.00, "averageOrderValue": 33.33 },
{ "rank": 3, "customerId": 7, "name": "Cormack Rail", "orderCount": 1, "totalSpend": 40.05, "averageOrderValue": 40.05 },
{ "rank": 4, "customerId": 3, "name": "Whitlock Works", "orderCount": 2, "totalSpend": 40.05, "averageOrderValue": 20.03 },
{ "rank": 5, "customerId": 6, "name": "Dunmore Supply", "orderCount": 0, "totalSpend": 0, "averageOrderValue": 0 },
{ "rank": 6, "customerId": 4, "name": "Tamarind Studio", "orderCount": 0, "totalSpend": 0, "averageOrderValue": 0 }
]
Borealis Freight paid for three orders adding up to 100.00, which does not divide evenly, so its average is reported as 33.33. Cormack Rail and Whitlock Works both spent 40.05, so they run in name order, which puts Cormack Rail first even though Whitlock Works paid for more orders. Whitlock Works paid 20.00 and 20.05, an average of 20.025, which lands exactly halfway between two hundredths and is reported as 20.03.
Asking for a different sized board takes the first entries of that same ranking:
- ?count=3 holds Aurora Labs, Borealis Freight and Cormack Rail.
- No count at all holds at most 5 entries, so Tamarind Studio, the sixth, falls outside the board.
- ?count=0, or any count below 1, answers 200 with an empty ranking rather than an error, and so does any count at all when the directory holds no customers.
- With the order log holding no orders, every active customer is still ranked at 0, so they all tie and run in name order: Aurora Labs, Borealis Freight, Cormack Rail, Dunmore Supply, Tamarind Studio, Whitlock Works.
POST /api/analytics/pinned?count=10
Answers 201 with a Location of /api/analytics/pinned and the same six entries shown above, and keeps them.
Now record a paid order of 900.00 for Cormack Rail:
- GET /api/analytics/pinned still answers those six entries exactly as they were pinned, Cormack Rail still showing one order of 40.05.
- GET /api/analytics/top-customers?count=10 builds from the sources as they stand, so Cormack Rail is now the biggest spender with 2 orders, a totalSpend of 940.05 and an average of 470.03, ahead of Aurora Labs.
GET /api/analytics/pinned before anything has been pinned answers 404. GET / answers 200.
