|
| 1 | +using System.Globalization; |
| 2 | +using Microsoft.JSInterop; |
| 3 | + |
| 4 | +namespace Tevling.Pages; |
| 5 | + |
| 6 | +public partial class Statistics : ComponentBase |
| 7 | +{ |
| 8 | + [Inject] private IJSRuntime Js { get; set; } = null!; |
| 9 | + [Inject] private IAuthenticationService AuthenticationService { get; set; } = null!; |
| 10 | + [Inject] private IActivityService ActivityService { get; set; } = null!; |
| 11 | + |
| 12 | + private Athlete _athlete = null!; |
| 13 | + private Activity[] _activities = []; |
| 14 | + |
| 15 | + |
| 16 | + protected override async Task OnInitializedAsync() |
| 17 | + { |
| 18 | + _athlete = await AuthenticationService.GetCurrentAthleteAsync(); |
| 19 | + |
| 20 | + ActivityFilter filter = new(_athlete.Id, false, DateTimeOffset.Now.AddMonths(-2)); |
| 21 | + _activities = await ActivityService.GetActivitiesAsync(filter); |
| 22 | + } |
| 23 | + |
| 24 | + private Dictionary<string, float[]> GetAggregatedMeasurementData(Func<Activity, float> selector, int monthCount = 3) |
| 25 | + { |
| 26 | + DateTimeOffset now = DateTimeOffset.Now; |
| 27 | + |
| 28 | + Dictionary<string, float[]> aggregatedData = _activities |
| 29 | + .GroupBy(a => a.Details.Type) |
| 30 | + .ToDictionary( |
| 31 | + g => g.Key.ToString(), |
| 32 | + g => Enumerable.Range(-monthCount + 1, monthCount) |
| 33 | + .Select(m => |
| 34 | + { |
| 35 | + int month = now.AddMonths(m).Month; |
| 36 | + return g |
| 37 | + .Where(a => a.Details.StartDate.Month == month) |
| 38 | + .Sum(selector); |
| 39 | + }) |
| 40 | + .ToArray() |
| 41 | + ); |
| 42 | + |
| 43 | + if (aggregatedData.Count != 0) |
| 44 | + { |
| 45 | + aggregatedData["Total"] = |
| 46 | + [ |
| 47 | + .. aggregatedData.Values.Aggregate((sum, next) => [.. sum.Zip(next, (a, b) => a + b)]), |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + return aggregatedData; |
| 52 | + } |
| 53 | + |
| 54 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 55 | + { |
| 56 | + if (firstRender) |
| 57 | + { |
| 58 | + List<int> lastThreeMonths = |
| 59 | + [ |
| 60 | + DateTimeOffset.Now.AddMonths(-2).Month, |
| 61 | + DateTimeOffset.Now.AddMonths(-1).Month, |
| 62 | + DateTimeOffset.Now.Month, |
| 63 | + ]; |
| 64 | + |
| 65 | + Dictionary<string, float[]> distancesLastThreeMonths = |
| 66 | + GetAggregatedMeasurementData(a => a.Details.DistanceInMeters); |
| 67 | + Dictionary<string, float[]> elevationLastThreeMonths = |
| 68 | + GetAggregatedMeasurementData(a => a.Details.TotalElevationGain); |
| 69 | + Dictionary<string, float[]> timeLastThreeMonths = |
| 70 | + GetAggregatedMeasurementData(a => (float)a.Details.MovingTimeInSeconds / 3600); |
| 71 | + |
| 72 | + |
| 73 | + await Js.InvokeVoidAsync( |
| 74 | + "drawChart", |
| 75 | + distancesLastThreeMonths, |
| 76 | + lastThreeMonths.Select(m => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m)).ToList(), |
| 77 | + "totalDistanceChart", |
| 78 | + "Total Distance [m]"); |
| 79 | + await Js.InvokeVoidAsync( |
| 80 | + "drawChart", |
| 81 | + elevationLastThreeMonths, |
| 82 | + lastThreeMonths.Select(m => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m)).ToList(), |
| 83 | + "totalElevationChart", |
| 84 | + "Total Elevation [m]"); |
| 85 | + await Js.InvokeVoidAsync( |
| 86 | + "drawChart", |
| 87 | + timeLastThreeMonths, |
| 88 | + lastThreeMonths.Select(m => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m)).ToList(), |
| 89 | + "totalTimeChart", |
| 90 | + "Total Time [h]"); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments