Skip to content

Commit a8596f4

Browse files
committed
Top and average spend
1 parent 1d7ca78 commit a8596f4

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

.eleventy.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = function (eleventyConfig) {
2+
const _ = require("lodash")
3+
24
eleventyConfig.addLayoutAlias('base', 'layouts/base.njk');
35

46
eleventyConfig.addCollection("spendsOver500", (collection) => {
@@ -29,15 +31,30 @@ module.exports = function (eleventyConfig) {
2931
}, 0);
3032
});
3133

34+
const GBPFormat = new Intl.NumberFormat('en-GB', {
35+
style: 'currency',
36+
currency: 'GBP',
37+
});
38+
39+
function toCurrency(value){
40+
return GBPFormat.format(value);
41+
}
42+
3243
eleventyConfig.addFilter("toCurrency", function (value) {
33-
let GBPFormat = new Intl.NumberFormat('en-GB', {
34-
style: 'currency',
35-
currency: 'GBP',
36-
});
44+
return toCurrency(value);
45+
})
3746

38-
return GBPFormat.format(value);
47+
eleventyConfig.addFilter("findTopSpender", function (value) {
48+
var maxSpend = _(value)
49+
.groupBy("Directorate")
50+
.map((directorate, total) => ({
51+
directorate: total,
52+
total: _.sumBy(directorate, "Transaction Amount")
53+
}))
54+
.maxBy("total");
55+
56+
return `${maxSpend.directorate} (${toCurrency(maxSpend.total)})`;
3957
})
4058

4159
eleventyConfig.addPassthroughCopy("CNAME");
42-
4360
};

spendover500.njk

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,32 @@ eleventyComputed:
2020
<h1>{{title}}</h1>
2121

2222
<div class="row">
23-
<div class="col-lg-6 col-12">
23+
<div class="col-lg-8 col-12">
2424
<table class="table table-bordered">
2525
<thead>
2626
<tr>
2727
<th colspan="2" class="text-center bg-light">Spend profile</th>
2828
</tr>
2929
</thead>
30+
{% set total_spend = spends.data | sum("Transaction Amount") %}
31+
{% set transaction_count = spends.data | length %}
32+
{% set average_spend = total_spend / transaction_count %}
3033
<tbody>
3134
<tr>
3235
<th class="bg-light">Total spend</th>
33-
<td>{{spends.data | sum("Transaction Amount") | toCurrency }}</td>
36+
<td>{{total_spend | toCurrency }}</td>
3437
</tr>
3538
<tr>
3639
<th class="bg-light">Transaction count</th>
37-
<td>{{spends.data | length }}</td>
40+
<td>{{transaction_count }}</td>
41+
</tr>
42+
<tr>
43+
<th class="bg-light">Average transaction spend</th>
44+
<td>{{ average_spend | toCurrency }}</td>
45+
</tr>
46+
<tr>
47+
<th class="bg-light">Top spender</th>
48+
<td>{{ spends.data | findTopSpender }}</td>
3849
</tr>
3950
</tbody>
4051
</table>

0 commit comments

Comments
 (0)