From 300e5f4f9859e130c47befda68a0b20aa2b115de Mon Sep 17 00:00:00 2001 From: Arvind Date: Thu, 9 Jan 2014 18:36:08 +0530 Subject: [PATCH] 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 | 7 +--- report.py | 122 +++++++++++++++++++++++++++++++------------------------------ 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/logger.py b/logger.py index d7c35bf..eaba967 100644 --- a/logger.py +++ b/logger.py @@ -3,6 +3,7 @@ import sqlalchemy.orm import config import datetime + engine = sqlalchemy.create_engine(config.URI) metadata = sqlalchemy.MetaData(bind=engine) session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=engine)) @@ -22,14 +23,10 @@ def tableFactory(tablename): class Logger: - def __init__(self): - pass - - def getTable(self, tablename): + def __init__(self, tablename): """This method loads a table from the database and maps it to LoadedTable class. The loaded table is available through the "lt" data member.""" table = sqlalchemy.Table(tablename, metadata, autoload=True) self.lt = tableFactory(tablename) sqlalchemy.orm.mapper(self.lt, table) - return table diff --git a/report.py b/report.py index 04b91cc..b819297 100644 --- a/report.py +++ b/report.py @@ -1,60 +1,62 @@ -import logger -from sqlalchemy.sql import func - - -date = raw_input("Enter the date for which you want reports?(YYYY-MM-DD)") -dateRange = date + " 23:59:59" -postings = logger.Logger() -postings.getTable('lb_postings') -print '{0}: {1}'.format("Number of postings published", - postings.lt.query.filter(postings.lt.status == 3, - postings.lt.posted.between(date, dateRange)).count()) -print '{0}: {1}'.format("Number of recordings made", - postings.lt.query.filter(postings.lt.posted.between(date, - dateRange)).count()) -print '{0}: {1}'.format("Number of impact stories", - postings.lt.query.filter(postings.lt.title.like('impact%'), - postings.lt.posted.between(date, dateRange)).count()) - -callDetails = logger.Logger() -callDetails.getTable('cdr') -avg_query = callDetails.lt.query.with_entities(func.avg(callDetails.lt.duration).label('average'), - func.sum(callDetails.lt.duration).label('sum')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.calldate.between(date, dateRange)) -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)) -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)) -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)) - -average = 0 -sum = 0 -sumCh1 = 0 -sumCh2 = 0 -sumCh3 = 0 -for res in sumCh1_query.all(): - sumCh1 = res.sumCh1/60 - -for res in sumCh2_query.all(): - sumCh2 = res.sumCh2/60 - -for res in sumCh3_query.all(): - sumCh3 = res.sumCh3/60 - -for res in avg_query.all(): - average = res.average/60 #The duration recorded is in seconds, convert it to - #minutes - sum = res.sum/60 - -print '{0}: {1}'.format("Number of missed calls", - callDetails.lt.query.filter(((callDetails.lt.dcontext == 'mobilink') | - (callDetails.lt.dcontext == 'mobilinktata')) & - (callDetails.lt.calldate.between(date, dateRange))).count()) -print '{0}: {1}'.format("Number of calls answered", - callDetails.lt.query.filter(callDetails.lt.dcontext == 'callback', - callDetails.lt.calldate.between(date, dateRange)).count()) -print '{0}: {1}'.format("Average length of calls", average) -print '{0}: {1}'.format("Total number of audio minutes played", sum) -print '{0}: {1}'.format("Audio minutes on channel 10.0.0.20", sumCh1) -print '{0}: {1}'.format("Audio minutes on channel 10.0.0.21", sumCh2) -print '{0}: {1}'.format("Audio minutes on channel 10.0.0.22", sumCh3) -print '{0}: {1}'.format("Load on channel 10.0.0.20", sumCh1/sum) -print '{0}: {1}'.format("Load on channel 10.0.0.21", sumCh2/sum) -print '{0}: {1}'.format("Load on channel 10.0.0.22", sumCh3/sum) +import argparse +import datetime +import query + +parser = argparse.ArgumentParser(description='Generate report for the date specified.') +parser.add_argument('-s','--start-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="Start date (YYYY-MM-DD)") +parser.add_argument('-e','--end-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="End date (YYYY-MM-DD)") +parser.add_argument('-S','--start-time', type=str, default = '00:00:00', help="Start time (HH:MM:SS)") +parser.add_argument('-E','--end-time', type=str, default='23:59:59', help="Start time (HH:MM:SS)") +args = parser.parse_args() +startDate = args.start_date + " " + args.start_time +endDate = args.end_date + " " + args.end_time + + +postings = query.Query('lb_postings') +print '{0}: {1}'.format("Number of postings published", postings.posts(startDate, endDate)) +print '{0}: {1}'.format("Number of recordings made", postings.recordings(startDate, endDate)) +print '{0}: {1}'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate)) + +callDetails = query.Query('cdr') +average_call_length = callDetails.average(startDate, endDate) +audio_minutes = callDetails.sum(startDate, endDate) +channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate) +channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate) +channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate) + + +# avg_query = callDetails.lt.query.with_entities(func.avg(callDetails.lt.duration).label('average'), +# func.sum(callDetails.lt.duration).label('sum')).filter(callDetails.lt.dcontext == "callback", callDetails.lt.calldate.between(date, dateRange)) +# 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)) +# 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)) +# 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)) + +# average = 0 +# sum = 0 +# sumCh1 = 0 +# sumCh2 = 0 +# sumCh3 = 0 +# for res in sumCh1_query.all(): +# sumCh1 = res.sumCh1/60 + +# for res in sumCh2_query.all(): +# sumCh2 = res.sumCh2/60 + +# for res in sumCh3_query.all(): +# sumCh3 = res.sumCh3/60 + +# for res in avg_query.all(): +# average = res.average/60 #The duration recorded is in seconds, convert it to +# #minutes +# sum = res.sum/60 + +print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) +print '{0}: {1}'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate)) +print '{0}: {1}'.format("Average length of calls", average_call_length) +print '{0}: {1}'.format("Total number of audio minutes played", audio_minutes) +print '{0}: {1}'.format("Audio minutes on channel 10.0.0.20", channel1_minutes) +print '{0}: {1}'.format("Audio minutes on channel 10.0.0.21", channel2_minutes) +print '{0}: {1}'.format("Audio minutes on channel 10.0.0.22", channel3_minutes) +print '{0}: {1}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) +print '{0}: {1}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) +print '{0}: {1}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) -- 1.7.10.4