comparison pylons_app/lib/celerylib/tasks.py @ 467:3fc3ce53659b celery

starting celery branch
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 Sep 2010 01:55:46 +0200
parents
children a3d9d24acbec
comparison
equal deleted inserted replaced
466:183cee110578 467:3fc3ce53659b
1 from celery.decorators import task
2 from datetime import datetime, timedelta
3 from pylons_app.lib.helpers import person
4 from pylons_app.lib.utils import OrderedDict
5 from time import mktime
6 import calendar
7 import logging
8 from vcs.backends.hg import MercurialRepository
9
10 log = logging.getLogger(__name__)
11
12 @task()
13 def whoosh_index(repo_location,full_index):
14 from pylons_app.lib.indexers import DaemonLock
15 from pylons_app.lib.indexers.daemon import WhooshIndexingDaemon,LockHeld
16 try:
17 l = DaemonLock()
18 WhooshIndexingDaemon(repo_location=repo_location)\
19 .run(full_index=full_index)
20 l.release()
21 return 'Done'
22 except LockHeld:
23 log.info('LockHeld')
24 return 'LockHeld'
25
26 @task()
27 def get_commits_stats(repo):
28 aggregate = OrderedDict()
29 repo = MercurialRepository('/home/marcink/hg_repos/'+repo)
30 #graph range
31 td = datetime.today() + timedelta(days=1)
32 y, m, d = td.year, td.month, td.day
33 ts_min = mktime((y, (td - timedelta(days=calendar.mdays[m])).month,
34 d, 0, 0, 0, 0, 0, 0,))
35 ts_max = mktime((y, m, d, 0, 0, 0, 0, 0, 0,))
36
37 def author_key_cleaner(k):
38 k = person(k)
39 k = k.replace('"', "'") #for js data compatibilty
40 return k
41
42 for cs in repo[:200]:#added limit 200 until fix #29 is made
43 k = '%s-%s-%s' % (cs.date.timetuple()[0], cs.date.timetuple()[1],
44 cs.date.timetuple()[2])
45 timetupple = [int(x) for x in k.split('-')]
46 timetupple.extend([0 for _ in xrange(6)])
47 k = mktime(timetupple)
48 if aggregate.has_key(author_key_cleaner(cs.author)):
49 if aggregate[author_key_cleaner(cs.author)].has_key(k):
50 aggregate[author_key_cleaner(cs.author)][k]["commits"] += 1
51 aggregate[author_key_cleaner(cs.author)][k]["added"] += len(cs.added)
52 aggregate[author_key_cleaner(cs.author)][k]["changed"] += len(cs.changed)
53 aggregate[author_key_cleaner(cs.author)][k]["removed"] += len(cs.removed)
54
55 else:
56 #aggregate[author_key_cleaner(cs.author)].update(dates_range)
57 if k >= ts_min and k <= ts_max:
58 aggregate[author_key_cleaner(cs.author)][k] = {}
59 aggregate[author_key_cleaner(cs.author)][k]["commits"] = 1
60 aggregate[author_key_cleaner(cs.author)][k]["added"] = len(cs.added)
61 aggregate[author_key_cleaner(cs.author)][k]["changed"] = len(cs.changed)
62 aggregate[author_key_cleaner(cs.author)][k]["removed"] = len(cs.removed)
63
64 else:
65 if k >= ts_min and k <= ts_max:
66 aggregate[author_key_cleaner(cs.author)] = OrderedDict()
67 #aggregate[author_key_cleaner(cs.author)].update(dates_range)
68 aggregate[author_key_cleaner(cs.author)][k] = {}
69 aggregate[author_key_cleaner(cs.author)][k]["commits"] = 1
70 aggregate[author_key_cleaner(cs.author)][k]["added"] = len(cs.added)
71 aggregate[author_key_cleaner(cs.author)][k]["changed"] = len(cs.changed)
72 aggregate[author_key_cleaner(cs.author)][k]["removed"] = len(cs.removed)
73
74 d = ''
75 tmpl0 = u""""%s":%s"""
76 tmpl1 = u"""{label:"%s",data:%s,schema:["commits"]},"""
77 for author in aggregate:
78
79 d += tmpl0 % (author,
80 tmpl1 \
81 % (author,
82 [{"time":x,
83 "commits":aggregate[author][x]['commits'],
84 "added":aggregate[author][x]['added'],
85 "changed":aggregate[author][x]['changed'],
86 "removed":aggregate[author][x]['removed'],
87 } for x in aggregate[author]]))
88 if d == '':
89 d = '"%s":{label:"%s",data:[[0,1],]}' \
90 % (author_key_cleaner(repo.contact),
91 author_key_cleaner(repo.contact))
92 return (ts_min, ts_max, d)