diff --git a/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs b/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs new file mode 100644 index 0000000..61c9a2c --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/Interfaces/DailyLogs.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace Interfaces +{ + public class DailyLogs + { + public DailyLogs() + { + Logs = new List(); + } + public int LogCount { get { return Logs.Count; } } + public double DailyTotal { get; set; } + public List Logs { get; set; } + } +} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs b/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs index 0d44949..92fa59b 100644 --- a/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs +++ b/DataCenter_Windows/WindowsDataCenter/Interfaces/TimeLogList.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Interfaces { @@ -7,11 +8,14 @@ namespace Interfaces { public TimeLogList() { - TimeLogs = new List(); + TimeLogs = new Dictionary(); } public DateTime SelectedDate { get; set; } public int CalendarWeek { get; set; } - public int TimeLogCount { get { return TimeLogs.Count; } } - public List TimeLogs { get; set; } + public int TimeLogCount { get { return TimeLogs.Values.Sum(x => x.LogCount); } } + public Dictionary TimeLogs { get; set; } + public double WeeklyTotal { + get { return TimeLogs.Values.Sum(x => x.DailyTotal)/60.0d; } + } } } \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs index e9912d5..6d504fb 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteRepository.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Security.Cryptography.X509Certificates; using Interfaces; using SQLite.Net; using SQLite.Net.Platform.Win32; @@ -32,7 +31,7 @@ namespace SQLiteRepository { var ret = new UserList(); List users; - int userCount = -1; + int userCount; if (pageNumber == -1 && pageSize == -1) { users = _connection.Query(SQLiteProcedures.GET_ALL_USERS); @@ -365,11 +364,12 @@ namespace SQLiteRepository return OperationResponse.SUCCESS; } - private List GetTimeLogList(int userId, int calendarWeek, int year) + private Dictionary GetTimeLogList(int userId, int calendarWeek, int year) { var timeLogList = _connection.Query( SQLiteProcedures.GET_TIMELOGS, userId, calendarWeek, year); + var timeLogs = timeLogList.Select(x => new TimeLog { Id = x.Id, @@ -381,21 +381,60 @@ namespace SQLiteRepository Year = x.Year }).ToList(); - var dict = new Dictionary>(); - foreach (var log in timeLogs) + var dict = new Dictionary(); + + //make sure each day of the week is accounted for in the dictionary. + foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))) { - var dow = log.EventTime.DayOfWeek; - if (!dict.ContainsKey(dow)) - { - dict.Add(dow, new List()); - } - dict[dow].Add(log); + dict.Add(day, new DailyLogs()); } - var ret = timeLogs; - return ret; + //add the logs to the respective day of the week. + foreach (var log in timeLogs) + { + dict[log.EventTime.DayOfWeek].Logs.Add(log); + } + + foreach (var dailyCollection in dict) + { + dailyCollection.Value.DailyTotal = CalculateDailyTotal(dailyCollection.Value); + } + + return dict.OrderBy(x => ((int)x.Key + 6) % 7).ToDictionary(x=>x.Key, x=>x.Value); } + 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); + } + + /// + /// determines if the number is an odd or even value + /// + /// number to determine is odd or even + /// true - number is odd + private bool IsOdd(int value) + { + return value % 2 != 0; + } + + /// + /// Get the new direction for the user based on previous entry logs in the system. + /// + /// + /// 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") + /// + /// Id of the user to get the log direction of. + /// indicating what direction the new log is. private LogDirectionDb GetLogDirection(int userId) { var logDirection = LogDirectionDb.UNKNOWN;