Skip to content

Commit

Permalink
fix(stat): ReadWeekData should return most recent data
Browse files Browse the repository at this point in the history
Signed-off-by: hoyho <[email protected]>
  • Loading branch information
hoyho committed Jun 19, 2024
1 parent 4468729 commit 72374c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions Foundation/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ namespace iTimeSlot.Foundation;
public interface IStatistics
{
DailyStat ReadTodayData();
List<DailyStat> ReadWeekData();

//ReadWeekData return most recent statistics data
List<DailyStat> ReadWeekData(int max);

//CompleteTask marks a new task completed and record it for later use.
void CompleteTask(int minute);

//CompleteTask marks a new break interval completed and record it for later use.
void CompleteBreak(int minute);
}


//DiskStatistics is a class that implements IStatistics interface using disk as storage
public class DiskStatistics : IStatistics
{
private string iTimeslotDateFormat = "yyyy-MM-dd"; //date format: 2024-06-01
private string _dataPath = "";
private const string TimeslotDateFormat = "yyyy-MM-dd"; //date format: 2024-06-01
private readonly string _dataPath;

public DiskStatistics(string dataPath)
{
Expand Down Expand Up @@ -77,17 +82,13 @@ private void LogFinishedInterval(IntervalType type, int minute)
DailyStats = new List<DailyStat>() { }
};
}
else if (stats.DailyStats == null)
{
stats.DailyStats = new List<DailyStat>() { };
}

var entry = new DailyStat()
{
Date = DateTime.Today.ToString(iTimeslotDateFormat),
Date = DateTime.Today.ToString(TimeslotDateFormat),
};

var existed = stats.DailyStats.FirstOrDefault(s => s.Date == DateTime.Today.ToString(iTimeslotDateFormat));
var existed = stats.DailyStats.FirstOrDefault(s => s.Date == DateTime.Today.ToString(TimeslotDateFormat));
if (existed != null)
{
entry = existed;
Expand Down Expand Up @@ -129,15 +130,15 @@ public DailyStat ReadTodayData()

var ds = new DailyStat()
{
Date = DateTime.Today.ToString(iTimeslotDateFormat),
Date = DateTime.Today.ToString(TimeslotDateFormat),
};

if (stats == null || stats.DailyStats == null)
{
return ds;
}

var existed = stats.DailyStats.FirstOrDefault(s => s.Date == DateTime.Today.ToString(iTimeslotDateFormat));
var existed = stats.DailyStats.FirstOrDefault(s => s.Date == DateTime.Today.ToString(TimeslotDateFormat));
if (existed != null)
{
ds = existed;
Expand All @@ -146,7 +147,7 @@ public DailyStat ReadTodayData()
return ds;
}

public List<DailyStat> ReadWeekData()
public List<DailyStat> ReadWeekData(int max=7)
{
EnsureExist();

Expand All @@ -160,8 +161,9 @@ public List<DailyStat> ReadWeekData()
return rs;
}

var existed = stats.DailyStats.OrderBy(s => s.Date).Take(7);
var existed = stats.DailyStats.OrderByDescending(s => s.Date).Take(max);

return existed.ToList();
//ensure return in dated order
return existed.OrderBy(d => d.Date).ToList();
}
}
2 changes: 1 addition & 1 deletion ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public MainWindowViewModel()
TotalBreakMinutes = data.TotalBreakMinutes;
CompletedWorkCount = data.WorkCount;

var weekData = Global.StatReporter.ReadWeekData();
int displayNum = 3;
var weekData = Global.StatReporter.ReadWeekData(3);
StatSeries = new ISeries[]
{
new ColumnSeries<int>
Expand Down

0 comments on commit 72374c8

Please sign in to comment.