WineQualityStatistics: Add KMW mode

This commit is contained in:
2024-03-30 13:12:54 +01:00
parent 80e91ad776
commit 869f652afc
4 changed files with 23 additions and 22 deletions

View File

@ -5,14 +5,14 @@ using System.Collections.Generic;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Elwig.Models.Dtos {
public class WineQualityStatisticsData {
public record struct QualityRow(string? Variety, string? Attribute, string? Cultivation, string? Type, string QualId, int Oe, int Num, int Weight);
public record struct QualitySection(string Name, string? Type, Dictionary<string, (int Oe, int Num, int Weight)[]> Data);
public record struct QualityRow(string? Variety, string? Attribute, string? Cultivation, string? Type, string QualId, double Grad, int Num, int Weight);
public record struct QualitySection(string Name, string? Type, Dictionary<string, (double Grad, int Num, int Weight)[]> Data);
public bool UseOe = true;
public QualitySection[] Sections;
public WineQualityStatisticsData(QualitySection[] sections) {
@ -21,8 +21,8 @@ namespace Elwig.Models.Dtos {
private static QualitySection[] GetQualitySections(IEnumerable<QualityRow> rows) {
var data = new List<QualitySection>();
var currentQual = new Dictionary<int, (int Num, int Weight)>();
var current = new Dictionary<string, (int, int, int)[]>();
var currentQual = new Dictionary<double, (int Num, int Weight)>();
var current = new Dictionary<string, (double, int, int)[]>();
string? lastSection = null;
string? lastType = null;
string? lastQual = null;
@ -43,7 +43,7 @@ namespace Elwig.Models.Dtos {
current = [];
currentQual.Clear();
}
currentQual[row.Oe] = (row.Num, row.Weight);
currentQual[row.Grad] = (row.Num, row.Weight);
lastSection = sec;
lastType = row.Type;
lastQual = row.QualId;
@ -60,7 +60,7 @@ namespace Elwig.Models.Dtos {
return [.. data];
}
public static async Task<WineQualityStatisticsData> FromQuery(IQueryable<DeliveryPart> query) {
public static async Task<WineQualityStatisticsData> FromQuery(IQueryable<DeliveryPart> query, bool useOe = true) {
var rows = (await query
.GroupBy(p => new {
p.Variety.Type,
@ -68,7 +68,7 @@ namespace Elwig.Models.Dtos {
Attribute = p.Attribute!.Name,
Cultivation = p.Cultivation!.Name,
p.QualId,
Oe = (int)Math.Round(p.Kmw * (4.54 + 0.022 * p.Kmw), 0),
Oe = useOe ? Math.Round(p.Kmw * (4.54 + 0.022 * p.Kmw), 0) : Math.Round(p.Kmw, 1),
}, (k, g) => new { Key = k, Num = g.Count(), Weight = g.Sum(p => p.Weight) })
.OrderBy(g => g.Key.Variety)
.ThenBy(g => g.Key.Attribute)
@ -84,22 +84,22 @@ namespace Elwig.Models.Dtos {
return new(data);
var typeRows = rows
.GroupBy(s => new { s.Type, s.QualId, s.Oe }, (k, g) => new QualityRow(null, null, null, k.Type, k.QualId, k.Oe, g.Sum(g => g.Num), g.Sum(p => p.Weight)))
.GroupBy(s => new { s.Type, s.QualId, s.Grad }, (k, g) => new QualityRow(null, null, null, k.Type, k.QualId, k.Grad, g.Sum(g => g.Num), g.Sum(p => p.Weight)))
.OrderBy(g => g.Type)
.ThenBy(g => g.QualId)
.ThenBy(g => g.Oe)
.ThenBy(g => g.Grad)
.ToList();
var typeData = GetQualitySections(typeRows);
if (typeData.Length <= 1)
return new([.. typeData, .. data]);
var totalRows = rows
.GroupBy(s => new { s.QualId, s.Oe }, (k, g) => new QualityRow(null, null, null, null, k.QualId, k.Oe, g.Sum(p => p.Num), g.Sum(p => p.Weight)))
.GroupBy(s => new { s.QualId, s.Grad }, (k, g) => new QualityRow(null, null, null, null, k.QualId, k.Grad, g.Sum(p => p.Num), g.Sum(p => p.Weight)))
.OrderBy(g => g.QualId)
.ThenBy(g => g.Oe)
.ThenBy(g => g.Grad)
.ToList();
var totalData = GetQualitySections(totalRows);
return new([.. totalData, .. typeData, .. data]);
return new([.. totalData, .. typeData, .. data]) { UseOe = useOe };
}
}
}