Skip to content

Commit

Permalink
refactor: remove nullable attributes from School (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocorallo authored Jul 8, 2024
1 parent fcf6dc2 commit 802af87
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 92 deletions.
2 changes: 1 addition & 1 deletion PoliNetwork.Graduatorie.Parser/Objects/Json/DateFound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void SetDate(string path, DateTime? minDateTime)
private DateTime? GetMinTime(Ranking variable, string path)
{
FirstDate ??= new SortedDictionary<string, DateTime?>();
var dateTime = new DateTime(variable.Year ?? DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
var dateTime = new DateTime(variable.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
var tryGetValue = TryGetValue(FirstDate, path);
var minDateTime = tryGetValue.Item2 switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public static BySchoolYearCourseJson From(RankingsSet set)
mainJson.All = list;

// group rankings by school
var bySchool = set.Rankings.Where(r => r.School != null).GroupBy(r => r.School!.Value);
var bySchool = set.Rankings.GroupBy(r => r.School);

foreach (var schoolGroup in bySchool)
{
var school = schoolGroup.Key;

var byYears = schoolGroup.Where(r => r.Year != null).GroupBy(r => r.Year!.Value);
var byYears = schoolGroup.GroupBy(r => r.Year);
var yearsDict = GetYearsDict(byYears);

mainJson.Schools.Add(school, yearsDict);
Expand Down Expand Up @@ -75,9 +75,6 @@ private static CoursesDict GetCoursesDict(IEnumerable<Ranking> yearGroup)

foreach (var ranking in yearGroup)
{
if (ranking.ByCourse == null)
continue;

var byTitle =
ranking.ByCourse.Where(c => c.Title != null).GroupBy(c => c.Title!); // e.g. INGEGNERIA AEROSPAZIALE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public static BySchoolYearJson From(RankingsSet set)
mainJson.All = list;

// group rankings by school
var bySchool = set.Rankings.Where(r => r.School != null).GroupBy(r => r.School!.Value);
var bySchool = set.Rankings.GroupBy(r => r.School);
foreach (var schoolGroup in bySchool)
{
var school = schoolGroup.Key;
var byYears = schoolGroup.Where(r => r.Year != null).GroupBy(r => r.Year!.Value);
var byYears = schoolGroup.GroupBy(r => r.Year);

var yearsDict = GetYearsDict(byYears);
mainJson.Schools.Add(school, yearsDict);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public static ByYearSchoolJson From(RankingsSet set)
mainJson.All = list;

// group rankings by year
var byYear = set.Rankings.Where(r => r.Year != null).GroupBy(r => r.Year!.Value);
var byYear = set.Rankings.GroupBy(r => r.Year);
foreach (var yearGroup in byYear)
{
var year = yearGroup.Key;
var bySchools = yearGroup.Where(r => r.School != null).GroupBy(r => r.School!.Value);
var bySchools = yearGroup.GroupBy(r => r.School);

var schoolsDict = GetSchoolsDict(bySchools);
mainJson.Years.Add(year, schoolsDict);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static StatsJson From(RankingsSet rankingsSet)
{
var statsJson = new StatsJson();

var byYears = rankingsSet.Rankings.Where(r => r.Year != null).GroupBy(r => r.Year!.Value);
var byYears = rankingsSet.Rankings.GroupBy(r => r.Year);
foreach (var yearGroup in byYears)
{
var statsYear = StatsYear.From(yearGroup.ToList());
Expand Down
10 changes: 4 additions & 6 deletions PoliNetwork.Graduatorie.Parser/Objects/Json/Stats/StatsSchool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ public class StatsSchool
public List<StatsSingleCourseJson> List = new();
public int NumStudents;

public static StatsSchool From(IEnumerable<Ranking> pRankings)
public static StatsSchool From(IEnumerable<Ranking> rankings)
{
var statsSchool = new StatsSchool();
var rankings = pRankings.Where(r => r is { Year: not null, School: not null }).ToList();

statsSchool.NumStudents =
rankings.Select(x => (x.RankingSummary ?? x.CreateSummary()).HowManyStudents ?? 0).Sum();

statsSchool.List = rankings
var rankingsEnumerable = rankings.ToList();
statsSchool.NumStudents = rankingsEnumerable.Select(x => x.RankingSummary.HowManyStudents ?? 0).Sum();
statsSchool.List = rankingsEnumerable
.SelectMany(r => r.ToStats())
.DistinctBy(x => new { x.SingleCourseJson.Id, x.SingleCourseJson.Location })
.OrderBy(x => x.SingleCourseJson.Id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public StatsSingleCourseJson(SingleCourseJson singleCourseJson, RankingSummary s
public static List<StatsSingleCourseJson> From(Ranking ranking)
{
var singleCourseJsons = ranking.ToSingleCourseJson();
ranking.RankingSummary ??= ranking.CreateSummary();
return singleCourseJsons.Select(scj => new StatsSingleCourseJson(scj, ranking.RankingSummary)).ToList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class StatsYear

public static StatsYear From(List<Ranking> rankings)
{
if (rankings.Count == 0) return new StatsYear();
var statsYear = new StatsYear
{
Year = rankings.First(r => r.Year != null).Year!.Value, // just hilarious
NumStudents = rankings.Select(r => (r.RankingSummary ?? r.CreateSummary()).HowManyStudents ?? 0)
.Sum() // this ?? is crazy
Year = rankings.First().Year, // just hilarious
NumStudents = rankings.Select(r => r.RankingSummary.HowManyStudents ?? 0).Sum() // this ?? is crazy
};

var bySchool = rankings.Where(r => r.School != null).GroupBy(r => r.School!.Value);
var bySchool = rankings.GroupBy(r => r.School);
foreach (var schoolGroup in bySchool)
{
var statsSchool = StatsSchool.From(schoolGroup.ToList());
Expand Down
71 changes: 30 additions & 41 deletions PoliNetwork.Graduatorie.Parser/Objects/RankingNS/Ranking.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using PoliNetwork.Graduatorie.Common.Data;
Expand All @@ -20,15 +19,15 @@ namespace PoliNetwork.Graduatorie.Parser.Objects.RankingNS;
[JsonObject(MemberSerialization.Fields, NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Ranking : IComparable<Ranking>, IEquatable<Ranking>
{
public List<CourseTable>? ByCourse;
public MeritTable? ByMerit;
public string? Extra;
public DateTime LastUpdate;
public RankingOrder? RankingOrder;
public RankingSummary? RankingSummary;
public SchoolEnum? School;
public RankingUrl? Url;
public int? Year;
public required DateTime LastUpdate;
public required SchoolEnum School;
public required int Year;
public required RankingUrl Url;
public required string Extra;
public required RankingOrder RankingOrder;
public required RankingSummary RankingSummary;
public required MeritTable ByMerit;
public required List<CourseTable> ByCourse;

public int CompareTo(Ranking? other)
{
Expand All @@ -47,7 +46,7 @@ public bool Equals(Ranking? other)

public RankingSummaryStudent GetRankingSummaryStudent()
{
return new RankingSummaryStudent(RankingOrder?.Phase, School, Year, Url);
return new RankingSummaryStudent(RankingOrder.Phase, School, Year, Url);
}

public static Ranking? FromJson(string fullPath)
Expand All @@ -67,9 +66,9 @@ public bool IsSimilarTo(Ranking ranking)
{
return Year == ranking.Year &&
School == ranking.School &&
RankingOrder?.Phase == ranking.RankingOrder?.Phase &&
RankingOrder.Phase == ranking.RankingOrder.Phase &&
Extra == ranking.Extra &&
Url?.Url == ranking.Url?.Url;
Url.Url == ranking.Url.Url;
}


Expand All @@ -83,27 +82,23 @@ public string GetId()
{
var idList = new List<string>();

var schoolShort = School?.ToShortName();
if (schoolShort != null) idList.Add(schoolShort);
var schoolShort = School.ToShortName();
idList.Add(schoolShort);

var yearStr = Year.ToString();
if (yearStr != null) idList.Add(yearStr);
idList.Add(yearStr);

var orderId = RankingOrder?.GetId();
if (orderId != null) idList.Add(orderId);

var fallback = DateTime.UtcNow.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture) + "Z";
if (idList.Count == 0) idList.Add(fallback);
var orderId = RankingOrder.GetId();
idList.Add(orderId);

return string.Join("_", idList);
}

public List<SingleCourseJson> ToSingleCourseJson()
{
var result = new List<SingleCourseJson>();
var schoolString = School == null ? null : Enum.GetName(typeof(SchoolEnum), School);
var schoolString = Enum.GetName(typeof(SchoolEnum), School);
var courseTables = ByCourse;
if (courseTables == null) return result;
result.AddRange(courseTables.Select(variable => new SingleCourseJson
{
Link = GetFilename(),
Expand Down Expand Up @@ -160,24 +155,18 @@ public void WriteAsJson(string outFolder, bool forceReparse = false)
public int GetHashWithoutLastUpdate()
{
var i = "Ranking".GetHashCode();
i ^= Extra?.GetHashCode() ?? "Extra".GetHashCode();
i ^= RankingOrder?.GetHashWithoutLastUpdate() ?? "RankingOrder".GetHashCode();
i ^= RankingSummary?.GetHashWithoutLastUpdate() ?? "RankingSummary".GetHashCode();
i ^= School?.GetHashCode() ?? "School".GetHashCode();
i ^= Url?.GetHashWithoutLastUpdate() ?? "Url".GetHashCode();
i ^= Year?.GetHashCode() ?? "Year".GetHashCode();
var iMerit = ByMerit?.GetHashWithoutLastUpdate();
i ^= iMerit ?? "ByMerit".GetHashCode();


if (ByCourse == null)
i ^= "ByCourse".GetHashCode();
else
i = ByCourse.Aggregate(i, (current, variable) =>
{
var hashWithoutLastUpdate = variable.GetHashWithoutLastUpdate();
return current ^ hashWithoutLastUpdate;
});
i ^= Extra.GetHashCode();
i ^= RankingOrder.GetHashWithoutLastUpdate();
i ^= RankingSummary.GetHashWithoutLastUpdate();
i ^= School.GetHashCode();
i ^= Url.GetHashWithoutLastUpdate();
i ^= Year.GetHashCode();
i ^= ByMerit.GetHashWithoutLastUpdate();
i = ByCourse.Aggregate(i, (current, variable) =>
{
var hashWithoutLastUpdate = variable.GetHashWithoutLastUpdate();
return current ^ hashWithoutLastUpdate;
});

return i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,34 @@ public int GetHashWithoutLastUpdate()

public static RankingSummary From(Ranking ranking)
{
var byMeritRows = ranking.ByMerit?.Rows;
var byMeritRows = ranking.ByMerit.Rows;
var results = CalculateResultsScores(byMeritRows);

var keyValuePairs = results?.OrderBy(x => x.Key)
.ToDictionary(obj => obj.Key, obj => obj.Value);

var courseTableStatsList = ranking.ByCourse?.Select(x => x.GetStats())
var courseTableStatsList = ranking.ByCourse.Select(x => x.GetStats())
.OrderBy(x => x.Title).ThenBy(x => x.Location).ToList();

var howManyCanEnroll = byMeritRows?.Count(x => x.EnrollType?.CanEnroll ?? false);


var groupBy = courseTableStatsList?.GroupBy(x =>
var groupBy = courseTableStatsList.GroupBy(x =>
{
TitleLocation titleLocation;
titleLocation.Title = x.Title;
titleLocation.Location = x.Location;
return titleLocation;
});
var distinctBy = groupBy
?.DistinctBy(x =>
.DistinctBy(x =>
{
TitleLocation titleLocation;
titleLocation.Title = x.Key.Title;
titleLocation.Location = x.Key.Location;
return titleLocation;
});
var tableStatsList = distinctBy?.ToList();
var tableStatsList = distinctBy.ToList();
var tableStatsList2 = Get(tableStatsList);
var resultsSummarized = new SortedDictionary<int, int>(keyValuePairs ?? new Dictionary<int, int>());
return new RankingSummary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CourseTableStats GetStats()

public RankingSummaryStudent GetRankingSummaryStudent(Ranking ranking)
{
return new RankingSummaryStudent(Title, ranking.RankingOrder?.Phase, ranking.School,
return new RankingSummaryStudent(Title, ranking.RankingOrder.Phase, ranking.School,
ranking.Url, ranking.Year);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static IdsDict GetIdsDict(RankingsSet rankingsSet)
var dictionary = new IdsDict();
foreach (var ranking in rankingsSet.Rankings)
{
var byMeritRows = ranking.ByMerit?.Rows;
var byMeritRows = ranking.ByMerit.Rows;
if (byMeritRows != null)
foreach (var student in byMeritRows.Where(student => !string.IsNullOrEmpty(student.Id)))
{
Expand All @@ -56,7 +56,6 @@ private static IdsDict GetIdsDict(RankingsSet rankingsSet)
}

var rankingByCourse = ranking.ByCourse;
if (rankingByCourse == null) continue;
foreach (var courseTable in rankingByCourse.Where(c => c.Rows != null))
{
var row = courseTable.Rows!;
Expand Down
Loading

0 comments on commit 802af87

Please sign in to comment.