-
Couldn't load subscription status.
- Fork 1
Heedls 459 usage stats report #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6b0f755
HEEDLS-459 activity services WIP
12e5054
HEEDLS-459 controller and services WIP
e18ef04
HEEDLS-459: services to fetch activity data
da036d8
HEEDLS-459: view model and views
e25d6d6
HEEDLS-459 wire up controller and fix sql
47a7eb5
HEEDLS-459: filter by centre ID, inject dependencies, add link
b3760cf
HEEDLS-459: use clockservice
b93b6ae
HEEDLS-459: tests
5d5faac
HEEDLS-459: 'period' frontend presentation
a794aaf
HEEDLS-459 reapply link after rebase
6c527e6
HEEDLS-459 review markups
22c5479
HEEDLS-459 fix h1 title
2ba2439
HEEDLS-459: review markups
e332940
HEDLS-459: update tests
c24498c
HEEDLS-459: review markups
3597af6
HEEDLS-459: extract month slot generation into helper
c8d5180
HEEDLS-459 refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
DigitalLearningSolutions.Data.Tests/DataServices/ActivityDataServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| namespace DigitalLearningSolutions.Data.Tests.DataServices | ||
| { | ||
| using System; | ||
| using System.Linq; | ||
| using DigitalLearningSolutions.Data.DataServices; | ||
| using DigitalLearningSolutions.Data.Tests.TestHelpers; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| public class ActivityDataServiceTests | ||
| { | ||
| private IActivityDataService service = null!; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| var connection = ServiceTestHelper.GetDatabaseConnection(); | ||
| service = new ActivityDataService(connection); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetActivityForMonthsInYear_gets_activity_by_month_for_date_range() | ||
| { | ||
| // when | ||
| var start = DateTime.Parse("2014-01-01 00:00:00.000"); | ||
| var end = DateTime.Parse("2014-04-30 23:59:59.999"); | ||
| var result = service.GetActivityInRangeByMonth(101, start, end).ToList(); | ||
|
|
||
| // then | ||
| result.Count().Should().Be(4); | ||
|
|
||
| var first = result.First(); | ||
| first.Year.Should().Be(2014); | ||
| first.Month.Should().Be(1); | ||
| first.Completions.Should().Be(1); | ||
| first.Evaluations.Should().Be(0); | ||
| first.Registrations.Should().Be(12); | ||
|
|
||
| var last = result.Last(); | ||
| last.Year.Should().Be(2014); | ||
| last.Month.Should().Be(4); | ||
| last.Completions.Should().Be(0); | ||
| last.Evaluations.Should().Be(1); | ||
| last.Registrations.Should().Be(7); | ||
| } | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
DigitalLearningSolutions.Data.Tests/Helpers/DateHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| namespace DigitalLearningSolutions.Data.Tests.Helpers | ||
| { | ||
| using System; | ||
| using System.Linq; | ||
| using DigitalLearningSolutions.Web.Helpers; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| public class DateHelperTests | ||
| { | ||
| [Test] | ||
| public void GetMonthsAndYearsBetweenDates_returns_single_entry_for_dates_in_same_month() | ||
| { | ||
| // when | ||
| var startDate = DateTime.Parse("2014-01-01 00:00:00.000"); | ||
| var endDate = DateTime.Parse("2014-01-31 23:59:59.999"); | ||
| var result = DateHelper.GetMonthsAndYearsBetweenDates(startDate, endDate).ToList(); | ||
|
|
||
| // then | ||
| result.Count.Should().Be(1); | ||
| result[0].Year.Should().Be(2014); | ||
| result[0].Month.Should().Be(1); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetMonthsAndYearsBetweenDates_returns_loops_over_months_and_increments_years() | ||
| { | ||
| // when | ||
| var startDate = DateTime.Parse("2013-12-31 23:59:59.999"); | ||
| var endDate = DateTime.Parse("2014-01-01 00:00:00.000"); | ||
| var result = DateHelper.GetMonthsAndYearsBetweenDates(startDate, endDate).ToList(); | ||
|
|
||
| // then | ||
| result.Count.Should().Be(2); | ||
| result[0].Year.Should().Be(2013); | ||
| result[0].Month.Should().Be(12); | ||
| result[1].Year.Should().Be(2014); | ||
| result[1].Month.Should().Be(1); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetMonthsAndYearsBetweenDates_increments_months_within_year() | ||
| { | ||
| // when | ||
| var startDate = DateTime.Parse("2014-01-31 23:59:59.999"); | ||
| var endDate = DateTime.Parse("2014-02-01 00:00:00.000"); | ||
| var result = DateHelper.GetMonthsAndYearsBetweenDates(startDate, endDate).ToList(); | ||
|
|
||
| // then | ||
| result.Count.Should().Be(2); | ||
| result[0].Year.Should().Be(2014); | ||
| result[0].Month.Should().Be(1); | ||
| result[1].Year.Should().Be(2014); | ||
| result[1].Month.Should().Be(2); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetMonthsAndYearsBetweenDates_returns_13_months_across_two_years_for_one_year_difference() | ||
| { | ||
| // when | ||
| var startDate = DateTime.Parse("2014-01-01 00:00:00.000"); | ||
| var endDate = DateTime.Parse("2015-01-01 00:00:00.000"); | ||
| var result = DateHelper.GetMonthsAndYearsBetweenDates(startDate, endDate).ToList(); | ||
|
|
||
| // then | ||
| result.Count.Should().Be(13); | ||
| result.Select(m => m.Month).Should().BeEquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1 }); | ||
| result.Take(12).Select(m => m.Year).Should().AllBeEquivalentTo(2014); | ||
| result[12].Year.Should().Be(2015); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
DigitalLearningSolutions.Data.Tests/Services/ActivityServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| namespace DigitalLearningSolutions.Data.Tests.Services | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using DigitalLearningSolutions.Data.DataServices; | ||
| using DigitalLearningSolutions.Data.Models.TrackingSystem; | ||
| using DigitalLearningSolutions.Data.Services; | ||
| using FakeItEasy; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| public class ActivityServiceTests | ||
| { | ||
| private IActivityDataService activityDataService = null!; | ||
| private IClockService clockService = null!; | ||
| private IActivityService activityService = null!; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| activityDataService = A.Fake<IActivityDataService>(); | ||
| clockService = A.Fake<IClockService>(); | ||
| activityService = new ActivityService(activityDataService, clockService); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetRecentActivity_gets_recent_activity() | ||
| { | ||
| // given | ||
| var expectedActivityResult = new List<MonthOfActivity>{new MonthOfActivity()}; | ||
| A.CallTo(() => activityDataService.GetActivityInRangeByMonth(A<int>._, A<DateTime>._, A<DateTime>._)) | ||
| .Returns(expectedActivityResult); | ||
| GivenCurrentTimeIs(DateTime.Parse("2015-12-22 06:52:09.080")); | ||
|
|
||
| // when | ||
| var result = activityService.GetRecentActivity(101).ToList(); | ||
|
|
||
| // then | ||
| A.CallTo(() => activityDataService.GetActivityInRangeByMonth(A<int>._, A<DateTime>._, A<DateTime>._)) | ||
| .MustHaveHappened(1, Times.Exactly); | ||
| result.Last().Should().BeEquivalentTo(new MonthOfActivity | ||
| { | ||
| Year = 2014, | ||
| Month = 12, | ||
| Completions = 0, | ||
| Registrations = 0, | ||
| Evaluations = 0 | ||
| }); | ||
| result.First().Should().BeEquivalentTo(new MonthOfActivity | ||
| { | ||
| Year = 2015, | ||
| Month = 12, | ||
| Completions = 0, | ||
| Registrations = 0, | ||
| Evaluations = 0 | ||
| }); | ||
| result.Count().Should().Be(13); | ||
| result.All(m => m.Completions == 0 && m.Evaluations == 0 && m.Registrations == 0).Should().BeTrue(); | ||
| } | ||
|
|
||
| private void GivenCurrentTimeIs(DateTime time) | ||
| { | ||
| A.CallTo(() => clockService.UtcNow).Returns(time); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
DigitalLearningSolutions.Data/DataServices/ActivityDataService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| namespace DigitalLearningSolutions.Data.DataServices | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Data; | ||
| using Dapper; | ||
| using DigitalLearningSolutions.Data.Models.TrackingSystem; | ||
|
|
||
| public interface IActivityDataService | ||
| { | ||
| IEnumerable<MonthOfActivity> GetActivityInRangeByMonth(int centreId, DateTime start, DateTime end); | ||
| } | ||
|
|
||
| public class ActivityDataService : IActivityDataService | ||
| { | ||
| private readonly IDbConnection connection; | ||
|
|
||
| public ActivityDataService(IDbConnection connection) | ||
| { | ||
| this.connection = connection; | ||
| } | ||
|
|
||
| public IEnumerable<MonthOfActivity> GetActivityInRangeByMonth(int centreId, DateTime start, DateTime end) | ||
| { | ||
| return connection.Query<MonthOfActivity>( | ||
| @"SELECT | ||
| LogYear AS Year, | ||
| LogMonth AS Month, | ||
| SUM(CONVERT(INT, Completed)) AS Completions, | ||
| SUM(CONVERT(INT, Evaluated)) AS Evaluations, | ||
| SUM(CONVERT(INT, Registered)) AS Registrations | ||
| FROM tActivityLog | ||
| WHERE (LogDate > @start AND LogDate < @end AND CentreID = @centreId) | ||
| GROUP BY LogYear, LogMonth | ||
| ORDER BY LogYear, LogMonth", | ||
| new { centreId, start, end } | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| namespace DigitalLearningSolutions.Web.Helpers | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public static class DateHelper | ||
| { | ||
| public static IEnumerable<(int Month, int Year)> GetMonthsAndYearsBetweenDates(DateTime startDate, DateTime endDate) | ||
| { | ||
| var diffInMonths = (endDate.Year - startDate.Year) * 12 + (endDate.Month - startDate.Month); | ||
| var monthEnumerable = Enumerable.Range(startDate.Month, diffInMonths + 1); | ||
|
|
||
| return monthEnumerable.Select( | ||
| m => | ||
| { | ||
| var month = (m - 1) % 12 + 1; | ||
| var yearsToAdd = (m - 1) / 12; | ||
| return (month, startDate.Year + yearsToAdd); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
DigitalLearningSolutions.Data/Models/TrackingSystem/MonthOfActivity.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| namespace DigitalLearningSolutions.Data.Models.TrackingSystem | ||
| { | ||
| public class MonthOfActivity | ||
| { | ||
| public MonthOfActivity() | ||
| { | ||
| Year = 0; | ||
| Month = 0; | ||
| Completions = 0; | ||
| Evaluations = 0; | ||
| Registrations = 0; | ||
| } | ||
|
|
||
| public MonthOfActivity((int Month, int Year) slot, MonthOfActivity? data) | ||
| { | ||
| Year = slot.Year; | ||
| Month = slot.Month; | ||
| Completions = data?.Completions ?? 0; | ||
| Evaluations = data?.Evaluations ?? 0; | ||
| Registrations = data?.Registrations ?? 0; | ||
| } | ||
|
|
||
| public int Year { get; set; } | ||
| public int Month { get; set; } | ||
| public int Completions { get; set; } | ||
| public int Evaluations { get; set; } | ||
| public int Registrations { get; set; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| namespace DigitalLearningSolutions.Data.Services | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using DigitalLearningSolutions.Data.DataServices; | ||
| using DigitalLearningSolutions.Data.Models.TrackingSystem; | ||
| using DigitalLearningSolutions.Web.Helpers; | ||
|
|
||
| public interface IActivityService | ||
| { | ||
| public IEnumerable<MonthOfActivity> GetRecentActivity(int centreId); | ||
| } | ||
|
|
||
| public class ActivityService : IActivityService | ||
| { | ||
| private readonly IActivityDataService activityDataService; | ||
| private readonly IClockService clockService; | ||
|
|
||
| public ActivityService(IActivityDataService activityDataService, IClockService clockService) | ||
| { | ||
| this.activityDataService = activityDataService; | ||
| this.clockService = clockService; | ||
| } | ||
|
|
||
| public IEnumerable<MonthOfActivity> GetRecentActivity(int centreId) | ||
stellake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var endTime = clockService.UtcNow; | ||
| var startTime = endTime.AddYears(-1); | ||
|
|
||
| var activityData = activityDataService.GetActivityInRangeByMonth(centreId, startTime, endTime).ToList(); | ||
|
|
||
| var monthSlots = DateHelper.GetMonthsAndYearsBetweenDates(startTime, endTime).ToList(); | ||
| monthSlots.Reverse(); | ||
|
|
||
| return monthSlots.Select( | ||
| slot => | ||
| { | ||
| var monthData = | ||
| activityData.SingleOrDefault(data => data.Year == slot.Year && data.Month == slot.Month); | ||
| return new MonthOfActivity(slot, monthData); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.