def week_summary(self, man_day_minutes): """Get the week summary: tasks and total time in minutes.""" tasks = {} query = (Task.select( Task.name, fn.SUM((fn.strftime('%s', Task.end_time) - fn.strftime('%s', Task.start_time)).cast('real') / 60).alias('sum')).join(Day).where( (Day.week == self._week) & Task.start_time.is_null(False) & Task.end_time.is_null(False)).group_by( Task.name).order_by(SQL('sum').desc())) for counter, row in enumerate(query): task = {} task[ResultColumn.Task] = row.name task[ResultColumn.Time] = row.sum if man_day_minutes: task[ResultColumn.Man_Day] = round(row.sum / man_day_minutes, 2) else: task[ResultColumn.Man_Day] = '' tasks[counter] = task self.logger.debug('Week summary: %s', tasks) return tasks
def create_task(self, task_name): """Create a task for a given task name.""" try: query = Task.insert(name=task_name, day=self._day) self.logger.debug('Executing query: %s', query.sql()) # pylint: disable=locally-disabled,E1120 return query.execute() > 0 except IntegrityError: return False
def total_time_worked(self): """Get the total worked time (minutes) for the entire period.""" minutes = (Task.select( fn.SUM((fn.strftime('%s', Task.end_time) - fn.strftime('%s', Task.start_time)).cast('real') / 60).alias('sum')).where( Task.start_time.is_null(False) & Task.end_time.is_null(False)).scalar()) self.logger.debug('Get total time worked: %s', minutes) return minutes or 0
def get_last_unique_task_names(): """Return the last unique task names since last three months.""" logger = logging.getLogger(__name__) last_3_months = date.today() + timedelta(days=-90) logger.debug('Last unique task names period: %s', last_3_months) tasks = (tuple(x.name for x in Task.select(Task.name).join(Day).distinct().where( Day.date > last_3_months).order_by(Task.name))) logger.debug('Last unique task names: %s', tasks) return tasks
def minutes_of_day(self): """Get the total time in minutes of today's tasks.""" minutes = (Task.select( fn.SUM( (fn.strftime('%s', Task.end_time) - fn.strftime('%s', Task.start_time)).cast('real') / 60).alias( 'sum')).where((Task.day == self._day) & Task.start_time.is_null(False) & Task.end_time.is_null(False)).scalar()) self.logger.debug('Minutes of day: %s', minutes) return minutes or 0
def minutes_of_week(self): """Get the total time in minutes of week's tasks.""" minutes = (Task.select( fn.SUM((fn.strftime('%s', Task.end_time) - fn.strftime('%s', Task.start_time)).cast('real') / 60).alias('sum')).join(Day).where( (Day.week == self._week) & Task.start_time.is_null(False) & Task.end_time.is_null(False)).scalar()) self.logger.debug('Get minutes of week: %s', minutes) return minutes or 0
def get_total_annual_worked_hours(_year): """Get the total time worked in hours of `_year`.""" logger = logging.getLogger(__name__) minutes = (Task.select( fn.SUM((fn.strftime('%s', Task.end_time) - fn.strftime('%s', Task.start_time)).cast('real') / 60).alias('sum')).join(Day).join(Week).where( (Week.year == int(_year)) & Task.start_time.is_null(False) & Task.end_time.is_null(False)).scalar()) logger.debug('Get total time of year: %s', minutes) return max(int(minutes / 60), 0) if minutes is not None else 0
def __cache_data(self): """Cache data.""" self._cached_data = {} # ensure that null start_time appears in last positions for counter, task in enumerate( Task.select(Task.id, Task.name, Task.start_time, Task.end_time).join(Day).where( Task.day == self._day).order_by( SQL("IFNULL(start_time, '24:00')"))): row = {} row[TaskColumn.Id] = task.id row[TaskColumn.Task] = task.name row[TaskColumn.Start_Time] = task.start_time row[TaskColumn.End_Time] = task.end_time self._cached_data[counter] = row self.logger.debug('Cached data: %s', self._cached_data)
def update_task(id_, field, value): """Update task field with a given value for a given id.""" logger = logging.getLogger(__name__) args = dict() if field == TaskColumn.Task: args['name'] = value elif field == TaskColumn.Start_Time: args['start_time'] = '{:02d}:{:02d}'.format( value.hour(), value.minute()) elif field == TaskColumn.End_Time: args['end_time'] = '{:02d}:{:02d}'.format(value.hour(), value.minute()) if args: try: query = Task.update(**args).where(Task.id == id_) logger.debug('Executing query: %s', query.sql()) return query.execute() > 0 except IntegrityError: return False return False
def delete_task(id_): """Delete a task with the given id.""" logger = logging.getLogger(__name__) query = Task.delete().where(Task.id == id_) logger.debug('Executing query: %s', query.sql()) return query.execute() > 0