Commit 300e5f4f9859e130c47befda68a0b20aa2b115de

  • avatar
  • arvind
  • Thu Jan 09 18:36:08 IST 2014
Code refactor
     - report.py now accepts command line arguments, start and end date, start
     and end time
     - report.py now uses ``Query`` objects to generate report.
logger.py
(2 / 5)
  
33import config
44import datetime
55
6
67engine = sqlalchemy.create_engine(config.URI)
78metadata = sqlalchemy.MetaData(bind=engine)
89session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=engine))
2323
2424
2525class Logger:
26 def __init__(self):
27 pass
28
29 def getTable(self, tablename):
26 def __init__(self, tablename):
3027 """This method loads a table from the database and maps it to LoadedTable class.
3128 The loaded table is available through the "lt" data member."""
3229 table = sqlalchemy.Table(tablename, metadata, autoload=True)
3330 self.lt = tableFactory(tablename)
3431 sqlalchemy.orm.mapper(self.lt, table)
35 return table
report.py
(54 / 52)
  
1import logger
2from sqlalchemy.sql import func
1import argparse
2import datetime
3import query
34
5parser = argparse.ArgumentParser(description='Generate report for the date specified.')
6parser.add_argument('-s','--start-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="Start date (YYYY-MM-DD)")
7parser.add_argument('-e','--end-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="End date (YYYY-MM-DD)")
8parser.add_argument('-S','--start-time', type=str, default = '00:00:00', help="Start time (HH:MM:SS)")
9parser.add_argument('-E','--end-time', type=str, default='23:59:59', help="Start time (HH:MM:SS)")
10args = parser.parse_args()
11startDate = args.start_date + " " + args.start_time
12endDate = args.end_date + " " + args.end_time
413
5date = raw_input("Enter the date for which you want reports?(YYYY-MM-DD)")
6dateRange = date + " 23:59:59"
7postings = logger.Logger()
8postings.getTable('lb_postings')
9print '{0}: {1}'.format("Number of postings published",
10 postings.lt.query.filter(postings.lt.status == 3,
11 postings.lt.posted.between(date, dateRange)).count())
12print '{0}: {1}'.format("Number of recordings made",
13 postings.lt.query.filter(postings.lt.posted.between(date,
14 dateRange)).count())
15print '{0}: {1}'.format("Number of impact stories",
16 postings.lt.query.filter(postings.lt.title.like('impact%'),
17 postings.lt.posted.between(date, dateRange)).count())
1814
19callDetails = logger.Logger()
20callDetails.getTable('cdr')
21avg_query = callDetails.lt.query.with_entities(func.avg(callDetails.lt.duration).label('average'),
22 func.sum(callDetails.lt.duration).label('sum')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.calldate.between(date, dateRange))
23sumCh1_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh1')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.20%'), callDetails.lt.calldate.between(date, dateRange))
24sumCh2_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh2')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.21%'), callDetails.lt.calldate.between(date, dateRange))
25sumCh3_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh3')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.22%'), callDetails.lt.calldate.between(date, dateRange))
15postings = query.Query('lb_postings')
16print '{0}: {1}'.format("Number of postings published", postings.posts(startDate, endDate))
17print '{0}: {1}'.format("Number of recordings made", postings.recordings(startDate, endDate))
18print '{0}: {1}'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate))
2619
27average = 0
28sum = 0
29sumCh1 = 0
30sumCh2 = 0
31sumCh3 = 0
32for res in sumCh1_query.all():
33 sumCh1 = res.sumCh1/60
20callDetails = query.Query('cdr')
21average_call_length = callDetails.average(startDate, endDate)
22audio_minutes = callDetails.sum(startDate, endDate)
23channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate)
24channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate)
25channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate)
3426
35for res in sumCh2_query.all():
36 sumCh2 = res.sumCh2/60
3727
38for res in sumCh3_query.all():
39 sumCh3 = res.sumCh3/60
28# avg_query = callDetails.lt.query.with_entities(func.avg(callDetails.lt.duration).label('average'),
29# func.sum(callDetails.lt.duration).label('sum')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.calldate.between(date, dateRange))
30# sumCh1_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh1')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.20%'), callDetails.lt.calldate.between(date, dateRange))
31# sumCh2_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh2')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.21%'), callDetails.lt.calldate.between(date, dateRange))
32# sumCh3_query = callDetails.lt.query.with_entities(func.sum(callDetails.lt.duration).label('sumCh3')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.channel.like('SIP/10.0.0.22%'), callDetails.lt.calldate.between(date, dateRange))
4033
41for res in avg_query.all():
42 average = res.average/60 #The duration recorded is in seconds, convert it to
43 #minutes
44 sum = res.sum/60
34# average = 0
35# sum = 0
36# sumCh1 = 0
37# sumCh2 = 0
38# sumCh3 = 0
39# for res in sumCh1_query.all():
40# sumCh1 = res.sumCh1/60
4541
46print '{0}: {1}'.format("Number of missed calls",
47 callDetails.lt.query.filter(((callDetails.lt.dcontext == 'mobilink') |
48 (callDetails.lt.dcontext == 'mobilinktata')) &
49 (callDetails.lt.calldate.between(date, dateRange))).count())
50print '{0}: {1}'.format("Number of calls answered",
51 callDetails.lt.query.filter(callDetails.lt.dcontext == 'callback',
52 callDetails.lt.calldate.between(date, dateRange)).count())
53print '{0}: {1}'.format("Average length of calls", average)
54print '{0}: {1}'.format("Total number of audio minutes played", sum)
55print '{0}: {1}'.format("Audio minutes on channel 10.0.0.20", sumCh1)
56print '{0}: {1}'.format("Audio minutes on channel 10.0.0.21", sumCh2)
57print '{0}: {1}'.format("Audio minutes on channel 10.0.0.22", sumCh3)
58print '{0}: {1}'.format("Load on channel 10.0.0.20", sumCh1/sum)
59print '{0}: {1}'.format("Load on channel 10.0.0.21", sumCh2/sum)
60print '{0}: {1}'.format("Load on channel 10.0.0.22", sumCh3/sum)
42# for res in sumCh2_query.all():
43# sumCh2 = res.sumCh2/60
44
45# for res in sumCh3_query.all():
46# sumCh3 = res.sumCh3/60
47
48# for res in avg_query.all():
49# average = res.average/60 #The duration recorded is in seconds, convert it to
50# #minutes
51# sum = res.sum/60
52
53print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate))
54print '{0}: {1}'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate))
55print '{0}: {1}'.format("Average length of calls", average_call_length)
56print '{0}: {1}'.format("Total number of audio minutes played", audio_minutes)
57print '{0}: {1}'.format("Audio minutes on channel 10.0.0.20", channel1_minutes)
58print '{0}: {1}'.format("Audio minutes on channel 10.0.0.21", channel2_minutes)
59print '{0}: {1}'.format("Audio minutes on channel 10.0.0.22", channel3_minutes)
60print '{0}: {1}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes)
61print '{0}: {1}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes)
62print '{0}: {1}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes)