namespace BonchCalendar.Services;
///
/// Service that tracks results of the most recent requests.
///
public class IssueTrackingService
{
private bool _isLastFacultyFetchSuccessful = true;
private readonly List _unsuccessfulGroupFetches = [];
private readonly List _unsuccessfulTimetableFetches = [];
///
/// Record whether the last attempt to retrieve faculty list was successful.
///
/// Set true if the attempt was successful. Otherwise, set false
public void TrackFacultyFetch(bool isSuccessful) =>
_isLastFacultyFetchSuccessful = isSuccessful;
///
/// Record whether the last attempt to retrieve groups for provided faculty and term year was successful.
///
/// ID of a faculty which was used to retrieve the group list.
/// Term year which was used to retrieve the group list.
/// Set true if the attempt was successful. Otherwise, set false
public void TrackGroupFetch(int facultyId, int termYear, bool isSuccessful)
{
string key = $"{facultyId}/{termYear}";
if (!isSuccessful)
{
if (!_unsuccessfulGroupFetches.Contains(key))
_unsuccessfulGroupFetches.Add(key);
}
else
_unsuccessfulGroupFetches.Remove(key);
}
///
/// Record whether the last attempt to retrieve timetable for provided group was successful.
///
/// ID of a faculty the group belongs to.
/// ID of a group the timetable was retrieved for.
/// Set true if the attempt was successful. Otherwise, set false
public void TrackTimetableFetch(int facultyId, int groupId, bool isSuccessful)
{
string key = $"{facultyId}/{groupId}";
if (!isSuccessful)
{
if (!_unsuccessfulTimetableFetches.Contains(key))
_unsuccessfulTimetableFetches.Add(key);
}
else
_unsuccessfulTimetableFetches.Remove(key);
}
///
/// Get report on the success of latest retrieval attempts.
///
/// A dictionary for each of the tracked groups.
///
/// If the dictionary is empty, that means that there're no known issues.
///
public Dictionary GetReport()
{
Dictionary report = [];
if (!_isLastFacultyFetchSuccessful)
report.Add("/faculties", false);
if (_unsuccessfulGroupFetches.Count > 0)
report.Add("/groups", _unsuccessfulGroupFetches.ToArray());
if (_unsuccessfulTimetableFetches.Count > 0)
report.Add("/timetable", _unsuccessfulTimetableFetches.ToArray());
// No issues example:
/*
* { }
*/
// Report example with issues:
/*
* {
* "/faculties": false,
* "/groups": [
* "123/1",
* "321/3"
* ],
* "/timetable": [
* "123/321",
* "456/654"
* ],
* }
*/
return report;
}
}