Skip to content

Commit

Permalink
Merge pull request #12 from surgeforward/AddUtilityCommands
Browse files Browse the repository at this point in the history
Add a Report Command
  • Loading branch information
jeremysawesome authored Jun 14, 2016
2 parents 5064b36 + 0f66f36 commit f93fd2a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 6 deletions.
45 changes: 42 additions & 3 deletions src/Nagger.Data/Local/LocalTimeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

public class LocalTimeRepository : LocalBaseRepository, ILocalTimeRepository
{
readonly ILocalTaskRepository _localTaskRepository;
readonly ILocalProjectRepository _localProjectRepository;
readonly ILocalTaskRepository _localTaskRepository;

public LocalTimeRepository(ILocalTaskRepository localTaskRepository, ILocalProjectRepository localProjectRepository)
public LocalTimeRepository(ILocalTaskRepository localTaskRepository,
ILocalProjectRepository localProjectRepository)
{
_localTaskRepository = localTaskRepository;
_localProjectRepository = localProjectRepository;
Expand Down Expand Up @@ -46,7 +47,7 @@ public TimeEntry GetLastTimeEntry(bool getInternal = false)
{
var internalWhere = (getInternal) ? "" : "WHERE Internal = 0";

cmd.CommandText = "SELECT * FROM TimeEntries "+internalWhere+" ORDER BY Id DESC LIMIT 1";
cmd.CommandText = "SELECT * FROM TimeEntries " + internalWhere + " ORDER BY Id DESC LIMIT 1";

using (var reader = cmd.ExecuteReader())
{
Expand Down Expand Up @@ -106,6 +107,44 @@ public IEnumerable<TimeEntry> GetUnsyncedEntries(bool getInternal = false)
return entries;
}

public IEnumerable<TimeEntry> GetTimeEntriesSince(DateTime time, bool getInternal = false)
{
var entries = new List<TimeEntry>();
using (var cnn = GetConnection())
using (var cmd = cnn.CreateCommand())
{
var cmdText = "SELECT * FROM TimeEntries WHERE TimeRecorded >= @time";
if (!getInternal) cmdText += " AND Internal = 0";
cmd.CommandText = cmdText;

cmd.Prepare();
cmd.Parameters.AddWithValue("@time", time);

using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var timeEntry = new TimeEntry
{
TimeRecorded =
DateTime.SpecifyKind(reader.GetDateTime(reader.GetOrdinal("TimeRecorded")),
DateTimeKind.Local),
Comment = reader.Get<string>("Comment"),
Id = reader.Get<int>("Id"),
Task = _localTaskRepository.GetTaskById(reader.Get<string>("TaskId")),
Synced = reader.Get<bool>("Synced"),
MinutesSpent = reader.Get<int>("MinutesSpent"),
Internal = reader.Get<bool>("Internal"),
Project = _localProjectRepository.GetProjectById(reader.Get<string>("ProjectId"))
};

entries.Add(timeEntry);
}
}
}
return entries;
}

public IEnumerable<string> GetRecentlyRecordedTaskIds(int limit)
{
var ids = new List<string>();
Expand Down
18 changes: 18 additions & 0 deletions src/Nagger.Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Nagger.Extensions
{
using System;

public static class DateTimeExtensions
{
// see http://stackoverflow.com/a/38064/296889
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
var diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1*diff).Date;
}
}
}
1 change: 1 addition & 0 deletions src/Nagger.Extensions/Nagger.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DateTimeExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="TimeSpanExtensions.cs" />
Expand Down
4 changes: 3 additions & 1 deletion src/Nagger.Interfaces/ILocalTimeRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Nagger.Interfaces
{
using System;
using System.Collections.Generic;
using Models;

Expand All @@ -13,7 +14,8 @@ public interface ILocalTimeRepository

TimeEntry GetLastTimeEntry(bool getInternal = false);
IEnumerable<TimeEntry> GetUnsyncedEntries(bool getInternal = false);
IEnumerable<TimeEntry> GetTimeEntriesSince(DateTime time, bool getInternal = false);
IEnumerable<string> GetRecentlyRecordedTaskIds(int limit);
IEnumerable<string> GetRecentlyRecordedCommentsForTaskId(int limit, string taskId);
}
}
}
2 changes: 2 additions & 0 deletions src/Nagger.Interfaces/ITimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface ITimeService
int IntervalsSinceLastRecord(bool justToday = true);
IEnumerable<string> GetRecentlyRecordedTaskIds(int limit);
IEnumerable<string> GetRecentlyRecordedCommentsForTask(int limit, Task task);

string GetTimeReport();
}
}
4 changes: 4 additions & 0 deletions src/Nagger.Services/CommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void ExecuteCommands(string[] args)
_timeService.DailyTimeOperations(true);
_outputService.ShowInformation("Push Has been run.");
break;
case "-report":
var report = _timeService.GetTimeReport();
_outputService.ShowInformation(report);
break;
}
}
}
Expand Down
38 changes: 36 additions & 2 deletions src/Nagger.Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensions;
using Interfaces;
using Models;

public class TimeService : ITimeService
{
readonly ILocalTimeRepository _localTimeRepository;
readonly IProjectService _projectService;
readonly IRemoteTimeRepository _remoteTimeRepository;
readonly ISettingsService _settingsService;
readonly IProjectService _projectService;

public TimeService(ILocalTimeRepository localTimeRepository, IRemoteTimeRepository remoteTimeRepository,
ISettingsService settingsService, IProjectService projectService)
Expand Down Expand Up @@ -102,7 +103,32 @@ public IEnumerable<string> GetRecentlyRecordedTaskIds(int limit)

public IEnumerable<string> GetRecentlyRecordedCommentsForTask(int limit, Task task)
{
return task?.Id == null ? new List<string>() : _localTimeRepository.GetRecentlyRecordedCommentsForTaskId(limit, task.Id);
return task?.Id == null
? new List<string>()
: _localTimeRepository.GetRecentlyRecordedCommentsForTaskId(limit, task.Id);
}

public string GetTimeReport()
{
SquashTime();

var workThisWeek = GetSpanOfWorkSince(DayOfWeek.Sunday);
var workToday = GetSpanOfWorkSince(DateTime.Today.DayOfWeek);

var hoursThisWeek = workThisWeek.Hours;
var minutesThisWeek = workThisWeek.Minutes;

var hoursToday = workToday.Hours;
var minutesToday = workToday.Minutes;

var builder = new StringBuilder();
builder.AppendLine("---This Week---");
builder.AppendFormat("{0} hours and {1} minutes", hoursThisWeek, minutesThisWeek);
builder.AppendLine();
builder.AppendLine("---Today---");
builder.AppendFormat("{0} hours and {1} minutes", hoursToday, minutesToday);

return builder.ToString();
}

public void DailyTimeOperations(bool force = false)
Expand Down Expand Up @@ -211,6 +237,14 @@ public void SquashTime()
RecordMarker(unsyncedEntries.Last().TimeRecorded);
}

TimeSpan GetSpanOfWorkSince(DayOfWeek dayOfWeek)
{
var weekStart = DateTime.Now.StartOfWeek(dayOfWeek);
var entries = _localTimeRepository.GetTimeEntriesSince(weekStart);

return TimeSpan.FromMinutes(entries.Sum(entry => entry.MinutesSpent));
}

static bool EntriesAreForSameWork(TimeEntry firstEntry, TimeEntry secondEntry)
{
// entries are considered for the same work if they have the same task and if they have the same comment
Expand Down

0 comments on commit f93fd2a

Please sign in to comment.