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
|
#!/usr/bin/python2.4
# Copyright 2005 Canonical Ltd. All rights reserved.
# pylint: disable-msg=C0103,W0403
"""Script to generate reports with data from ShipIt orders."""
import _pythonpath
from datetime import datetime, date
from zope.component import getUtility
import pytz
from canonical.config import config
from canonical.uuid import generate_uuid
from canonical.launchpad.scripts.base import LaunchpadCronScript
from canonical.launchpad.interfaces import (
ILibraryFileAliasSet, IShippingRequestSet, IShipItReportSet)
class ShipitReporter(LaunchpadCronScript):
def _createLibraryFileAlias(self, csv_file, basename):
"""Create and return a LibraryFileAlias containing the given csv file.
The filename is generated using the given basename, the current date
and a random string, in order for it to not be guessable.
"""
fileset = getUtility(ILibraryFileAliasSet)
csv_file.seek(0)
now = datetime.now(pytz.timezone('UTC'))
filename = ('%s-%s-%s.csv'
% (basename, now.strftime('%y-%m-%d'), generate_uuid()))
return fileset.create(
name=filename, size=len(csv_file.getvalue()), file=csv_file,
contentType='text/plain')
def main(self):
self.logger.info('Generating ShipIt reports')
requestset = getUtility(IShippingRequestSet)
reportset = getUtility(IShipItReportSet)
self.txn.begin()
csv_file = requestset.generateRequestDistributionReport()
reportset.new(self._createLibraryFileAlias(
csv_file, 'RequestDistribution'))
self.txn.commit()
self.txn.begin()
csv_file = requestset.generateCountryBasedReport()
reportset.new(self._createLibraryFileAlias(csv_file, 'OrdersByCountry'))
self.txn.commit()
self.txn.begin()
csv_file = requestset.generateShipmentSizeBasedReport()
reportset.new(self._createLibraryFileAlias(csv_file, 'OrdersBySize'))
self.txn.commit()
self.txn.begin()
# XXX: salgado 2005-11-24:
# For now this will be hardcoded as the date when a new ShipIt is
# opened.
start_date = date(2007, 4, 5)
csv_file = requestset.generateWeekBasedReport(start_date, date.today())
reportset.new(self._createLibraryFileAlias(csv_file, 'OrdersByWeek'))
self.txn.commit()
self.logger.info('Done.')
if __name__ == '__main__':
script = ShipitReporter('shipit-reporter', dbuser=config.shipit.dbuser)
script.lock_and_run(implicit_begin=False)
|