-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorgserver.py
executable file
·1646 lines (1439 loc) · 67.3 KB
/
orgserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
org-server.py -- Provide HTTP access to project data in the supplied Git repository
org-server.py <repo> <project> ...
api/projects
Returns JSON describing all projects available.
api/data/<project>/<time-style>
Returns <project> history from the org data in git, with the given time style:
effort Data is aggregated for periods of effort time elapsed
elapsed Data is aggregated for periods of calendar time elapsed
sprint Data is aggregated for each sprint elapsed
"""
from __future__ import with_statement
import argparse
import cgi
import copy
import datetime
try:
import json
except:
import simplejson as json
import logging
import math
import re
import socket
import string
import os
import sys
import textwrap
import time
import git # modules from site-packages
from mathdict import * # modules local to project
global day_seconds
day_seconds = 8*60*60 # Default day is 8 hours
# For compatibility with 2.5
# Lifted from:
# http://stackoverflow.com/questions/1716428/def-next-for-python-pre-2-6-instead-of-object-next-method/1716464#1716464
# https://github.com/mikeboers/PyHAML/commit/45db152f9c8754ef3f8f249f3e68114c83f8580d
try:
next
except NameError:
class Throw(object): pass
throw = Throw() # easy sentinel hack
def next(iterator, default=throw):
"""next(iterator[, default])
Return the next item from the iterator. If default is given
and the iterator is exhausted, it is returned instead of
raising StopIteration.
"""
try:
iternext = iterator.next.__call__
# this way an AttributeError while executing next() isn't hidden
# (2.6 does this too)
except AttributeError:
raise TypeError("%s object is not an iterator" % type(iterator).__name__)
try:
return iternext()
except StopIteration:
if default is throw:
raise
return default
def project_data( repository, projects ):
"""Given a repo name, return a dict containing a list of
historical blobs for each project. If the "master"" commit hasn't
changed, we can safely return the previously cached data.
We work back from master, and we must find a non-ambiguous path.
In each commit, we must find a blob named <project>.org.
(Initial commit)
v
o-----o-----o-----o---------------------------o-----o OK.
\ / ^
o-----o-----o-----o-----o |
master
(Initial commit) (parent)
v <---
o-----o-----o-----o-------------o-------------o-----o BAD; Ambiguous!
\ / ^
o-----o-----o-----o-----o |
master
Collect each "name"-ed project's 'org' file git.Blobs for all of
its historical commits into:
project["name"] = [oldest, ..., newest]
"""
# Obtain read-only access to the Git repo 'master' branch
repo = git.Repo( repository )
assert repo.bare == False
repo.config_reader()
master = repo.heads.master
commit = master.commit
# See if "master" commit has changed; if not, return cached result data
if project_data.hexsha != commit.hexsha:
# A new "master" commit; recompute result data
project_data.hexsha = commit.hexsha
project_data.result = {}
# See what project entries remain after removing those in cache
remains = set( projects ) - set( project_data.result.keys() )
if not remains:
return project_data.hexsha, project_data.result
# Some project data remains to be gleaned.
while commit:
#'''
print "Commit %8.8s by %-20.20s on %s" % (
commit.hexsha, commit.author, time.strftime( "%Y-%m-%d %H:%M:%S",
time.localtime( commit.committed_date )))
print " %s" % ( commit.message )
for b in commit.tree.blobs:
print " %8.8s: %-20s: %-50.50s" % (
b.hexsha, b.name, repr( b.data_stream.read( 50 )))
#'''
for p in remains:
try:
b = commit.tree/(p + ".org")
except Exception, e:
print "No %s.org found in commit %8.8s" %( p, commit.hexsha )
continue
bl = project_data.result.setdefault( p, [] )
if not bl or bl[-1].hexsha != b.hexsha:
bl.insert( 0, b )
# else:d print "Dropping duplicate blob:" + b.hexsha
commit = commit.parents[0] if commit.parents else None
'''
for p, bl in project_data.result.items():
print "Project %s:" % ( p )
for b in bl:
print " %8.8s: %-20s: %-50.50s" % (
b.hexsha, b.name, repr( b.data_stream.read( 50 )))
'''
return project_data.hexsha, project_data.result
# Initial cache for project_data function
project_data.hexsha = None
project_data.result = None
class task( object ):
"""Represents a single task in a tree of tasks/subtasks. Each
task is of the form:
.description = "Project burndown <2012-03-02 Fri>"
.state = "TODO"
.data = {
"TODO": <timedict>{
"Effort": "22:00",
"CLOCKSUM": "24:00"
},
"DONE": <timedict>{...
}
.subtask = [<task>, ...]
From this we can collect a breakdown of effort estimates and
actual clocked time, between all the different tasks states.
"""
def __init__( self, state, description, times=None ):
self.state = state
self.description = description
self.data = timedict(int)
if times:
for t in times:
self.data += t
self.subtask = []
def format( self, level=1, cols=None ):
if cols is None:
cols = sorted( self.data.keys(), reverse=True )
timespecs = collections.defaultdict(str, reversed( self.data ))
return "| %s %-4s %-*.*s | %s |" % (
"*" * level, self.state, 56-7-level, 56-7-level, self.description,
" | ".join( "%8s" % timespecs[k] for k in cols ))
def __str__( self ):
return self._format()
def walk( self, level=1 ):
yield ( self, level )
for child in self.subtask:
for record in child.walk( level+1 ):
yield record
def display( self, legend=True, cols=None ):
if cols is None:
cols = sorted( self.data.keys(), reverse=True )
result = []
if legend:
result.append( "| %-56s" % "Task"
+ "".join( "| %-8s " % col
for col in cols )
+ "|" )
result.append( "|-" + "-" * 56
+ ( "|" + "-" * 10 ) * len( cols )
+ "|" )
for tsk, lvl in self.walk():
result.append( tsk.format( lvl, cols=cols ))
return "\n".join( result )
def add( self, child ):
self.subtask.append( child )
def totals( self ):
"""
Returns a tuple containing the following 3 data:
o grand totals of each column, broken down by task state:
{"TODO": <timedict>{
"CLOCKSUM": 10800, "Effort": 21600},
"DONE": <timedict>{
"CLOCKSUM": 54800, "Effort": 50400}, ...}
o this task's individual contribution:
<timedict>{"CLOCKSUM": 10800, "Effort": 0}
o all subtasks contributions:
<timedict>{"CLOCKSUM": 75600, "Effort": 79200}
"""
res = {}
# Add all the subtask's times, into per-state buckets
for s in self.subtask:
for state, times in s.totals()[0].iteritems():
if state not in res:
res[state] = timedict(int)
res[state] += times
# We now have the totals for all children. If our own
# data differs from the sum of all our subtasks, they
# must be greater -- this means that this roll-up task has
# also accrued additional individual effort and/or
# clocksum. Add that, too.
sub = timedict(int)
for state, times in res.items():
sub += times
our = self.data - sub
if self.state not in res:
res[self.state] = timedict(int)
res[self.state] += our
return res, our, sub
def parse_tasks( lines ):
"""A generator that returns a sequence of (task, level).
At level 0, scan lines of input from the 'lines iterator
'til we find the start of a table. Then, deduces the column
names, and begins to scan tasks at level 1.
After parsing the next task, the level (number of leading *
characters) is examined. If it is greater than our level, the
task is added as a sub-task. If less or equal, the (task, leval)
is yielded and the parse ends.
"""
# Scan and discard lines 'til we find the beginning of an org-table
found = False
for line in lines:
if line.startswith("#+BEGIN:"):
found = True
break
if not found:
raise Exception("No org-mode table found")
# Parse column names, discard separator |---|
line = next( lines )
cols = list( c for c in map( string.strip, line.split( "|" )) if c )
next( lines )
# Parse records, yielding tasks, 'til end of org-table
refirst = re.compile( r"\s* \| \s* ( \*+ ) \s* ( \w+ )", re.VERBOSE )
revalue = re.compile( r"\s* ( [^|]* ) \|", re.VERBOSE )
for line in lines:
# | ** TODO
# ^^ ^^^^
pos = 0
match = refirst.match( line, pos=pos )
if match is None:
assert line.startswith( "#+END" ), "org-mode table must end with #+END, not: '%s'" % line
break
stars, state = match.groups()
state = state[0:4] # Limit states to 4 chars max
pos = match.end()
# ... Project burndown <2012-03-02 Fri> |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ... 22:00 |
# ^^^^^^
vals = []
while pos < len( line ):
match = revalue.match( line, pos=pos )
if match is None:
break
vals.append( match.group(1).strip() )
pos = match.end()
assert len( cols ) == len( vals )
assert cols[0] == "Task"
descr = vals[0]
yield task( state, descr, zip( cols[1:], vals[1:] )), len( stars )
def parse_task_heirarchy( lines ):
"""Produce a task heirarchy from a sequence of (task, level), correctly
making sub-tasks a child of the correct parent task. Return the root task.
"""
stack = []
for tsk, lvl in parse_tasks( lines ):
assert lvl > 0
if lvl > len( stack ):
# | * A Stack Before: []
# | ** B Stack After: [A]
assert lvl == len( stack ) + 1
stack.append( tsk )
elif lvl == len( stack ):
assert lvl > 1
# | * A Stack Before: [A, B]
# | ** B - Make B a child of A, replace B with new C
# > | ** C Stack After: [A, C]
stack[-2].add( stack[-1] )
stack[-1] = tsk
else:
while lvl < len( stack ):
# | ***
# > | **
stack[-2].add( stack[-1] )
stack.pop()
while len( stack ) > 1:
stack[-2].add( stack[-1] )
stack.pop()
assert len( stack ) == 1
return stack[0]
def project_data_parse( data, project ):
"""Return the parsed org-mode project statistics data for one
project, from the supplied data.
Searches each blob for an org-mode table like:
#+BEGIN: columnview :hlines 1 :id local
| Task | Effort | CLOCKSUM |
|--------------------------------------------------------+--------+----------|
| * TODO Project burndown <2012-03-02 Fri> | 22:00 | 24:00 |
| ** DONE Display bar chart for different periods | 2:00 | 4:00 |
| ** NEXT Split Bar display | 1:00 | 6:00 |
| ** NEXT Horizontal/Vertical Grid Lines | 1:00 | |
| ** TODO Define JSON format | 2:00 | |
| ** TODO Create org-mode/git web service | 16:00 | 11:00 |
| *** DONE Load Git repo | 4:00 | 4:00 |
| *** TODO Parse org-mode data | 4:00 | |
| *** DONE Create Python web server | 8:00 | 7:00 |
#+END:
From this we harvest the aggregate data, and track the state-changes of
tasks. We'll create a tree of task objects from the data parsed from each
blob, containing roll-up statistics of all of the sub-tasks in each state.
Cache the raw task statistical data, to avoid having to reparse it. The
same blog may appear in many consecutive commits...
"""
# cache[blob.hexsha] == {"todo": {}, ...} or None.
cache = project_data_parse.cache
results = {}
results["project"] = project
results["list"] = []
# Traverse the (from oldest to newest) list, collecting the
# differences between each. Ignore duplicates, cache any blobs
# parsed.
rec, old = None, None
stats, prior, ahead = None, None, None
for blob in data[project]:
# Now: rec, stats contains last cycle's computed data
if blob.hexsha in cache:
ahead = cache[blob.hexsha] # May be None (no data found)
else:
try:
print "Parsing blob %s: %s" % ( blob.hexsha, blob.name )
ahead = {}
ahead["task"] = parse_task_heirarchy(
iter( blob.data_stream.read().splitlines() ))
print ahead["task"].display()
# ... <2012-03-02 Fri> ...
# ^^^^^^^^^^
match = re.search( r"<([0-9-]*)[^>]*>",
ahead["task"].description )
if match is None:
raise Exception( "No date found in task: %s" % (
ahead["task"].description ))
ahead["date"] = match.group( 1 )
ahead["date#"] = time.mktime( time.strptime( ahead["date"],
"%Y-%m-%d" ))
# ...Sprint 3...
# ^
match = re.search( r"[Ss]print\s+([0-9]+)",
ahead["task"].description )
sprint = 0
if match is not None:
sprint = int( match.group( 1 ))
ahead["sprint"] = sprint
except Exception, e:
print "No Task Data: %s" % ( e )
ahead = None
cache[blob.hexsha] = ahead
# Now: rec, stats still contains last cycle's computed data;
# ahead contains this blob's task's data.
# This blob may need to be ignored for various reasons.
if ahead is None:
print "Commit contains blob with no tasks data; skipping"
continue
if rec and blob.hexsha == rec["blob"]:
print "Commit contains same blob as last; skipping"
continue
if stats and ahead["date"] == stats["date"]:
# This record contains the same date as the last; must be a later
# commit on the same day. 0 Use its data instead; throw away the
# last record computed, and ensure we retain the same old, prior for
# this round...
print "Commit contains same date; re-doing %r" % ahead["date"]
results["list"] = results["list"][:-1]
rec, stats = old, prior
# Remember this round's task's rec/stats in old/prior, to compute the
# next round's differences. Now safe to advance ahead to the stats just
# loaded.
old, prior = rec, stats
stats = ahead
# We have a valid task! Create the summary rec for the JSON
# result data list.
rec = {}
rec["blob"] = blob.hexsha
rec["date"] = stats["date"]
rec["date#"] = stats["date#"]
rec["sprint"] = stats["sprint"]
dicts = [ # (first contains *all* columns!)
"total", "project", # Overall sums
"todo", "todoTotal",
"done", "doneTotal", # Done since last, and sum total
"removed", "removedTotal", # Existing tasks cancelled
"added", "addedTotal", # New tasks added/uncancelled
"delta", "deltaTotal", # net change and total change
]
if dicts[0] not in stats:
# We haven't yet computed the cached stats for this blob.
#
# total -- all tasks.
# todo -- all incomplete tasks. Tasks left to do.
# done -- all complete tasks. Tasks finished.
# project -- all non-cancelled tasks. Delivered
# added -- New Tasks added to project this period. (delta total)
# removed -- Tasks removed from project this period. (delta canc)
# growth -- Net Project added - removed this period.
#
stats["todoTotal"] = timedict(int)
stats["doneTotal"] = timedict(int)
stats["removedTotal"] = timedict(int)
tot, our, sub = stats["task"].totals()
for k,v in tot.iteritems():
if k in ("DONE"):
stats["doneTotal"] += v
elif k in ("CANC"):
# Items removed from project. Both Effort estimate
# (and clocked time) no longer appear in the 'total'
# project data, so are effectively subtracted from any
# others "added".
stats["removedTotal"] += v
else: # ("TODO", "NEXT", "HOLD", "WAIT", "PHON", ...)
stats["todoTotal"] += v
stats["project"] = stats["todoTotal"] + stats["doneTotal"]
stats["total"] = stats["project"] + stats["removedTotal"]
if prior:
stats["todo"] = stats["todoTotal"] - prior["todoTotal"]
stats["done"] = stats["doneTotal"] - prior["doneTotal"]
# Added is the sum of: a) the the absolute increase in
# the total project size (including all tasks, even
# cancelled),
stats["added"] = stats["total"] - prior["total"]
# PLUS b) any existing tasks changed from cancelled to
# something else; if "removed" goes -'ve, this really
# means "added"; never let "removed" go -'ve.
stats["removed"]= stats["removedTotal"] - prior["removedTotal"]
for k, v in list( stats["removed"].items() ):
if v < 0:
stats["added"] += (k, -v)
stats["removed"] += (k, -v)
stats["addedTotal"] = stats["added"] + prior["addedTotal"]
stats["delta"] = stats["added"] - stats["removed"]
stats["deltaTotal"] = stats["delta"] + prior["deltaTotal"]
# If there were no results for a stat (eg. no tasks in the
# given state), ensure that the resultant timedict at
# least have zero entries for all known columns. Assumes
# total (first item in 'dicts'') will have all columns...
for d in dicts:
for k in stats[dicts[0]].keys():
if d not in stats:
stats[d] = timedict(int)
if k not in stats[d]:
stats[d] += (k, 0)
for d in dicts:
print task( state="",
description=d,
times=stats[d].items() ).format( level=0 )
# Turn all the stats <timedict> back into textual time specs,
# using their custom __reversed__ method.
texts = {}
for d in dicts:
texts[d] = dict( reversed( stats[d] ))
# The "estimated" (the unfortunately named Effort) column
# deals in the total number of story points (estimated in
# hours, roughly) for all tasks. Map some known columns to
# more correct names.
mapping = {
"Effort": "estimated",
"CLOCKSUM": "work",
}
for i in stats[dicts[0]].keys():
n = mapping.get( i, i )
rec[n] = {}
for d in dicts:
rec[n][d] = texts[d][i]
rec[n][d+"#"] = stats[d][i]
results["list"].append( rec )
print "Adding record %3d for %r" % ( len( results["list"] ), rec["date"] )
return results
project_data_parse.cache = {}
def best_fit( points ):
"""
Computes a best-fit line for the given [(x,y), ...] data. Returns
a line equation in point, slope form x, y, slope. Make two passes over
the data, to avoid subtraction of nearly equal numbers. See
http://www.johndcook.com/blog/2008/10/20/comparing-two-ways-to-fit-a-line-to-data/
"""
sx = 0.0
sy = 0.0
stt = 0.0
sts = 0.0
n = len( points )
for x, y in points:
sx += x
sy += y
for x, y in points:
t = x - sx/n
stt += t*t
sts += t*y
slope = sts/stt if stt else float( "inf" )
intercept = (sy - sx*slope)/n if n else 0.0
return 0.0, intercept, slope
def project_stats_transform( results, style, bestfit=True ):
"""Transform and return the project stats into the specified x-axis style.
The incoming data is not interfered with (may be from a cache), and is
assumed to be summary project data, in standard elapsed (calendar) time,
possibly with gaps. Adds a "label" field to each entry. For the given
style, we create equally spaced records for units of:
elapsed -- linear elapsed (calendar) time
effort -- units of clocked work on all tasks (even canceled ones)
sprint -- for each "Sprint #", as defined in the project name's
These deal in processed records of the form:
results["list"] = [{
"work": {
"added": 0,
"delta": 32,
...
},
"estimated": {
"added": 0,
...
}
}
Lines are projected for "progress" -- amount left To Do minus the increase
in the project scope (todoTotal - deltaTotal), "change" -- the increase in
the project scope (deltaTotal). Where these lines intersect is the
projected completion of the project.
The date[#] data is also projected, to synthesize date for any entries added
at the end of the project.
"""
results = copy.copy( results ) # Shallow copy
results["style"] = style
def date_components( date ):
"""YYYY-MM-DD" --> (y, m, d)"""
y, m, d = ( int(x) for x in date.split( '-' ))
return y, m, d
filled = []
rec, old = None, None
if style == "elapsed":
# Fill in missing days in elapsed time entries in 'filled'.
for rec in results["list"]:
# Take the date of 'old' (the last record in the filled list), and
# keep copying it and advancing its date 'til we reach the date in
# the current rec, filling in with copies of the last record's data
# (keeping xxxxTotal data, but zeroing out the xxxxx change data)
rec = copy.deepcopy( rec )
recymd = date_components( rec["date"] )
print "Process %s" %( repr( recymd ))
while filled: # (Skip for very first record, otherwise loop forever)
old = filled[-1]
oldymd = date_components( old["date"] )
if oldymd >= recymd:
print "old: %s" % json.dumps( old, indent=4 )
print "rec: %s" % json.dumps( rec, indent=4 )
raise Exception( "Out of order records! %r (%s) >= %r (%s)" %(
oldymd, old["date"], recymd, rec["date"] ))
nxtdtm = datetime.datetime( *oldymd ) + datetime.timedelta( 1 )
nxtymd = ( nxtdtm.year, nxtdtm.month, nxtdtm.day )
if nxtymd >= recymd:
break
nxt = copy.deepcopy( old )
nxt["date#"] = time.mktime( nxtdtm.timetuple() )
nxt["date"] = nxtdtm.strftime( "%Y-%m-%d" )
nxt["blob"] = None
for d in "estimated", "work":
for k, v in list( nxt[d].items() ):
if k in ( "added", "removed", "delta", "todo", "done" ):
nxt[d][k] = "0:00"
nxt[d][k+"#"] = 0
print "Filling %s" % ( repr( nxtymd ))
nxt["label"] = "Day %d" % ( len( filled ) + 1 )
print " Emitting %s" % ( nxt["label"] )
#print " Emitting %s: %s" % (
# nxt["label"], json.dumps( nxt, sort_keys=True, indent=4 ))
filled.append( nxt )
print "Copying %s" % ( repr( recymd ))
rec["label"] = "Day %d" % ( len( filled ) + 1 )
print " Emitting %s" % ( rec["label"] )
#print " Emitting %s: %s" % (
# rec["label"], json.dumps( rec, sort_keys=True, indent=4 ))
filled.append( rec )
elif style == "effort":
# Output the first record, and then scan the records, outputting a copy
# of the latest record for each # hours of CLOCKSUM work reported. Drop
# the last modulo-# remainder. By default, # == 8 hours. When skipping
# records due to insuffient total CLOCKSUM, sum up the 'xxxx' data from
# intervening records, and use the last record's 'xxxxTotal' data. Then
# emitting the same record multiple times, zero out the 'xxxx' data for
# the additional copies, and use the original 'xxxxTotal' data.
increment = day_seconds
step = 0
add = {} # data to save from discarded records
changekeys = [ "added", "delta", "done", "removed", "todo" ]
masterkeys = [ "work", "estimated" ]
add = None
for rec in results["list"]:
# The 'rec["total"]["CLOCKSUM"]' value defines how much work has
# been clocked, so far. This includes all tasks, whether still in
# 'project' or having been removed. We uses this value (instead of
# 'project', which doesn't include removed tasks), because we want
# to measure progress vs. the actual amount of effort expended --
# including tasks we later decide to remove from the project's
# scope.
recymd = date_components( rec["date"] )
print "Process %s: %5d hours effort" %( repr( recymd ),
rec["work"]["total#"]/60/60 )
if add is None:
add = dict( ( d, timedict(int) ) for d in masterkeys )
nxt = copy.deepcopy( rec )
if step <= nxt["work"]["total#"]:
# We met/exceeded this step with this record. Include any data
# from previously dropped records, and update the corresponding
# "xxxx" (textual "00:00" version) for each "xxxx#". Keep
# emiting copies 'til our step advances beyond this record's
# total amount of work effort. We'll use the 'add' timedict's'
# += ( field, value ) operator to add sum to nxt. Also, since
# we'll be changing and re-emitting nxt, make sure we do a deep
# copy each time, so changes don't affect the reference already
# appended to filled...
while step <= nxt["work"]["total#"]:
nxt["label"]= "Day %d" % ( len( filled ) + 1 )
for d in masterkeys:
if not add or not add[d]:
continue
for fn in add[d].keys():
assert fn.endswith("#")
print " Adding: %-10s %-10s of %s" % (
d, fn, repr( dict( reversed( add[d] ))))
add[d] += ( fn, nxt[d][fn] )
for fn, v in reversed( add[d] ):
f = fn[:-1]
print " Update: %-10s %-10s of %7d (%5s) to %7d (%5s)" % (
d, f, nxt[d][fn], nxt[d][f], add[d][fn], v )
nxt[d][fn] = add[d][fn]
nxt[d][f] = v
add = None
step += increment
# OK, time to add record to filled; 'nxt' record (contains
# any change data collected in 'add' from from discarded
# records.) Zero out the xxxx change data, but retain the
# xxxxTotal data, in case we need to emit copies of this
# record.
print " Emitting %s" % ( nxt["label"] )
#print " Emitting %s: %s" % (
# nxt["label"], json.dumps( nxt, sort_keys=True, indent=4 ))
filled.append( nxt )
nxt = copy.deepcopy( nxt )
for d in masterkeys:
for f in changekeys:
fn = f+"#"
nxt[d][f] = "0:00"
nxt[d][fn] = 0
else:
# This record doesn't include enough logged work to meet the
# next step. Discard it, but retain any non-zero differentials
# to 'add' to the next record output. Then, advance to next
# record. We'll simply discard this data if we never reach the
# next 'interval' hours.
for d in masterkeys:
for f in changekeys:
fn = f+"#"
if rec[d][fn] != 0:
add[d] += ( fn, rec[d][fn] )
print " Saving: %-10s %-10s of %7ds; now %s" % (
d, f, rec[d][fn], repr( dict( reversed( add[d] ))))
else:
raise Exception( "Unkown style: %s" % ( style ))
results["list"] = filled
# Compute the burndown lines for each record. These estimate the rate of
# change in Effort (the estimatd time), vs. the selected X axis (which are
# assumed to be at consistently equidistant from each-other by some
# interpretation; "elapsed", "effort", etc.).
#
# The first record doesn't have one (as there is no slope), nor does any
# empty record (one with no "list" entry). If too much change is detected
# (eg. too many tasks added/removed or estimates changed), then the project
# is considered to have suffered a discontinuity, and fresh estimates will
# be generated from that point forward.
#
# If 'bestfit' is not selected, we'll just use a linear average between the
# first and current record, over (todoTotal - addedTotal) for our "progress"
# line, and over (deltaTotal) for our "change" line. For 'bestfit', we'll
# compute the best fit line for the same data. Computes the project
# finish-x 'fx' (None if not computable)
change_max = 10 # Percent change indicating discontinuity
fxmax = None
fxmultiple = 3 # Allow expanding the results by this factor
rec, zro = None, None
prgrdata = []
chngdata = []
datedata = []
num = len( results["list"] )
for i in xrange( num ):
rec = results["list"][i]
rec["lines"] = None
print "Record %d, %-10s" % ( i, rec["label"] )
# If the delta (change) in the project is greater than a certain
# percentage of the project size, then we'll assume a "discontinous"
# change to the project, and compute fresh slopes.
estdlta = rec["estimated"]["delta#"]
estproj = rec["estimated"]["project#"]
print " Change: %4d%%: project is %7d, change is %7d" % (
estdlta * 100 / estproj, estproj, estdlta )
if ( abs( estdlta ) > estproj * change_max / 100 ):
print " Discontinuity; %3d%% change" % ( abs( estdlta ) * 100 / estproj )
prgrdata = []
chngdata = []
datedata = []
esttodoT = rec["estimated"]["todoTotal#"]
estdltaT = rec["estimated"]["deltaTotal#"]
print " Progress: %7d (%7d todo - %7d Change)" % (
esttodoT - estdltaT, esttodoT, estdltaT )
prgrdata.append( (i, esttodoT - estdltaT) )
chngdata.append( (i, -estdltaT) )
datedata.append( (i, rec["date#"]) )
if len( chngdata ) <= 1:
# 0 or 1 data point; no line can be computed
continue
# We have at least 2 points! Compute progress/change/date slopes.
lines = rec["lines"] = {}
if bestfit:
px0, py0, pslope = best_fit( prgrdata )
px0, py0 = int( px0 ), int( py0 )
pC = py0 - pslope * px0
px1, py1 = i, int( pslope * i + pC )
cx0, cy0, cslope = best_fit( chngdata )
cx0, cy0 = int( cx0 ), int( cy0 )
cC = cy0 - cslope * cx0
cx1, cy1 = i, int( cslope * i + cC )
dx0, dy0, dslope = best_fit( datedata )
dx0, dy0 = int( dx0 ), int( dy0 )
dC = dy0 - dslope * dx0
dx1, dy1 = i, int( dslope * i + dC )
else:
px0, py0 = prgrdata[0]
px1, py1 = prgrdata[i]
pslope = float(py0 - py1) / (px0 - px1)
pC = py0 - pslope * px0
cx0, cy0 = chngdata[0]
cx1, cy1 = chngdata[i]
cslope = float(cy0 - cy1) / (cx0 - cx1)
cC = cy0 - cslope * cx0
dx0, dy0 = datedata[0]
dx1, dy1 = datedata[i]
dslope = float(dy0 - dy1) / (dx0 - dx1)
dC = dy0 - dslope * dx0
lines["progress"] = {"x1": px0, "y1": py0, "x2": px1, "y2": py1}
lines["change"] = {"x1": cx0, "y1": cy0, "x2": cx1, "y2": cy1}
lines["date"] = {"x1": dx0, "y1": dy0, "x2": dx1, "y2": dy1}
#
# A line equation is:
#
# y = mx + C
#
# To compute the intersection point of the two lines, and from that the
# x coordinate (ordinal) where the project will be done, we need the
# compute the C (constant) for each line equation. We have a point and
# a slope:
#
# pslope = (py0 - py1) / (px0 - px1)
#
# and from above:
#
# C = y - mx
#
# Now, given two line equations:
# y = mx + b
# y = nx + c
#
# We set them equal and solve for x:
# mx + b = nx + c
# mx - nx = c - b
# (m-n)x = c - b
# x = (c - b) / (m-n)
#
# So we now have a formula for x. y can then be calculated using the
# numerical value of x and one of the original formulas for y.
#
# y = mx + C
# y = m((c - b) / (m-n)) + C
#
if cslope - pslope > 0:
fx = ( pC - cC ) / ( cslope - pslope )
print "Slopes will intercept in future at x == %f" % ( fx )
# Compute the number of columns required to contain the point
# where the progress and change lines meet. However, clamp at a
# fxmultiple of the length of the current data... Then, compute
# where the progress/change lines intercept that last bar. This
# will cause the lines to cross exactly at the finish point, even
# if it is between two bars.
fxint = int( math.ceil( fx ))
fxnext = min( fxint, fxmultiple * num)
fxmax = max( fxnext, fxmax or 0 )
# We can now project the finish date as of this sample
ftimestamp = dslope * fx + dC
fdate = None
try:
d = datetime.date.fromtimestamp( ftimestamp )
fdate = "%4d-%02d-%02d (Day %d)" % (
d.year, d.month, d.day, fxint+1 )
print " Finish date at projected intercept: %s" % ( fdate )
except:
print " Finish date incomputable"
pass
rec["finish"] = fdate
lines["progress"] = {"x1":px0, "y1":py0,
"x2":fxnext, "y2":int( pslope * fxnext + pC )}
lines["change"] = {"x1":cx0, "y1":cy0,
"x2":fxnext, "y2":int( cslope * fxnext + cC )}
lines["date"] = {"x1":dx0, "y1":dy0,
"x2":fxnext, "y2":int( dslope * fxnext + dC )}
else:
print "Slopes will intercept in past"
print "Record %d: progress: %-32s, %f slope (%s)" % (
i, repr( lines["progress"] ), pslope, "best-fit" if bestfit else "linear")
print "Record %d: change: %-32s, %f slope" % (
i, repr( lines["change"] ), cslope )
print "Record %d: date: %-32s, %f slope" % (
i, repr( lines["date"] ), dslope )
# We've computed a finish-x. Fill in the results["list"] with empty
# records. Since we've computed an intersection, compute the approximate
# projected date for each (may be impossible; if so, None)
if fxmax is not None:
print "Need %d total records; adding %d" % (
fxmax + 1, fxmax - len( results["list"] ) + 1 )
while fxmax and fxmax >= len( results["list"] ):
rec = copy.deepcopy( results["list"][-1] )
rec["date#"] = dslope * len( results["list"] ) + dC
try:
d = datetime.date.fromtimestamp( rec["date#"] )
rec["date"] = "%4d-%02d-%02d" % ( d.year, d.month, d.day )
except:
rec["date"] = None
rec["label"] = "Day %d" % ( len( results["list"] ) + 1 )
rec["blob"] = None
rec["lines"] = None
rec["estimated"] = None
rec["work"] = None
print "Extend %s" % ( rec["date"] )
results["list"].append( rec )
return results
def deduce_encoding( available, environ, accept=None ):
"""Deduce acceptable encoding from HTTP Accept: header:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
If it remains None (or the supplied one is unrecognized), the
caller should fail to produce the desired content, and return an
HTML status code 406 Not Acceptable.
If no Accept: encoding is supplied in the environ, the default
(first) encoding in order is used.
We don't test a supplied 'accept' encoding against the HTTP_ACCEPT
settings, because certain URLs have a fixed encoding. For
example, /some/url/blah.json always wants to return
"application/json", regardless of whether the browser's Accept:
header indicates it is acceptable. We *do* however test the
supplied 'accept' encoding against the 'available' encodings,
because these are the only ones known to the caller.