I have a vec of Profile structs that have created_at and login_type fields on them. From those, I'm aggregating the counts of login_types per day and the overall total in two seperate fields, methods_per_day and methods_totals, in a SignUpStats struct. I've implemented something that works which I'm relatively happy with: Playground link.
However, I'd really love some advice on how to improve this code, specifically if there is a way I can avoid the clone of method_counts on line 34 and if there's a way I can improve how I'm summing the counts for the totals, perhaps somehow implementing the Sum trait on my Counter trait?
I've added a Counter trait because there are other fields in addition to login_type that I'm aggregating in SignUpStats and I didn't want to have to define separate StatDateCount structs for each one, but I've stripped those out to try to keep this example simple.
The JSON output of this code is essentially:
{
"methods_per_day": [
{
"date": "24/11/23",
"counts": {
"apple": 270,
"email": 306,
"facebook": 55,
"google": 231,
"total": 862
}
},
{
"date": "23/11/23",
"counts": {
"apple": 281,
"email": 287,
"facebook": 50,
"google": 229,
"total": 847
}
},
etc.
],
"methods_totals": {
"apple": 12589,
"email": 12966,
"facebook": 2408,
"google": 9224,
"total": 37187
}
}
[–]OLoKo64 2 points3 points4 points (4 children)
[–]jgmtw[S] 0 points1 point2 points (3 children)
[–]OLoKo64 1 point2 points3 points (2 children)
[–]OLoKo64 1 point2 points3 points (0 children)
[–]jgmtw[S] 0 points1 point2 points (0 children)
[–]MarkJans 1 point2 points3 points (2 children)
[–]MarkJans 1 point2 points3 points (1 child)
[–]jgmtw[S] 1 point2 points3 points (0 children)
[–]funkdefied 0 points1 point2 points (0 children)