Esempio n. 1
0
        protected override List <Overtime> CalculateReportData()
        {
            var users       = _usersActiveInMonthReporter.Report();
            var attendances = _attendanceReporter.Report();

            var timeTrackingUsers = users.Where(x => x.IsTracking.HasValue && x.IsTracking.Value);
            var workHours         = _workHoursReporter.Report();

            Logger.Info("Calculating overtimes for {0} users", users.Count);

            var overtimes = attendances
                            .Where(x => x.Date.Month == _month && x.Date.Year == _year)
                            .GroupBy(x => x.User)
                            .Join(timeTrackingUsers, gu => gu.Key, u => u.Login, (gu, u) => new Overtime
            {
                Login            = gu.Key,
                HoursWorked      = gu.Sum(x => x.HoursWorked),
                ContractType     = u.ContractType,
                FirstName        = u.FirstName,
                LastName         = u.LastName,
                CostCenter       = u.CostCenter,
                Month            = _month,
                Year             = _year,
                Absences         = gu.Sum(x => x.AbsenceTotal),
                WorkHoursInMonth = CalculateWorkHoursInMonth(u, workHours)
            })
                            .ToList();


            return(overtimes);
        }
Esempio n. 2
0
        protected override List <IEnumerable <Sinner> > CalculateReportData()
        {
            Logger.Info("Resolving sinners");

            var users      = _userReporter.Report();
            var workLogs   = _worklogsReporter.Report();
            var attendance = _attendanceReporter.Report();

            //var worklogCountSinners = users.Join(workLogs, u => u.UserName, w => w.User, (u, w) => new
            //{
            //   u.UserName,
            //   u.IsTracking,
            //   w.Date,
            //   w.Hours
            //})
            //   .Where(uw => uw.Date.Equals(_dateOfSin) && uw.IsTracking)
            //   .GroupBy(uw => uw.UserName)
            //   .Select(guw =>
            //      new WorklogCountSinner
            //      {
            //         TotalHours = guw.Sum(x => x.Hours),
            //         WorklogCount = guw.Count(),
            //         SinDate = _dateOfSin,
            //         SinnerLogin = guw.Key
            //      })
            //   .Where(wcs => wcs.WorklogCount < WorklogCountSinner.CountThreshold);

            var longWorklogSinners = workLogs
                                     .Where(x => x.Date.Equals(_dateOfSin) && x.Hours >= LongWorklogSinner.LongWorklogThreshold)
                                     .Join(users, w => w.User, u => u.Login, (w, u) => new { w.Hours, w.User, u.IsTracking })
                                     .Where(x => x.IsTracking.HasValue && x.IsTracking.Value)
                                     .Select(x => new LongWorklogSinner
            {
                SinnerLogin = x.User,
                Hours       = x.Hours,
                SinDate     = _dateOfSin
            });

            var timeTrackedSinners = attendance
                                     .Where(x => x.Date.Equals(_dateOfSin) && (x.TotalHours <TimeTrackedSinner.LowHoursThreshold ||
                                                                                             x.TotalHours> TimeTrackedSinner.HighHoursThreshold))
                                     .Join(users, a => a.User, u => u.Login,
                                           (a, u) => new { a.User, a.AbsenceTotal, a.TotalHours, a.HoursWorked, u.IsTracking })
                                     .Where(x => x.IsTracking.HasValue && x.IsTracking.Value)
                                     .Select(x => new TimeTrackedSinner
            {
                SinnerLogin = x.User,
                Absence     = x.AbsenceTotal,
                TotalHours  = x.TotalHours,
                TimeTracked = x.HoursWorked,
                SinDate     = _dateOfSin
            });

            var noTimeTrackedSinners = attendance
                                       .Where(x => x.Date.Equals(_dateOfSin) && x.TotalHours == 0)
                                       .Join(users, a => a.User, u => u.Login,
                                             (a, u) => new { a.User, u.IsTracking })
                                       .Where(x => x.IsTracking.HasValue && x.IsTracking.Value)
                                       .Select(x => new NoTimeTrackedSinner
            {
                SinnerLogin = x.User,
                SinDate     = _dateOfSin
            });

            var sinners = new List <IEnumerable <Sinner> >
            {
                longWorklogSinners,
                timeTrackedSinners,
                noTimeTrackedSinners
            };

            return(sinners);
        }
Esempio n. 3
0
        protected override List <SalaryData> CalculateReportData()
        {
            Logger.Info("Calculating salary data");
            var jiraAbsences = _jiraAbsenceReporter.Report();

            var users         = _usersActiveInMonthReporter.Report();
            var salariedUsers = users.Where(x => SalariedContractTypes.Contains(x.GetContractType())).ToList();

            Logger.Info("Found {0} salaried users", salariedUsers.Count);

            var relevantAttendance = _attendanceReporter.Report()
                                     .Where(x => x.Date.Year == _year && x.Date.Month == _month)
                                     .GroupBy(x => x.User)
                                     .Select(x => new
            {
                User                      = x.Key,
                HoursWorkedTotal          = x.Sum(y => y.HoursWorked),
                AbsenceVacationTotal      = x.Sum(y => y.AbsenceVacation),
                AbsenceDoctorTotal        = x.Sum(y => y.AbsenceDoctor),
                AbsenceDoctorFamilyTotal  = x.Sum(y => y.AbsenceDoctorFamily),
                AbsencePersonalLeaveTotal = x.Sum(y => y.AbsencePersonalLeave)
            })
                                     .Join(salariedUsers, a => a.User, u => u.Login,
                                           (a, u) => new
            {
                u.Login,
                u.Title,
                u.StartDate,
                u.Benefit,
                u.FirstName,
                u.LastName,
                u.Salary,
                u.Rate,
                ContractType = u.GetContractType(),
                a.HoursWorkedTotal,
                a.AbsenceVacationTotal,
                a.AbsenceDoctorTotal,
                a.AbsenceDoctorFamilyTotal,
                a.AbsencePersonalLeaveTotal
            });

            var result = relevantAttendance.Select(x =>
            {
                decimal?salary = null;
                if (x.ContractType == ContractType.Employee)
                {
                    salary = x.Salary;
                }
                else if (x.ContractType == ContractType.PartTimeEmployee)
                {
                    salary = x.Rate * x.HoursWorkedTotal;
                }

                return(new SalaryData
                {
                    Year = _year,
                    Month = _month,
                    Title = x.Title,
                    StartDate = x.StartDate,
                    Benefit = x.Benefit,
                    FirstName = x.FirstName,
                    Login = x.Login,
                    LastName = x.LastName,
                    DoctorFamilyHours = x.AbsenceDoctorFamilyTotal,
                    DoctorHours = x.AbsenceDoctorTotal,
                    VacationDays = x.AbsenceVacationTotal / 8,
                    Salary = salary,
                    IllnessDays = CalculateIllnessDays(x.Login, jiraAbsences),
                    PersonalLeaveDays = x.AbsencePersonalLeaveTotal / 8
                });
            });

            return(result.ToList());
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Config config = JsonConvert.DeserializeObject <Config>(File.ReadAllText(ConfigFilePath));

            AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
            {
                Exception exception = eventArgs.ExceptionObject as Exception;
                Logger.Fatal(exception);

                ErrorNotificationGmail errorNotificationGmail = new ErrorNotificationGmail(config.ErrorGmailSettings);
                errorNotificationGmail.Write(exception);
            };
            DateTime runStartDateTime = DateTime.Now;

            DateTime from = config.DateFrom ?? new DateTime(DateTime.Now.Year, 1, 1);
            DateTime till = config.DateTo ?? new DateTime(DateTime.Now.Year, 12, 31);
            DateTime considerWorklogsFrom = DateTime.Now.AddMonths(-config.MonthsToLog).Date;

            PublicHolidayReporter publicHolidayReporter = new PublicHolidayReporter(config.PublicHolidayApiKey, Enumerable.Range(2016, till.Year - 2016 + 1).ToList());

            var shouldReportSinnersToday = !DateTimeUtils.IsNonWorkingDay(publicHolidayReporter.Report(), DateTime.Now);

            Logger.Info("Running for date range from {0} to {1}", from, till);

            JiraApiClient client = new JiraApiClient(config.JiraSettings);

            JiraProjectReporter projectsReporter = new JiraProjectReporter(client);
            var projectsWriter = new ReportWriter <List <JiraProject> >(projectsReporter, new ProjectSheet(config.ProjectSheetSettings));

            projectsWriter.Write();

            var rawUserDataReporter      = new RawUserDataReporter(new RawUserDataSheet(config.UsersSheetSettings));
            var freshestUserDataReporter = new FreshestUserDataReporter(rawUserDataReporter);

            WorklogsReporter fullInitWorklogsReporter = new WorklogsReporter(freshestUserDataReporter, client, new DateTime(2017, 1, 1), considerWorklogsFrom);

            WorklogsReporter currentRangeWorklogsReporter = new WorklogsReporter(freshestUserDataReporter, client, from, till);

            JiraAbsenceReporter jiraAbsenceReporter = new JiraAbsenceReporter(freshestUserDataReporter, client);

            AbsenceReporter absenceReporter = new AbsenceReporter(publicHolidayReporter, jiraAbsenceReporter);

            AttendanceReporter attendanceReporter = new AttendanceReporter(freshestUserDataReporter, absenceReporter, currentRangeWorklogsReporter, from, till);

            var attendanceReportWriter = new ReportWriter <List <Attendance> >(attendanceReporter, new AttendanceGridSheet(config.AttendanceGridSheetSettings));

            attendanceReportWriter.Write();

            if (shouldReportSinnersToday)
            {
                var dateOfSin            = DateTimeUtils.GetLastWorkDay(publicHolidayReporter.Report(), DateTime.Now.Date.AddDays(-1));
                var currentUsersReporter = new CurrentUsersReporter(freshestUserDataReporter);

                SinnersReporter sinnersReporter = new SinnersReporter(currentUsersReporter, currentRangeWorklogsReporter, attendanceReporter, dateOfSin);

                var gSheetSinnerReportWriter = new ReportWriter <List <IEnumerable <Sinner> > >(sinnersReporter, new SinnerSheet(config.SinnersSheetSettings));
                gSheetSinnerReportWriter.Write();

                var gmailSinnerReportWriter = new ReportWriter <List <IEnumerable <Sinner> > >(sinnersReporter, new SinnerGmail(config.SinnerNotifierGmailSettings, dateOfSin));
                gmailSinnerReportWriter.Write();
            }
            else
            {
                Logger.Info("Tool wont report sinners because it is weekend or holiday!");
            }

            Logger.Info("Cleaning and filling database");
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges <JiraToolDbContext>());
            using (JiraToolDbContext ctx = new JiraToolDbContext())
            {
                using (var transaction = ctx.Database.BeginTransaction())
                {
                    InsertInitialWorklogs(ctx, fullInitWorklogsReporter);
                    ClearDatabase(ctx, considerWorklogsFrom);
                    WorklogsReporter updateReporter = new WorklogsReporter(freshestUserDataReporter, client, GetDateTimeForUpdate(ctx, considerWorklogsFrom), DateTime.Now);
                    ctx.Absences.AddRange(absenceReporter.Report().Select(AbsenceMapper.ToModel));
                    ctx.Worklogs.AddRange(updateReporter.Report().Select(WorklogMapper.ToModel));
                    ctx.Attendance.AddRange(attendanceReporter.Report().Select(AttendanceMapper.ToModel));
                    ctx.Users.AddRange(freshestUserDataReporter.Report().Select(UserMapper.ToModel));
                    ctx.SaveChanges();
                    transaction.Commit();
                }
            }
            WorklogsFromDbReporter   fullHistoryWorklogsReporter = new WorklogsFromDbReporter(freshestUserDataReporter, new JiraToolDbContext(), new DateTime(2017, 1, 1), DateTime.Now);
            ProjectTimeSpentReporter projectTimeSpentReporter    = new ProjectTimeSpentReporter(fullHistoryWorklogsReporter);
            var projectTimeSpentWriter = new ReportWriter <Dictionary <string, decimal> >(projectTimeSpentReporter, new ProjectTimeSpentSheet(config.ProjectTimeSpentSheetSettings));

            projectTimeSpentWriter.Write();

            DateTime runEndDateTime = DateTime.Now;

            ToolRunReporter        runReporter   = new ToolRunReporter(runStartDateTime, runEndDateTime);
            ReportWriter <ToolRun> toolRunWriter = new ReportWriter <ToolRun>(runReporter, new RunLogSheet(config.RunLogSheetSettings));

            toolRunWriter.Write();

            Logger.Info("Tool run finished");
        }