コード例 #1
0
ファイル: Program.cs プロジェクト: csMACnz/TogglHeatmap
        public static RunResult RunApp(RunSettings settings)
        {
            if (!File.Exists(settings.FileName))
            {
                Console.Error.WriteLine($"Could not find file '{settings.FileName}'");
                return(RunResult.FileNotFound);
            }

            var data = new List <TimeEntryData>();

            using (Stream stream = File.OpenRead(settings.FileName))
            {
                using (var reader = new StreamReader(stream))
                {
                    reader.ReadLine();
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        var      row       = line.Split(',');
                        var      startDate = row[7];
                        var      startTime = row[8];
                        var      endDate   = row[9];
                        var      endTime   = row[10];
                        var      duration  = row[11];
                        DateTime start     = DateTime.Parse($"{startDate} {startTime}");
                        DateTime end       = DateTime.Parse($"{endDate} {endTime}");

                        data.Add(new TimeEntryData {
                            Start = start, End = end, Duration = duration
                        });
                    }
                }
            }
            var sorted = new Dictionary <DateTime, List <TimeEntryData> >();

            foreach (var dataItem in data)
            {
                AddItem(sorted, dataItem.Start.Date, dataItem);
                if (dataItem.Start.Date != dataItem.End.Date)
                {
                    var date = dataItem.Start.Date;
                    while (date < dataItem.End.Date)
                    {
                        date = date.AddDays(1);
                        AddItem(sorted, date.Date, dataItem);
                    }
                }
            }

            Console.WriteLine($"Data from {settings.StartDate} to {settings.EndDate}");
            string firstDayOfWeek = Enum.GetName(typeof(DayOfWeek), settings.StartDate.DayOfWeek);

            Console.WriteLine($"First day: {firstDayOfWeek}");
            Console.WriteLine($"Number of weeks: {settings.NumberOfWeeks}");
            Console.WriteLine("----");
            var weekdayNames = Enum.GetNames(typeof(DayOfWeek));
            var pivotIndex   = weekdayNames
                               .Select((v, i) => (index: i, day: v))
                               .Where(t => t.day == firstDayOfWeek)
                               .Select(t => t.index)
                               .First();

            var daysList = new[] { "Time" }
            .Union(weekdayNames.Skip(pivotIndex).Take(7 - pivotIndex))
            .Union(weekdayNames.Take(pivotIndex))
            .ToList();

            Console.WriteLine(string.Join(",", daysList));

            var allWeeksData = Enumerable
                               .Range(0, settings.NumberOfWeeks)
                               .Select(i => settings.StartDate.AddDays(i * 7))
                               .Select(date => GetData(date, sorted))
                               .ToList();

            var results = new int[7, 48];

            foreach (var week in allWeeksData)
            {
                foreach (var day in week.Select((x, i) => new { WeekDay = i, Value = x }))
                {
                    foreach (var segment in day.Value.Select((x, i) => new { WeekDay = day.WeekDay, Halfhour = i, HasData = x }))
                    {
                        if (segment.HasData)
                        {
                            results[segment.WeekDay, segment.Halfhour] += 1;
                        }
                    }
                }
            }

            //Report
            for (var halfHour = 0; halfHour < 48; halfHour++)
            {
                var values = new List <string>();
                values.Add($"{halfHour / 2:00}:{(halfHour % 2 == 0 ? 0 : 30):00}");
                for (int day = 0; day < 7; day++)
                {
                    var count = results[day, halfHour];
                    values.Add(count.ToString());
                }
                Console.WriteLine(string.Join(",", values));
            }
            Console.WriteLine("----");
            Console.WriteLine();
            Console.WriteLine($"Copy the data above into a csv file to make it pretty in a spreadsheet");
            return(0);
        }
コード例 #2
0
        public static Either <RunResult, RunSettings> ParseArgs(string[] args)
        {
            if (args.Length < 1 || args.Length > 3)
            {
                Console.Error.WriteLine("Invalid Arguments");
                PrintUsage();
                return(RunResult.InvalidArgs);
            }
            else if (args.Length == 1 && args[0].StartsWith("--"))
            {
                var argument = args[0];
                switch (argument)
                {
                case "--help":
                    PrintUsage();
                    return(RunResult.Ok);

                case "--version":
                    Console.WriteLine(GetDisplayVersion());
                    return(RunResult.Ok);

                default:
                    Console.Error.WriteLine($"Unknown Argument '{argument}'");
                    PrintUsage();
                    return(RunResult.UnknownArgument);
                }
            }
            else if (args.Length != 3)
            {
                Console.Error.WriteLine("Invalid Arguments");
                PrintUsage();
                return(RunResult.InvalidArgs);
            }
            var      filePath       = args[0];
            var      startDateInput = args[1];
            DateTime actualStartDate;

            if (DateTime.TryParse(startDateInput, out actualStartDate))
            {
                actualStartDate = actualStartDate.Date;
            }
            else
            {
                Console.Error.WriteLine($"Invalid Start Date '{startDateInput}'");
                PrintUsage();
                return(RunResult.IncorrectArgsFormat);
            }

            var         endRangeInput = args[2];
            RunSettings settings;

            if (DateTime.TryParse(endRangeInput, out var finalDate))
            {
                settings = new RunSettings(filePath, actualStartDate, finalDate.Date);
            }
            else if (int.TryParse(endRangeInput, out var numberOfWeeks))
            {
                settings = new RunSettings(filePath, actualStartDate, numberOfWeeks);
            }
            else
            {
                Console.Error.WriteLine($"The argument '{endRangeInput}' must be either a Number (week count) or a DateTime (range end date)");
                PrintUsage();
                return(RunResult.IncorrectArgsFormat);
            }

            return(settings);
        }