Skip to content

Commit bdfe5f4

Browse files
committed
fix(statistics): produce monthly statistics
- use nested structure
1 parent 9dd7aa9 commit bdfe5f4

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/db.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,46 @@ export function countRequests() {
4848
count: { $sum: 1 }
4949
}
5050
},
51+
// Umwandlung der _id-Struktur zum Vereinfachen der nächsten Gruppierung
5152
{
52-
$sort: { "_id.year": 1, "_id.month": 1, count: -1 }
53+
$project: {
54+
year: "$_id.year",
55+
month: "$_id.month",
56+
affiliation: "$_id.affiliation",
57+
count: 1,
58+
_id: 0
59+
}
60+
},
61+
// Zweite Gruppierungsstufe nach Jahr und Monat, wobei die Ergebnisse je Affiliation gesammelt werden
62+
{
63+
$group: {
64+
_id: {
65+
year: "$year",
66+
month: "$month"
67+
},
68+
affiliations: {
69+
$push: {
70+
affiliation: "$affiliation",
71+
count: "$count"
72+
}
73+
}
74+
}
75+
},
76+
// Finalisierung der Struktur: Gruppierung nach Jahr und Anordnung der Monate je Jahr
77+
{
78+
$group: {
79+
_id: "$_id.year",
80+
months: {
81+
$push: {
82+
month: "$_id.month",
83+
affiliations: "$affiliations"
84+
}
85+
}
86+
}
87+
},
88+
// Sortieren der Ergebnisse, zunächst nach Jahr dann innerhalb der Jahre nach Monaten
89+
{
90+
$sort: { "_id": 1, "months.month": 1 }
5391
}
5492
])
5593
.toArray();

0 commit comments

Comments
 (0)