Skip to content

Commit

Permalink
refactor: move ranking saving to RankingsSet and Ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocorallo committed Jul 5, 2024
1 parent e7a33db commit c3daf44
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 143 deletions.
113 changes: 2 additions & 111 deletions PoliNetwork.Graduatorie.Parser/Objects/Json/Indexes/IndexJsonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using PoliNetwork.Graduatorie.Common.Objects;
using PoliNetwork.Graduatorie.Parser.Objects.Json.Indexes.Specific;
using PoliNetwork.Graduatorie.Parser.Objects.RankingNS;
using PoliNetwork.Graduatorie.Parser.Objects.Tables.Course;
using PoliNetwork.Graduatorie.Parser.Objects.Tables.Merit;
using PoliNetwork.Graduatorie.Parser.Utils;

#endregion

Expand All @@ -26,115 +23,9 @@ public void WriteToFile(string outFolder, string pathFile)
var mainJsonString = JsonConvert.SerializeObject(this, Culture.JsonSerializerSettings);
File.WriteAllText(mainJsonPath, mainJsonString);
}


public static void WriteSingleJsons(RankingsSet? set, string outFolder, ArgsConfig argsConfig)
{
if (set == null)
return;

// group rankings by year
var bySchool = set.Rankings.GroupBy(r => r.School);
foreach (var schoolGroup in bySchool)
{
if (schoolGroup.Key is null)
continue;
var school = schoolGroup.Key.Value;

var byYears = schoolGroup.GroupBy(r => r.Year);
foreach (var yearGroup in byYears)
{
if (yearGroup.Key is null)
continue;
var year = yearGroup.Key.Value;
var folder = Path.Join(outFolder, school.ToString(), year.ToString());
Directory.CreateDirectory(folder);

foreach (var ranking in yearGroup) WriteSingleJsonRanking(folder, ranking, argsConfig);
}
}
}

private static void WriteSingleJsonRanking(string folder, Ranking ranking, ArgsConfig argsConfig)
{
var path = Path.Join(folder, ranking.GetFilename());

if (ExitIfAlreadyExistsAndNotUpdated(ranking, path) && !argsConfig.ForceReparsing) return;

var rankingJsonString = JsonConvert.SerializeObject(ranking, Culture.JsonSerializerSettings);
File.WriteAllText(path, rankingJsonString);
}

private static bool ExitIfAlreadyExistsAndNotUpdated(Ranking a, string path)
{
if (!File.Exists(path)) return false;
var b = GetRankingFromFile(path);
return b != null && SameHash(a, b);
}

private static bool SameHash(Ranking a, Ranking b)
{
var ai = a.GetHashWithoutLastUpdate();
var bi = b.GetHashWithoutLastUpdate();
return ai == bi;
}

private static bool SameHashCourse(IReadOnlyCollection<CourseTable>? aTableCourse,
IReadOnlyCollection<CourseTable>? bTableCourse)
{
if (aTableCourse == null && bTableCourse == null)
return true;
if (aTableCourse == null || bTableCourse == null)
return false;

if (aTableCourse.Count != bTableCourse.Count)
return false;

var aHash = aTableCourse.Select(variable =>
{
var hashWithoutLastUpdate = Hashing.GetHashFromListHash(variable.GetHashWithoutLastUpdate());
return hashWithoutLastUpdate;
}).ToList();

var bHash = bTableCourse.Select(variable =>
{
var hashWithoutLastUpdate = Hashing.GetHashFromListHash(variable.GetHashWithoutLastUpdate());
return hashWithoutLastUpdate;
}).ToList();

var ai = Hashing.GetHashFromListHash(aHash);
var bi = Hashing.GetHashFromListHash(bHash);

return (ai ?? 0) == (bi ?? 0);
}

private static bool SameHashMerit(MeritTable? aTableMerit, MeritTable? bTableMerit)

public static void WriteAllIndexes(RankingsSet rankingsSet, string outFolder, ArgsConfig argsConfig)
{
if (aTableMerit == null && bTableMerit == null)
return true;
if (aTableMerit == null || bTableMerit == null)
return false;

var ai = aTableMerit.GetHashWithoutLastUpdate();
var bi = bTableMerit.GetHashWithoutLastUpdate();
var aii = Hashing.GetHashFromListHash(ai) ?? 0;
var bii = Hashing.GetHashFromListHash(bi) ?? 0;
return aii == bii;
}

private static Ranking? GetRankingFromFile(string path)
{
var x = File.ReadAllText(path);

var j = JsonConvert.DeserializeObject<Ranking>(x, Culture.JsonSerializerSettings);
return j;
}

public static void IndexesWrite(RankingsSet? rankingsSet, string outFolder, ArgsConfig argsConfig)
{
//let's write all single json files
WriteSingleJsons(rankingsSet, outFolder, argsConfig);

//now let's write each single different index
BySchoolYearJson.From(rankingsSet)?.WriteToFile(outFolder, BySchoolYearJson.PathCustom);
ByYearSchoolJson.From(rankingsSet)?.WriteToFile(outFolder, ByYearSchoolJson.PathCustom);
Expand Down
97 changes: 67 additions & 30 deletions PoliNetwork.Graduatorie.Parser/Objects/RankingNS/Ranking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using PoliNetwork.Graduatorie.Common.Data;
using PoliNetwork.Graduatorie.Common.Enums;
using PoliNetwork.Graduatorie.Common.Objects.RankingNS;
using PoliNetwork.Graduatorie.Parser.Objects.Json;
Expand All @@ -18,7 +19,7 @@ namespace PoliNetwork.Graduatorie.Parser.Objects.RankingNS;

[Serializable]
[JsonObject(MemberSerialization.Fields, NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Ranking : IComparable<Ranking>
public class Ranking : IComparable<Ranking>, IEquatable<Ranking>
{
public List<CourseTable>? ByCourse;
public MeritTable? ByMerit;
Expand All @@ -34,6 +35,15 @@ public RankingSummaryStudent GetRankingSummaryStudent()
{
return new RankingSummaryStudent(RankingOrder?.Phase, School, Year, Url);
}

public static Ranking? FromJson(string fullPath)
{
if (!File.Exists(fullPath)) return null;

var str = File.ReadAllText(fullPath);
var ranking = JsonConvert.DeserializeObject<Ranking>(str, Culture.JsonSerializerSettings);
return ranking;
}

public int CompareTo(Ranking? other)
{
Expand All @@ -44,33 +54,10 @@ public int CompareTo(Ranking? other)
}


/***
* Ottieni l'hash senza considerare il valore di LastUpdate
*/
public int GetHashWithoutLastUpdate()
public bool Equals(Ranking? other)
{
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 ^= Hashing.GetHashFromListHash(iMerit) ?? "ByMerit".GetHashCode();


if (ByCourse == null)
i ^= "ByCourse".GetHashCode();
else
i = ByCourse.Aggregate(i, (current, variable) =>
{
var hashWithoutLastUpdate = variable.GetHashWithoutLastUpdate();
var iList = Hashing.GetHashFromListHash(hashWithoutLastUpdate) ?? "empty".GetHashCode();
return current ^ iList;
});

return i;
if (other == null) return false;
return GetHashWithoutLastUpdate() == other.GetHashWithoutLastUpdate();
}


Expand All @@ -83,7 +70,6 @@ public bool IsSimilarTo(Ranking ranking)
Url?.Url == ranking.Url?.Url;
}


public void Merge(Ranking ranking)
{
LastUpdate = LastUpdate > ranking.LastUpdate ? LastUpdate : ranking.LastUpdate;
Expand Down Expand Up @@ -159,8 +145,59 @@ public RankingSummary CreateSummary()
return RankingSummary.From(this);
}

public string GetPath()
public string GetBasePath(string outFolder = "")
{
return Path.Join(outFolder, $"{School}/{Year}/");
}

public string GetFullPath(string outFolder = "")
{
return Path.Join(GetBasePath(outFolder), GetFilename());
}

public void WriteAsJson(string outFolder, bool forceReparse = false)
{
return School + "/" + Year + "/" + RankingOrder?.Phase;
var folderPath = GetBasePath(outFolder);
Directory.CreateDirectory(folderPath);

var fullPath = GetFullPath(outFolder);

var savedRanking = FromJson(fullPath);
var equalsSaved = savedRanking != null && Equals(savedRanking);

if (forceReparse || equalsSaved || savedRanking == null)
{
var rankingJsonString = JsonConvert.SerializeObject(this, Culture.JsonSerializerSettings);
File.WriteAllText(fullPath, rankingJsonString);
}
}

/***
* Ottieni l'hash senza considerare il valore di LastUpdate
*/
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 ^= Hashing.GetHashFromListHash(iMerit) ?? "ByMerit".GetHashCode();


if (ByCourse == null)
i ^= "ByCourse".GetHashCode();
else
i = ByCourse.Aggregate(i, (current, variable) =>
{
var hashWithoutLastUpdate = variable.GetHashWithoutLastUpdate();
var iList = Hashing.GetHashFromListHash(hashWithoutLastUpdate) ?? "empty".GetHashCode();
return current ^ iList;
});

return i;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public void Merge(RankingsSet set)
{
foreach (var ranking in set.Rankings) AddRanking(ranking);
}

public void WriteAllRankings(string outFolder, bool forceReparse = false)
{
foreach (var ranking in Rankings) ranking.WriteAsJson(outFolder, forceReparse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public OutputWriteUtil(ArgsConfig argsConfig)
_config = argsConfig;
}

public void SaveOutputs(RankingsSet? rankingsSet, DateFound dateFound)
public void SaveOutputs(RankingsSet rankingsSet, DateFound dateFound)
{
var outFolder = Path.Join(_config.DataFolder, Constants.OutputFolder);
IndexJsonBase.IndexesWrite(rankingsSet, outFolder, _config);

rankingsSet.WriteAllRankings(outFolder, _config.ForceReparsing);
IndexJsonBase.WriteAllIndexes(rankingsSet, outFolder, _config);
StatsJson.Write(outFolder, rankingsSet, _config);
HashMatricoleWrite.Write(rankingsSet, outFolder);

Expand Down

0 comments on commit c3daf44

Please sign in to comment.