Commit 1f7442fa authored by HankG's avatar HankG

Add TimelineManagementServices with tests working

parent ef664b28
Pipeline #98180827 failed with stage
in 60 minutes and 1 second
using System;
using System.Collections.Generic;
using System.Linq;
using MySocialPortalDesktop.Factory;
using MySocialPortalLib.Model;
using MySocialPortalLib.Repository;
using MySocialPortalLib.Util;
namespace MySocialPortalLib.Service
{
public class TimelineManagementService
{
private const int QueryPaddingMinutes = 1;
private const int QueryWindowDays = 1;
public ulong EarliestValue { get; }
public ulong LatestValue { get; }
public ulong ForwardValue { get; }
private TimelineManagementService()
{
throw new MethodAccessException();
}
public TimelineManagementService(ITimelineRepository repository, ulong earliestValue, ulong latestValue, ulong forwardValue)
public TimelineManagementService(ITimelineRepository repository)
{
TimelineRepository = repository;
EarliestValue = earliestValue;
LatestValue = latestValue;
ForwardValue = forwardValue;
}
public TimelineInterval? GetNextSamplingIntervals(string timelineName)
public TimelineInterval? GetNextSamplingInterval(string timelineName, ulong earliestValue, ulong forwardValue)
{
if (TimelineRepository.CountByTimelineName(timelineName) == 0)
{
var interval = new TimelineInterval
{
IntervalStart = EarliestValue,
IntervalStop = ForwardValue,
IntervalStart = earliestValue,
IntervalStop = forwardValue,
TimelineName = timelineName
};
......@@ -51,22 +39,108 @@ namespace MySocialPortalLib.Service
var interval2 = new TimelineInterval
{
IntervalStart = intervals[0].IntervalStop,
IntervalStop = ForwardValue,
IntervalStop = forwardValue,
TimelineName = timelineName
};
TimelineRepository.AddOrUpdate(interval2);
return interval2;
}
public bool UpdateRequestedInterval(TimelineInterval updatedInterval)
public TimelineInterval? GetPreviousSamplingInterval(string timelineName, ulong initialStart, ulong initialStop)
{
if (TimelineRepository.CountByTimelineName(timelineName) == 0)
{
var interval = new TimelineInterval
{
IntervalStart = initialStart,
IntervalStop = initialStop,
TimelineName = timelineName
};
TimelineRepository.AddOrUpdate(interval);
return interval;
}
var intervals = new List<TimelineInterval>(TimelineRepository.FindByTimelineName(timelineName));
intervals.Sort((i1, i2) => i2.IntervalStop.CompareTo(i1.IntervalStop));
if (intervals.Count == 1)
{
if (intervals[0].IntervalStart <= initialStart)
{
return null;
}
var newInterval = new TimelineInterval
{
IntervalStart = initialStart,
IntervalStop = intervals[0].IntervalStart,
TimelineName = timelineName
};
TimelineRepository.AddOrUpdate(newInterval);
return newInterval;
}
var earliest = ulong.MaxValue;
for (var i = 0; i < intervals.Count - 1; i++)
{
var interval1 = intervals[i];
var interval2 = intervals[i + 1];
if (interval1.IntervalStart < earliest)
{
earliest = interval1.IntervalStart;
}
var gap = TimelineIntervalUtilities.Gap(interval1, interval2);
if (gap != null)
{
TimelineRepository.AddOrUpdate(gap);
return gap;
}
}
if (earliest <= initialStart)
{
return null;
}
var fillInterval = new TimelineInterval
{
IntervalStart = earliest,
IntervalStop = initialStart,
TimelineName = timelineName
};
TimelineRepository.AddOrUpdate(fillInterval);
return fillInterval;
}
public bool UpdateRequestedInterval(TimelineInterval updatedInterval, bool withCleanup = true)
{
return TimelineRepository.FindById(updatedInterval.Id) != null
&& TimelineRepository.AddOrUpdate(updatedInterval);
if (TimelineRepository.FindById(updatedInterval.Id) == null)
{
return false;
}
bool success = TimelineRepository.AddOrUpdate(updatedInterval);
if (withCleanup)
{
CleanupIntervals(updatedInterval.TimelineName);
}
return success;
}
private ITimelineRepository TimelineRepository { get; }
private void CleanupIntervals(string timelineName)
{
var allIntervals = TimelineRepository.FindByTimelineName(timelineName);
var mergedIntervals = TimelineIntervalUtilities.CleanupList(allIntervals);
TimelineRepository.RemoveByTimelineName(timelineName);
TimelineRepository.AddOrUpdate(mergedIntervals);
}
}
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ namespace MySocialPortalLib.Util
};
}
public static List<TimelineInterval> CleanupList(List<TimelineInterval> originalList, ulong gapTolerance = 0)
public static List<TimelineInterval> CleanupList(IList<TimelineInterval> originalList, ulong gapTolerance = 0)
{
var retainedIntervals = new List<TimelineInterval>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment