Added method to calculate the daily logs.

extended TimeLogList object:
 > to group logs into days with the "DailyLogs" object
 > to calculate the WeeklyTotal hours automatically based on the TimeLogs.DailyLogs object for each day.

Created DailyLogs object:
 > holds the number of entries for a day.
 > holds the time "in"/"at work" for that day (dailytotal).

#15
This commit is contained in:
Chris.Watts90@outlook.com 2017-02-14 17:06:16 +00:00
parent eb1ba4157a
commit 69aaf53bb4
3 changed files with 74 additions and 16 deletions

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace Interfaces
{
public class DailyLogs
{
public DailyLogs()
{
Logs = new List<TimeLog>();
}
public int LogCount { get { return Logs.Count; } }
public double DailyTotal { get; set; }
public List<TimeLog> Logs { get; set; }
}
}

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Interfaces namespace Interfaces
{ {
@ -7,11 +8,14 @@ namespace Interfaces
{ {
public TimeLogList() public TimeLogList()
{ {
TimeLogs = new List<TimeLog>(); TimeLogs = new Dictionary<DayOfWeek, DailyLogs>();
} }
public DateTime SelectedDate { get; set; } public DateTime SelectedDate { get; set; }
public int CalendarWeek { get; set; } public int CalendarWeek { get; set; }
public int TimeLogCount { get { return TimeLogs.Count; } } public int TimeLogCount { get { return TimeLogs.Values.Sum(x => x.LogCount); } }
public List<TimeLog> TimeLogs { get; set; } public Dictionary<DayOfWeek,DailyLogs> TimeLogs { get; set; }
public double WeeklyTotal {
get { return TimeLogs.Values.Sum(x => x.DailyTotal)/60.0d; }
}
} }
} }

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Interfaces; using Interfaces;
using SQLite.Net; using SQLite.Net;
using SQLite.Net.Platform.Win32; using SQLite.Net.Platform.Win32;
@ -32,7 +31,7 @@ namespace SQLiteRepository
{ {
var ret = new UserList(); var ret = new UserList();
List<UserIdentity> users; List<UserIdentity> users;
int userCount = -1; int userCount;
if (pageNumber == -1 && pageSize == -1) if (pageNumber == -1 && pageSize == -1)
{ {
users = _connection.Query<UserIdentity>(SQLiteProcedures.GET_ALL_USERS); users = _connection.Query<UserIdentity>(SQLiteProcedures.GET_ALL_USERS);
@ -365,11 +364,12 @@ namespace SQLiteRepository
return OperationResponse.SUCCESS; return OperationResponse.SUCCESS;
} }
private List<TimeLog> GetTimeLogList(int userId, int calendarWeek, int year) private Dictionary<DayOfWeek, DailyLogs> GetTimeLogList(int userId, int calendarWeek, int year)
{ {
var timeLogList = _connection.Query<TimeLogDb>( var timeLogList = _connection.Query<TimeLogDb>(
SQLiteProcedures.GET_TIMELOGS, SQLiteProcedures.GET_TIMELOGS,
userId, calendarWeek, year); userId, calendarWeek, year);
var timeLogs = timeLogList.Select(x => new TimeLog var timeLogs = timeLogList.Select(x => new TimeLog
{ {
Id = x.Id, Id = x.Id,
@ -381,21 +381,60 @@ namespace SQLiteRepository
Year = x.Year Year = x.Year
}).ToList(); }).ToList();
var dict = new Dictionary<DayOfWeek, List<TimeLog>>(); var dict = new Dictionary<DayOfWeek, DailyLogs>();
//make sure each day of the week is accounted for in the dictionary.
foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
{
dict.Add(day, new DailyLogs());
}
//add the logs to the respective day of the week.
foreach (var log in timeLogs) foreach (var log in timeLogs)
{ {
var dow = log.EventTime.DayOfWeek; dict[log.EventTime.DayOfWeek].Logs.Add(log);
if (!dict.ContainsKey(dow)) }
foreach (var dailyCollection in dict)
{ {
dict.Add(dow, new List<TimeLog>()); dailyCollection.Value.DailyTotal = CalculateDailyTotal(dailyCollection.Value);
}
dict[dow].Add(log);
} }
var ret = timeLogs; return dict.OrderBy(x => ((int)x.Key + 6) % 7).ToDictionary(x=>x.Key, x=>x.Value);
return ret;
} }
private double CalculateDailyTotal(DailyLogs dailyLogs)
{
var totalInTime = TimeSpan.FromSeconds(0);
var logs = dailyLogs.Logs.OrderBy(x => x.Id).ToArray();
var totalCalcMax = IsOdd(logs.Length) ? logs.Length - 1 : logs.Length;
for (int i = 0; i < totalCalcMax; i += 2)
{
totalInTime += (logs[i + 1].EventTime - logs[i].EventTime);
}
return Math.Round(totalInTime.TotalMinutes, 2);
}
/// <summary>
/// determines if the number is an odd or even value
/// </summary>
/// <param name="value">number to determine is odd or even</param>
/// <returns>true - number is odd</returns>
private bool IsOdd(int value)
{
return value % 2 != 0;
}
/// <summary>
/// Get the new direction for the user based on previous entry logs in the system.
/// </summary>
/// <remarks>
/// If the user has not logged in today, the direction will be In.
/// If the user has logged in already today, the direction will be the opposite of the last
/// recorded log direction. ("out" if "in", "in" if "out")
/// </remarks>
/// <param name="userId">Id of the user to get the log direction of.</param>
/// <returns><see cref="LogDirectionDb"/> indicating what direction the new log is.</returns>
private LogDirectionDb GetLogDirection(int userId) private LogDirectionDb GetLogDirection(int userId)
{ {
var logDirection = LogDirectionDb.UNKNOWN; var logDirection = LogDirectionDb.UNKNOWN;