def _cmd_list(self): date_from, date_to, items = entry.fetch(self.date_from, self.date_to, self.limit) sum_of_duration = 0 dates = set() groups = OrderedDict() date_from = date_from.date() date_to = date_to.date() if date_from != date_to: self._display('\n%s - %s\n' % (date_from, date_to)) else: self._display('\n%s\n' % date_from) for item in items: date = item['start_at'].date() dates.add(date) duration = int(item['duration'] or 0) sum_of_duration += duration date_duration = groups.get(date, 0) + duration groups[date] = date_duration if not self.group_by_date: self._display(entry.to_formatted_line(item)) if self.group_by_date and len(groups) > 0: for date, duration in groups.iteritems(): if duration > 0: self._display('%s: %s' % (date, duration_to_text(duration))) if sum_of_duration > 0: self._display('\nTotal: %s\tAverage: %s per day' % (duration_to_text(sum_of_duration), duration_to_text(sum_of_duration / len(dates))))
def to_formatted_line(row): _id = row['id'] start_at = row['start_at'].strftime('%Y-%m-%d %H:%M - ') if row['end_at'] != NOT_FINISHED: end_at = row['end_at'].strftime('%H:%M') else: end_at = '<NOW>' event = row['event'] if row['duration']: duration = int(row['duration']) duration_text = timing.duration_to_text(duration) else: duration_text = '' line = '%5d %s%-6s\t%6s\t%s' % (_id, start_at, end_at, duration_text, event) return line