👊 card statistics 2.0

This commit is contained in:
Amy Palamountain
2014-02-15 15:15:20 +13:00
parent 63da057642
commit ec5db2cdd5
13 changed files with 137 additions and 12 deletions
@@ -73,12 +73,13 @@ namespace Octokit.Tests.Integration.Clients
}
[IntegrationTest]
public async Task CanGetCommitPerHourPerDay()
public async Task CanGetPunchCardForRepository()
{
var repository = await CreateRepository();
await CommitToRepository(repository);
var hourlyCommits = await _client.Statistics.GetCommitPerHour(repository.Owner, repository.Name);
Assert.NotNull(hourlyCommits);
var punchCard = await _client.Statistics.GetPunchCard(repository.Owner, repository.Name);
Assert.NotNull(punchCard);
Assert.NotNull(punchCard.PunchPoints);
}
async Task<RepositorySummary> CreateRepository()
@@ -148,7 +148,7 @@ namespace Octokit.Tests.Clients
var client = Substitute.For<IApiConnection>();
var statisticsClient = new StatisticsClient(client);
statisticsClient.GetCommitPerHour("username", "repositoryName");
statisticsClient.GetPunchCard("username", "repositoryName");
client.Received().GetQueuedOperation<IEnumerable<int[]>>(expectedEndPoint, Args.CancellationToken);
}
@@ -157,14 +157,14 @@ namespace Octokit.Tests.Clients
public async Task ThrowsIfGivenNullOwner()
{
var statisticsClient = new StatisticsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(() => statisticsClient.GetCommitPerHour(null, "repositoryName"));
await AssertEx.Throws<ArgumentNullException>(() => statisticsClient.GetPunchCard(null, "repositoryName"));
}
[Fact]
public async Task ThrowsIfGivenNullRepositoryName()
{
var statisticsClient = new StatisticsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(() => statisticsClient.GetCommitPerHour("owner", null));
await AssertEx.Throws<ArgumentNullException>(() => statisticsClient.GetPunchCard("owner", null));
}
}
}
+53
View File
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using Octokit.Response;
using Xunit;
namespace Octokit.Tests.Models
{
public class PunchCardTests
{
[Fact]
public void ThrowsExceptionWithNullPunchCardPoints()
{
Assert.Throws<ArgumentNullException>(()=>new PunchCard(null));
}
[Fact]
public void ThrowsExceptionWhenPunchCardPointsHaveIncorrectFormat()
{
IList<int> point1 = new []{1,2,3,4,5,6};
IEnumerable<IList<int>> points = new List<IList<int>>{point1};
Assert.Throws<ArgumentException>(() => new PunchCard(points));
}
[Fact]
public void DoesNotThrowExceptionWhenPunchPointsHaveCorrectFormat()
{
IList<int> point1 = new[] { 1, 2, 3};
IEnumerable<IList<int>> points = new List<IList<int>> { point1 };
Assert.DoesNotThrow(() => new PunchCard(points));
}
[Fact]
public void CanQueryCommitsForDayAndHour()
{
IList<int> point1 = new[] { 1, 0, 3 };
IList<int> point2 = new[] { 1, 1, 4 };
IList<int> point3 = new[] { 1, 2, 0 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1,point2,point3 };
var punchCard = new PunchCard(points);
var commitsAtMondayAt12Am = punchCard.GetCommitCountFor(DayOfWeek.Monday, 0);
var commitsAtMondayAt1Am = punchCard.GetCommitCountFor(DayOfWeek.Monday, 1);
var commitsAtMondayAt2Am = punchCard.GetCommitCountFor(DayOfWeek.Monday, 2);
var commitsAtTuesdayAt2Am = punchCard.GetCommitCountFor(DayOfWeek.Tuesday, 2);
Assert.Equal(3,commitsAtMondayAt12Am);
Assert.Equal(4, commitsAtMondayAt1Am);
Assert.Equal(0, commitsAtMondayAt2Am);
Assert.Equal(0, commitsAtTuesdayAt2Am);
}
}
}
+1
View File
@@ -127,6 +127,7 @@
<Compile Include="Models\MilestoneRequestTests.cs" />
<Compile Include="Models\IssueRequestTests.cs" />
<Compile Include="Models\ModelExtensionsTests.cs" />
<Compile Include="Models\PunchCardTests.cs" />
<Compile Include="Models\ReadOnlyPagedCollectionTests.cs" />
<Compile Include="Models\RequestParametersTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+3 -2
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Octokit.Response;
namespace Octokit
{
@@ -80,7 +81,7 @@ namespace Octokit
/// <param name="owner">The owner of the repository</param>
/// <param name="repositoryName">The name of the repository</param>
/// <returns>Returns commit counts per hour in each day</returns>
Task<IEnumerable<int[]>> GetCommitPerHour(string owner, string repositoryName);
Task<PunchCard> GetPunchCard(string owner, string repositoryName);
/// <summary>
/// Returns a list of the number of commits per hour in each day
@@ -89,6 +90,6 @@ namespace Octokit
/// <param name="repositoryName">The name of the repository</param>
/// <param name="cancellationToken">A token used to cancel this potentially long running request</param>
/// <returns>Returns commit counts per hour in each day</returns>
Task<IEnumerable<int[]>> GetCommitPerHour(string owner, string repositoryName, CancellationToken cancellationToken);
Task<PunchCard> GetPunchCard(string owner, string repositoryName, CancellationToken cancellationToken);
}
}
+6 -4
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Octokit.Response;
namespace Octokit
{
@@ -128,9 +129,9 @@ namespace Octokit
/// <param name="owner">The owner of the repository</param>
/// <param name="repositoryName">The name of the repository</param>
/// <returns>Returns commit counts per hour in each day</returns>
public Task<IEnumerable<int[]>> GetCommitPerHour(string owner, string repositoryName)
public Task<PunchCard> GetPunchCard(string owner, string repositoryName)
{
return GetCommitPerHour(owner, repositoryName,CancellationToken.None);
return GetPunchCard(owner, repositoryName,CancellationToken.None);
}
/// <summary>
@@ -140,13 +141,14 @@ namespace Octokit
/// <param name="repositoryName">The name of the repository</param>
/// <param name="cancellationToken">A token used to cancel this potentially long running request</param>
/// <returns>Returns commit counts per hour in each day</returns>
public async Task<IEnumerable<int[]>> GetCommitPerHour(string owner, string repositoryName, CancellationToken cancellationToken)
public async Task<PunchCard> GetPunchCard(string owner, string repositoryName, CancellationToken cancellationToken)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName");
var endpoint = "/repos/{0}/{1}/stats/punch_card".FormatUri(owner, repositoryName);
return await ApiConnection.GetQueuedOperation<IEnumerable<int[]>>(endpoint, cancellationToken);
var punchCardData = await ApiConnection.GetQueuedOperation<IEnumerable<int[]>>(endpoint, cancellationToken);
return new PunchCard(punchCardData);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Octokit.Response
{
public class PunchCard
{
public PunchCard(IEnumerable<IList<int>> punchCardData)
{
Ensure.ArgumentNotNull(punchCardData, "punchCardData");
PunchPoints = punchCardData.Select(point => new PunchCardPoint(point)).ToList();
}
/// <summary>
/// The raw punch card points
/// </summary>
public IReadOnlyCollection<PunchCardPoint> PunchPoints { get; private set; }
/// <summary>
/// Gets the number of commits made on the specified day of the week
/// at the hour of the day, over the lifetime of this repository
/// </summary>
/// <param name="dayOfWeek">The day of the week to query</param>
/// <param name="hourOfDay">The hour in 24 hour time. 0-23.</param>
/// <returns>The total number of commits made.</returns>
public int GetCommitCountFor(DayOfWeek dayOfWeek, int hourOfDay)
{
var punchPoint = PunchPoints.SingleOrDefault(point => point.DayOfWeek == dayOfWeek && point.HourOfTheDay == hourOfDay);
return punchPoint == null ? 0 : punchPoint.CommitCount;
}
}
}
+24
View File
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
namespace Octokit.Response
{
public class PunchCardPoint
{
public PunchCardPoint(IList<int> punchPoint)
{
Ensure.ArgumentNotNull(punchPoint, "punchPoint");
if (punchPoint.Count != 3)
{
throw new ArgumentException("Daily punch card must only contain three data points.");
}
DayOfWeek = (DayOfWeek)punchPoint[0];
HourOfTheDay = punchPoint[1];
CommitCount = punchPoint[2];
}
public DayOfWeek DayOfWeek { get; private set; }
public int HourOfTheDay { get; private set; }
public int CommitCount { get; private set; }
}
}
+2
View File
@@ -273,6 +273,8 @@
<Compile Include="Clients\FollowersClient.cs" />
<Compile Include="Clients\IFollowersClient.cs" />
<Compile Include="Models\Request\ReleaseAssetUpdate.cs" />
<Compile Include="Models\Response\PunchCard.cs" />
<Compile Include="Models\Response\PunchCardPoint.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+2
View File
@@ -283,6 +283,8 @@
<Compile Include="Models\Response\WeeklyCommitActivity.cs" />
<Compile Include="Models\Response\WeeklyCommitCounts.cs" />
<Compile Include="Models\Response\WeeklyHash.cs" />
<Compile Include="Models\Response\PunchCard.cs" />
<Compile Include="Models\Response\PunchCardPoint.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+2
View File
@@ -278,6 +278,8 @@
<Compile Include="Models\Response\WeeklyCommitActivity.cs" />
<Compile Include="Models\Response\WeeklyCommitCounts.cs" />
<Compile Include="Models\Response\WeeklyHash.cs" />
<Compile Include="Models\Response\PunchCard.cs" />
<Compile Include="Models\Response\PunchCardPoint.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+2
View File
@@ -271,6 +271,8 @@
<Compile Include="Clients\IUserEmailsClient.cs" />
<Compile Include="Clients\UserEmailsClient.cs" />
<Compile Include="Models\Response\Emoji.cs" />
<Compile Include="Models\Response\PunchCard.cs" />
<Compile Include="Models\Response\PunchCardPoint.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
+2
View File
@@ -126,6 +126,8 @@
<Compile Include="Models\Response\Emoji.cs" />
<Compile Include="Models\Response\Contributor.cs" />
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Response\PunchCard.cs" />
<Compile Include="Models\Response\PunchCardPoint.cs" />
<Compile Include="Models\Response\Reference.cs" />
<Compile Include="Models\Response\Subscription.cs" />
<Compile Include="Models\Response\TreeItem.cs" />