From 24b6f814e31645ebd296790bb4e56b176922364b Mon Sep 17 00:00:00 2001 From: Arvind Date: Wed, 8 Jan 2014 11:18:44 +0530 Subject: [PATCH] First commit --- .gitignore | 13 ++++++++++++ logger.py | 35 ++++++++++++++++++++++++++++++++ report.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ sampleConfig.py | 2 ++ 4 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 logger.py create mode 100644 report.py create mode 100644 sampleConfig.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb1db35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +#Ignore virtualenv directories +bin/ +local/ +lib/ +include/ + +#Ignore .pyc and files saved by editors +*.*~ +*.pyc +.*.swp + +#Ignore config file +config.py \ No newline at end of file diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..d7c35bf --- /dev/null +++ b/logger.py @@ -0,0 +1,35 @@ +import sqlalchemy +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)) +query = session.query_property() + + +class Model(object): + query = query + +def tableFactory(tablename): + """Return a class type""" + class LoadedTable(Model): + pass + + LoadedTable.__name__ = tablename + return LoadedTable + + +class Logger: + def __init__(self): + pass + + def getTable(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 new file mode 100644 index 0000000..04b91cc --- /dev/null +++ b/report.py @@ -0,0 +1,60 @@ +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) diff --git a/sampleConfig.py b/sampleConfig.py new file mode 100644 index 0000000..04b6a43 --- /dev/null +++ b/sampleConfig.py @@ -0,0 +1,2 @@ +#Copy to config.py +URI = 'sqlite:///tmp/test.db' \ No newline at end of file -- 1.7.10.4