public static HistoryAnalysis getFinalRating(ICollection <HistoryAnalysis> histCollection, long timeInTicks) { try { HistoryAnalysis histAnalysis = new HistoryAnalysis { AnalysedDuration = TimeSpan.Zero, DriverRating = 0 }; if (timeInTicks == 0 || histCollection == null) { return(histAnalysis); } if (histCollection.Count > 0) { histCollection.ToList().ForEach(h => { histAnalysis.AnalysedDuration += h.AnalysedDuration; histAnalysis.DriverRating += (h.AnalysedDuration.Ticks * h.DriverRating); } ); histAnalysis.DriverRating = decimal.Round(histAnalysis.DriverRating / timeInTicks, 4); } return(histAnalysis); } catch (Exception ex) { throw ex; } }
public HistoryAnalysis Analyse(IReadOnlyCollection <Period> history, bool isPenalise) { HistoryAnalysis result = new HistoryAnalysis { AnalysedDuration = TimeSpan.Zero, DriverRating = 0, UnDocumentedPeriod = false }; if (history == null) { return(result); } if (history.Count == 0) { return(result); } result.AnalysedDuration = history.Last().End - history.First().Start; result.DriverRating = 1m; DateTimeOffset startDate = new DateTimeOffset(history.First().Start.Date); DateTimeOffset endDate = new DateTimeOffset(history.Last().End.Date); DateTime driverStartPeriod = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, startDate.Minute, startDate.Second); DateTime driverEndPeriod = new DateTime(endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute, endDate.Second); result.UnDocumentedPeriod = AnalyserHelper.getUndocumentedPeriod(history, driverStartPeriod, driverEndPeriod); result = AnalyserHelper.penaliseAnalyser(result, isPenalise && result.UnDocumentedPeriod); return(result); }
public HistoryAnalysis Analyse(IReadOnlyCollection <Period> history, bool isPenalise) { HistoryAnalysis result = new HistoryAnalysis { AnalysedDuration = TimeSpan.Zero, DriverRating = 0, UnDocumentedPeriod = false }; if (history == null) { return(result); } if (history.Count == 0) { return(result); } List <HistoryAnalysis> histCollection = new List <HistoryAnalysis>(); //Ignore anything before the first non-zero speed in a day, and after the last history.OrderBy(h => h.Start).SkipWhile(h => h.AverageSpeed == 0).Reverse().SkipWhile(h => h.AverageSpeed == 0).ToList().ForEach(h => { histCollection.Add(getValidHistoryData(h, h.Start.TimeOfDay, h.End.TimeOfDay)); } ); if (histCollection == null) { return(result); } if (histCollection.Count == 0) { return(result); } DateTimeOffset startDate = history.OrderBy(h => h.Start).SkipWhile(h => h.AverageSpeed == 0).First().Start; DateTimeOffset endDate = history.OrderBy(h => h.Start).SkipWhile(h => h.AverageSpeed == 0).Reverse().SkipWhile(h => h.AverageSpeed == 0).First().End; DateTime driverStartPeriod = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, startDate.Minute, startDate.Second); DateTime driverEndPeriod = new DateTime(endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute, endDate.Second); //DateTimeOffset driverStartPeriod = history.OrderBy(h => h.Start).SkipWhile(h => h.AverageSpeed == 0).First().Start; //DateTimeOffset driverEndPeriod = history.OrderBy(h => h.Start).SkipWhile(h => h.AverageSpeed == 0).Reverse().SkipWhile(h => h.AverageSpeed == 0).First().End; result = AnalyserHelper.getFinalRating(histCollection, (endDate - startDate).Ticks); result.UnDocumentedPeriod = AnalyserHelper.getUndocumentedPeriod(history, driverStartPeriod, driverEndPeriod); result = AnalyserHelper.penaliseAnalyser(result, isPenalise && result.UnDocumentedPeriod); return(result); }
private HistoryAnalysis getValidHistoryData(Period p, TimeSpan startingTime, TimeSpan endingTime) { HistoryAnalysis histdata = new HistoryAnalysis(); histdata.AnalysedDuration = (endingTime - startingTime); //Speeds between 0 and 30 map linearly to [0,1] and Speeds above 30 map to 0 histdata.DriverRating = ((p.AverageSpeed <= MAXIMUMSPEED && p.AverageSpeed > MINIMUMSPEED) ? (p.AverageSpeed / MAXIMUMSPEED) : ZEROSPEED); return(histdata); }
public void Should_YieldRatingOfOne_When_DurationWithinTimeSlotAndWithMaxSpeedLimit() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(8, 0, 0), DriverRating = 1m }; var actualResult = deliveryDriverAnalyser.Analyse(CannedDrivingData.DeliveryHistoryWithContinousWithMaxSpeedLimit); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }
public void Should_YieldZeroRatingAndDuration_When_PeriodDataSuppiledHasAverageSpeedGreaterThanMaxLimit() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(0, 0, 0), DriverRating = 0m }; var actualResult = deliveryDriverAnalyser.Analyse(CannedDrivingData.DeliveryHistortAverageSpeedGreaterThanMaxLimit); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }
public void Should_YieldZeroRatingAndDuration_When_NoPeriodDataisSuppiled() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(0, 0, 0), DriverRating = 0m }; var actualResult = deliveryDriverAnalyser.Analyse(null); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }
public void Should_YieldCorrectRatingAndDuration_When_PeriodDataSuppiledHasTimeSpanAndAverageSpeedInRange() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(7, 45, 0), DriverRating = 0.7638m }; var actualResult = deliveryDriverAnalyser.Analyse(CannedDrivingData.History); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }
public void Should_YieldCorrectRatingAndDuration_When_PeriodDataSuppiledIsWithinTimeSlotWithBreaksAndWithinMaxSpeedLimit() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(6, 30, 0), DriverRating = 0.649m }; var actualResult = deliveryDriverAnalyser.Analyse(CannedDrivingData.DeliveryHistoryWithBreaksWithinTimeSlotWithinMaxSpeedLimit); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }
public void DelieryRatin1CorrectUsingFile() { var expectedResult = new HistoryAnalysis { AnalysedDuration = new TimeSpan(8, 0, 0), DriverRating = 1.00m }; var actualResult = new DeliveryDriverAnalyser().Analyse( CannedDrivingData.GetPeriods(@"InterviewTest.DriverData.UnitTests/TestHistoryData/rating1.json"), false); Assert.That(actualResult.AnalysedDuration, Is.EqualTo(expectedResult.AnalysedDuration)); Assert.That(actualResult.DriverRating, Is.EqualTo(expectedResult.DriverRating).Within(0.001m)); }