Farbpalette vorladen lassen
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
This commit is contained in:
@@ -1,74 +1,86 @@
|
||||
using cfs_tracker.Helpers;
|
||||
using cfs_tracker.Models;
|
||||
|
||||
namespace cfs_tracker.Services;
|
||||
|
||||
public class AggregationService
|
||||
{
|
||||
public List<WeekDaySummary> BuildWeekSummary(DateTime weekStart, IEnumerable<HealthEntry> entries)
|
||||
public AverageStateResult CalculateAverage(IEnumerable<HealthEntry> entries)
|
||||
{
|
||||
var result = new List<WeekDaySummary>();
|
||||
for (int i = 0; i < 7; i++)
|
||||
var list = entries?.ToList() ?? new List<HealthEntry>();
|
||||
if (list.Count == 0)
|
||||
{
|
||||
var date = weekStart.Date.AddDays(i);
|
||||
var dayEntries = entries.Where(x => x.Timestamp.Date == date).ToList();
|
||||
var morning = dayEntries.Where(x => x.Timestamp.Hour < 12).ToList();
|
||||
var afternoon = dayEntries.Where(x => x.Timestamp.Hour >= 12).ToList();
|
||||
result.Add(new WeekDaySummary
|
||||
return new AverageStateResult
|
||||
{
|
||||
Date = date,
|
||||
MorningAverage = AverageOrNull(morning),
|
||||
AfternoonAverage = AverageOrNull(afternoon),
|
||||
MorningCount = morning.Count,
|
||||
AfternoonCount = afternoon.Count
|
||||
});
|
||||
HasValue = false,
|
||||
AverageScore = 0,
|
||||
State = HealthState.Gray
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<CalendarDaySummary> BuildMonthSummary(DateTime month, IEnumerable<HealthEntry> entries)
|
||||
{
|
||||
var first = new DateTime(month.Year, month.Month, 1);
|
||||
int days = DateTime.DaysInMonth(month.Year, month.Month);
|
||||
var result = new List<CalendarDaySummary>();
|
||||
for (int i = 0; i < days; i++)
|
||||
{
|
||||
var date = first.AddDays(i);
|
||||
var dayEntries = entries.Where(x => x.Timestamp.Date == date.Date).ToList();
|
||||
var avg = AverageOrNull(dayEntries);
|
||||
result.Add(new CalendarDaySummary { Date = date, Average = avg, EntryCount = dayEntries.Count, DisplayState = ToDisplayState(avg) });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
var average = list.Average(x => x.State.ToScore());
|
||||
|
||||
public List<CalendarDaySummary> BuildYearSummary(int year, IEnumerable<HealthEntry> entries)
|
||||
{
|
||||
var result = new List<CalendarDaySummary>();
|
||||
for (int month = 1; month <= 12; month++)
|
||||
return new AverageStateResult
|
||||
{
|
||||
int days = DateTime.DaysInMonth(year, month);
|
||||
for (int day = 1; day <= days; day++)
|
||||
HasValue = true,
|
||||
AverageScore = average,
|
||||
State = average switch
|
||||
{
|
||||
var date = new DateTime(year, month, day);
|
||||
var dayEntries = entries.Where(x => x.Timestamp.Date == date.Date).ToList();
|
||||
var avg = AverageOrNull(dayEntries);
|
||||
result.Add(new CalendarDaySummary { Date = date, Average = avg, EntryCount = dayEntries.Count, DisplayState = ToDisplayState(avg) });
|
||||
>= 2.5 => HealthState.Green,
|
||||
>= 1.5 => HealthState.Yellow,
|
||||
>= 0.5 => HealthState.Red,
|
||||
_ => HealthState.Gray
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public HealthState? ToDisplayState(double? average)
|
||||
public IEnumerable<HealthEntry> FilterForDayPart(IEnumerable<HealthEntry> entries, DayPart part)
|
||||
{
|
||||
if (average is null) return null;
|
||||
if (average >= 2.5) return HealthState.Green;
|
||||
if (average >= 1.5) return HealthState.Yellow;
|
||||
if (average >= 0.5) return HealthState.Red;
|
||||
return HealthState.Gray;
|
||||
return entries.Where(x =>
|
||||
{
|
||||
if (x.DayPart.HasValue)
|
||||
return x.DayPart == part;
|
||||
|
||||
return part == DayPart.Morning
|
||||
? x.Timestamp.Hour < 12
|
||||
: x.Timestamp.Hour >= 12;
|
||||
});
|
||||
}
|
||||
|
||||
private static double? AverageOrNull(IEnumerable<HealthEntry> entries)
|
||||
public string BuildDistinctActivityText(IEnumerable<HealthEntry> entries, IReadOnlyDictionary<int, string> activityMap)
|
||||
{
|
||||
var list = entries.ToList();
|
||||
return list.Count == 0 ? null : list.Average(x => (int)x.State);
|
||||
var names = entries
|
||||
.Where(x => x.ActivityId.HasValue)
|
||||
.Select(x => x.ActivityId!.Value)
|
||||
.Where(activityMap.ContainsKey)
|
||||
.Select(x => activityMap[x])
|
||||
.Where(x => !string.Equals(x, "Keine Tätigkeit", StringComparison.OrdinalIgnoreCase))
|
||||
.Distinct()
|
||||
.OrderBy(x => x)
|
||||
.ToList();
|
||||
|
||||
return names.Count == 0 ? string.Empty : string.Join(", ", names);
|
||||
}
|
||||
|
||||
public string BuildDistinctSymptomText(IEnumerable<HealthEntry> entries, IReadOnlyDictionary<int, List<string>> symptomsByEntryId)
|
||||
{
|
||||
var names = entries
|
||||
.Where(x => symptomsByEntryId.ContainsKey(x.Id))
|
||||
.SelectMany(x => symptomsByEntryId[x.Id])
|
||||
.Distinct()
|
||||
.OrderBy(x => x)
|
||||
.ToList();
|
||||
|
||||
return names.Count == 0 ? string.Empty : string.Join(", ", names);
|
||||
}
|
||||
|
||||
public string ToDisplayText(AverageStateResult averageResult, AppearanceService appearanceService)
|
||||
{
|
||||
if (!averageResult.HasValue)
|
||||
return "-";
|
||||
|
||||
return appearanceService.GetText(
|
||||
averageResult.State.ToAppearanceTextKey(),
|
||||
averageResult.State.ToFallbackText());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user