Skip to content

Commit

Permalink
🐛 Wrong action count for specific user statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
TDroogers committed Sep 27, 2024
1 parent 140256f commit 46a06bf
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task UpdateAnalyzeYearChartAll()
_renderChart = false;
await Task.Delay(1); // Will not update without this delay
StateHasChanged();
var analyzeData = await ReportActionRepository.AnalyzeYearChartsAll(SelectedUsers, _selectedPrio, _cls.Token);
var analyzeData = await ReportActionRepository.AnalyzeYearChartsAll(SelectedUsers, _selectedPrio, AllYears ? null : 5, _cls.Token);
if (analyzeData is null) return;
_elapsedMilliseconds = analyzeData.ElapsedMilliseconds;
_data = StatisticsTab.DrawLineChartAll(analyzeData, AllYears);
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Pages/Dashboard/Components/StatisticsTab.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public List<ChartYear> DrawLineChartAll(AnalyzeYearChartAllResponse? analyzeData
var yearCount = 0;
foreach (var year in analyzeData.Years.OrderByDescending(x => x.Year))
{
if (!allYears && yearCount >= 5)
break;
/*if (!allYears && yearCount >= 5)
break;*/
if (year.Year <= 2021)
{
_showHistoricalIncorrectWarning = true;
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Repositories/ReportActionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ public async Task<MultipleReportActionsResponse> GetLastActions(IEnumerable<Drog
return result;
}

public async Task<AnalyzeYearChartAllResponse?> AnalyzeYearChartsAll(IEnumerable<DrogeUser> users, IEnumerable<string?> prio, CancellationToken clt)
public async Task<AnalyzeYearChartAllResponse?> AnalyzeYearChartsAll(IEnumerable<DrogeUser> users, IEnumerable<string?>? prio, int? years, CancellationToken clt)
{
try
{
var request = new AnalyzeActionRequest()
{
Users = users.Select(x => (Guid?)x.Id).ToList(),
Prio = prio
Prio = prio,
Years = years
};
var result = await _reportActionClient.AnalyzeYearChartsAllAsync(request, clt);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Controllers/ReportTrainingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<ActionResult<AnalyzeYearChartAllResponse>> AnalyzeYearChartsAl
if (request.Users.Count > 5)
return new AnalyzeYearChartAllResponse() { Message = "To many users" };

var result = await _reportTrainingService.AnalyzeYearChartsAll(request.Users, customerId, timeZone, clt);
var result = await _reportTrainingService.AnalyzeYearChartsAll(request, customerId, timeZone, clt);
return result;
}
catch (Exception ex)
Expand Down
6 changes: 1 addition & 5 deletions src/Server/Database/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Drogecode.Knrm.Oefenrooster.Server.Database
{
public class DataContext : DbContext, IDataProtectionKeyContext
public class DataContext(DbContextOptions<DataContext> context) : DbContext(context), IDataProtectionKeyContext
{
// To persist key's
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; } = null!;
Expand Down Expand Up @@ -43,10 +43,6 @@ public class DataContext : DbContext, IDataProtectionKeyContext
public DbSet<DbLinkExchange> LinkExchanges{ get; set; }


public DataContext(DbContextOptions<DataContext> context) : base(context)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Audit
Expand Down
5 changes: 4 additions & 1 deletion src/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@
new[] { "application/octet-stream" });
});

builder.Services.AddDbContextPool<DataContext>(options => options.UseNpgsql(dbConnectionString));
builder.Services.AddDbContextPool<DataContext>(options =>
{
options.UseNpgsql(dbConnectionString);
});
builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Services/Interfaces/IReportTrainingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Drogecode.Knrm.Oefenrooster.Server.Services.Interfaces;
public interface IReportTrainingService
{
Task<MultipleReportTrainingsResponse> GetListTrainingUser(List<Guid?> users, Guid userId, int count, int skip, Guid customerId, CancellationToken clt);
Task<AnalyzeYearChartAllResponse> AnalyzeYearChartsAll(List<Guid?> users, Guid customerId, string timeZone, CancellationToken clt);
Task<AnalyzeYearChartAllResponse> AnalyzeYearChartsAll(AnalyzeTrainingRequest trainingRequest, Guid customerId, string timeZone, CancellationToken clt);
}
17 changes: 15 additions & 2 deletions src/Server/Services/ReportActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,32 @@ public async Task<AnalyzeYearChartAllResponse> AnalyzeYearChartsAll(AnalyzeActio
&& (actionRequest.Prio == null || !actionRequest.Prio.Any() || actionRequest.Prio.Contains(x.Prio))
&& x.Users!.Count(y => actionRequest.Users!.Contains(y.DrogeCodeId)) == actionRequest.Users!.Count)
.Select(x => new { x.Start, x.Number })
.OrderBy(x => x.Start)
.OrderByDescending(x => x.Start)
.ToListAsync(clt);
var result = new AnalyzeYearChartAllResponse { TotalCount = allReports.Count() };
var skipped = 0;
List<int> years = new();
foreach (var report in allReports)
{
if (report.Number is not null && (report.Number % 1) != 0 && allReports.Select(x => x.Start.Year == report.Start.Year && x.Number?.FloatingEquals((int)report.Number, 0.1) == true).Any())
if (report.Number is not null && (report.Number % 1) != 0 && allReports.Count(x => x.Start.Year == report.Start.Year && x.Number?.FloatingEquals((int)report.Number, 0.1) == true) >= 2)
{
skipped++;
continue;
}

var zone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
var start = TimeZoneInfo.ConvertTimeFromUtc(report.Start, zone);

if (!years.Contains(start.Year)) // Could not find a way to check this in the db request.
{
if (years.Count >= actionRequest.Years)
{
break;
}

years.Add(start.Year);
}

if (result.Years.All(x => x.Year != start.Year))
{
result.Years.Add(new AnalyzeYearDetails() { Year = start.Year });
Expand Down
15 changes: 13 additions & 2 deletions src/Server/Services/ReportTrainingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@ public async Task<MultipleReportTrainingsResponse> GetListTrainingUser(List<Guid
}


public async Task<AnalyzeYearChartAllResponse> AnalyzeYearChartsAll(List<Guid?> users, Guid customerId, string timeZone, CancellationToken clt)
public async Task<AnalyzeYearChartAllResponse> AnalyzeYearChartsAll(AnalyzeTrainingRequest trainingRequest, Guid customerId, string timeZone, CancellationToken clt)
{
var sw = Stopwatch.StartNew();
var allReports = _database.ReportTrainings
.Where(x => x.CustomerId == customerId && x.Users.Count(y => users.Contains(y.DrogeCodeId)) == users.Count)
.Where(x => x.CustomerId == customerId && x.Users!.Count(y => trainingRequest.Users!.Contains(y.DrogeCodeId)) == trainingRequest.Users!.Count)
.Select(x => new { x.Start });
var result = new AnalyzeYearChartAllResponse { TotalCount = allReports.Count() };
List<int> years = new();
foreach (var report in allReports)
{
var start = report.Start.ToDateTimeTimeZone(timeZone).ToDateTime();

if (!years.Contains(start.Year)) // Could not find a way to check this in the db request.
{
if (years.Count >= trainingRequest.Years)
{
break;
}

years.Add(start.Year);
}
if (result.Years.All(x => x.Year != start.Year))
{
result.Years.Add(new AnalyzeYearDetails() { Year = start.Year });
Expand Down
1 change: 0 additions & 1 deletion src/Server/Services/ScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ public async Task<ScheduleForAllResponse> ScheduleForAllAsync(Guid userId, Guid
else if (vehicle.IsDefault)
{
vehiclePrev = vehicle;
//break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Shared/Models/ReportAction/AnalyzeActionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class AnalyzeActionRequest
{
public List<Guid?>? Users { get; set; }
public IEnumerable<string?>? Prio { get; set; }
public int? Years { get; set; }
}
1 change: 1 addition & 0 deletions src/Shared/Models/ReportTraining/AnalyzeTrainingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public class AnalyzeTrainingRequest
{
public List<Guid?>? Users { get; set; }
public int? Years { get; set; }
}
18 changes: 18 additions & 0 deletions tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedCustomer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Drogecode.Knrm.Oefenrooster.Server.Database.Models;

namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds;

public class SeedCustomer
{
public static void Seed(DataContext dataContext, Guid defaultCustomerId)
{
dataContext.Customers.Add(new DbCustomers
{
Id = defaultCustomerId,
Name = "xUnit customer",
TimeZone = "Europe/Amsterdam",
Created = DateTime.UtcNow
});
dataContext.SaveChanges();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Drogecode.Knrm.Oefenrooster.Server.Database.Models;
using Drogecode.Knrm.Oefenrooster.Shared.Helpers;

namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds;

public static class SeedReportAction
{
public static void Seed(DataContext dataContext, Guid defaultCustomerId)
{
SeedForYear(dataContext, defaultCustomerId, 2022);
SeedForYear(dataContext, defaultCustomerId, 2020);
SeedForYear(dataContext, defaultCustomerId, 2019);
SeedForYear(dataContext, defaultCustomerId, 2018);
SeedForYear(dataContext, defaultCustomerId, 2016);
SeedForYear(dataContext, defaultCustomerId, 2012);
SeedForYear(dataContext, defaultCustomerId, 2004);
dataContext.SaveChanges();
}

private static void SeedForYear(DataContext dataContext, Guid defaultCustomerId, int year)
{
var start = new DateTime(year, 3, 8, 8, 5, 41);
dataContext.ReportActions.Add(new DbReportAction
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description A",
Start = start,
Commencement = start.AddMinutes(5),
Departure = start.AddMinutes(15),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Prio = "Prio 69",
Users = new List<DbReportUser>{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}},
});
start = start.AddDays(1);
dataContext.ReportActions.Add(new DbReportAction
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description B",
Start = start,
Commencement = start.AddMinutes(5),
Departure = start.AddMinutes(15),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Prio = "Prio 1",
Users = new List<DbReportUser>{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}},
});
start = start.AddMonths(1);
dataContext.ReportActions.Add(new DbReportAction
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description C",
Start = start,
Commencement = start.AddMinutes(5),
Departure = start.AddMinutes(15),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Prio = "Prio 1",
Users = new List<DbReportUser>{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Drogecode.Knrm.Oefenrooster.Server.Database.Models;
using Drogecode.Knrm.Oefenrooster.Shared.Helpers;

namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds;

public static class SeedReportTraining
{
public static void Seed(DataContext dataContext, Guid defaultCustomerId)
{
SeedForYear(dataContext, defaultCustomerId, 2022);
SeedForYear(dataContext, defaultCustomerId, 2020);
SeedForYear(dataContext, defaultCustomerId, 2019);
SeedForYear(dataContext, defaultCustomerId, 2018);
SeedForYear(dataContext, defaultCustomerId, 2016);
SeedForYear(dataContext, defaultCustomerId, 2012);
SeedForYear(dataContext, defaultCustomerId, 2004);
dataContext.SaveChanges();
}

private static void SeedForYear(DataContext dataContext, Guid defaultCustomerId, int year)
{
var start = new DateTime(year, 3, 8, 8, 5, 41);
dataContext.ReportTrainings.Add(new DbReportTraining
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description A",
Start = start,
Commencement = start.AddMinutes(5),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Users = new List<DbReportUser> { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } },
});
start = start.AddDays(1);
dataContext.ReportTrainings.Add(new DbReportTraining
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description B",
Start = start,
Commencement = start.AddMinutes(5),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Users = new List<DbReportUser> { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } },
});
start = start.AddMonths(1);
dataContext.ReportTrainings.Add(new DbReportTraining
{
Id = Guid.NewGuid(),
CustomerId = defaultCustomerId,
Description = "xUnit Description C",
Start = start,
Commencement = start.AddMinutes(5),
End = start.AddMinutes(121),
Boat = "xUnit boat",
Users = new List<DbReportUser> { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } },
});
}
}
Loading

0 comments on commit 46a06bf

Please sign in to comment.