Changeset 23:bc0b04a989aa in stamper for stamper


Ignore:
Timestamp:
Jul 24, 2014, 2:52:30 AM (10 years ago)
Author:
Óscar M. Lage <info@…>
Branch:
default
Phase:
public
Message:

Graphs, first version (limited to 15 days, without customer the graph order is inverse)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/stamper.py

    r22 r23  
    22import json
    33import re
     4import pygal
    45from datetime import datetime, timedelta
    56from os.path import expanduser, exists
     
    110111        details = OrderedDict()
    111112        totals = OrderedDict()
     113        total_customer = OrderedDict()
    112114        for stamp in self.stamps:
    113115            if stamp['customer']:
     
    125127                        'action': stamp['action']
    126128                    })
     129                customer = stamp['customer']
    127130                totals[start_day] += worktime
     131                if start_day not in total_customer:
     132                    total_customer[start_day] = {}
     133                if customer not in total_customer[start_day]:
     134                    total_customer[start_day][customer] = 0
     135                total_customer[start_day][customer] += worktime
    128136        for day in totals:
    129137            totals[day] = str(timedelta(seconds=totals[day]))
    130         return details, totals
     138        return details, totals, total_customer
    131139
    132140    def details_by_customer(self, customer):
     
    153161
    154162    def show_stamps(self, customer=None, stamp_filter=None, verbose=False,
    155         sum=False):
     163        sum=False, graph=False):
    156164        if stamp_filter:
    157165            stamp_filter = self.validate_filter(stamp_filter)
     
    161169        if customer:
    162170            seconds=totals.get(customer, 0)
    163             total = timedelta(seconds)
     171            total = timedelta(seconds=totals.get(customer, 0))
    164172            print(' %(customer)s: %(total)s' % {'customer': customer,
    165173                                                'total': total})
     
    167175            for c in totals:
    168176                seconds=totals[c]
    169                 total = timedelta(seconds)
     177                total = timedelta(seconds=totals[c])
    170178                print(' %(customer)s: %(total)s' % {'customer': c,
    171179                                                    'total': total})
     
    175183                details, totals = self.details_by_customer(customer)
    176184            else:
    177                 details, totals = self.details()
     185                details, totals, total_customer = self.details()
    178186            for day in details:
    179187                print('------ %(day)s ------' % {'day': day})
     
    201209                    totalDays, remainingHr, remainingMin, HOURS_DAY
    202210                ))
     211
     212        if graph:
     213            DAYS = 15
     214            list_days = []
     215            list_tot = []
     216            stackedbar_chart = pygal.StackedBar()
     217            stackedbar_chart.title = 'Worked time per day (in hours)'
     218
     219            if customer:
     220                for day, tot in totals.iteritems():
     221                    list_days.append(day)
     222                    (h, m, s) = tot.split(':')
     223                    tot_sec = int(h) * 3600 + int(m) * 60 + int(s)
     224                    tot_h = float(tot_sec / float(60) / float(60))
     225                    list_tot.append(tot_h)
     226                stackedbar_chart.add(customer, list_tot)
     227                stackedbar_chart.x_labels = map(str, list_days)
     228                stackedbar_chart.render_to_file('graphs/chart-%s.svg' % customer )
     229            else:
     230                all_customers = self.customers()
     231                total_per_customer = {}
     232                details, totals, total_customer = self.details()
     233                chars = 0
     234                total_customer_reverse = total_customer.items()
     235                total_customer_reverse.reverse()
     236                for day, tot in total_customer_reverse:
     237                    if chars < DAYS:
     238                        list_days.append(day)
     239                        for cust in self.customers():
     240                            if cust not in tot:
     241                                tot[cust] = 0
     242                        for cus, time in tot.iteritems():
     243                                tot_h = float(time / float(60) / float(60))
     244                                if cus not in total_per_customer:
     245                                    total_per_customer[cus] = []
     246                                total_per_customer[cus].append(tot_h)
     247                    chars = chars + 1
     248                for ccus, ctime in total_per_customer.iteritems():
     249                    stackedbar_chart.add(ccus, ctime)
     250                stackedbar_chart.x_labels = map(str, list_days[-DAYS:])
     251                stackedbar_chart.render_to_file('graphs/chart-all.svg')
Note: See TracChangeset for help on using the changeset viewer.