Commit 86441169 authored by Arthur Lutz's avatar Arthur Lutz
Browse files

make the command work for different periodicities

parent c248b2134a11
......@@ -43,16 +43,22 @@ def url_count_from_stats(stats_dict):
class UpdateWebstatsCommand(Command):
""" Update web stats """
""" Update web stats
according to periodicity setting the input format for the date is different :
* month 05/2011
* day 15/05/2011
* hour 15/05/2011-13h
"""
arguments = '<instance>'
name = 'update-webstats'
min_args = 1
def get_current_stats_period(self, session, chosendate):
def get_current_stats_period(self, session, chosendate, periodicity):
""" return a statperiod for the current month, if it doesn't exist, create it """
start = first_day(chosendate)
end = last_day(start)
start, end = self.choose_period(chosendate, periodicity)
rql = 'Any P WHERE P is StatPeriod, P start "%(start_date)s", P stop "%(end_date)s"'
rset = session.execute(rql %
{'start_date':start,
......@@ -62,17 +68,43 @@ class UpdateWebstatsCommand(Command):
else:
return session.create_entity('StatPeriod', start=start, stop=end)
def choose_period(self, chosendate, periodicity):
if periodicity == 'month':
start = first_day(chosendate)
end = last_day(start)
elif periodicity == 'day':
start = datetime(chosendate.year, chosendate.month, chosendate.day)
end = datetime(chosendate.year, chosendate.month, chosendate.day, 23, 59, 59)
elif periodicity == 'hour':
start = datetime(chosendate.year, chosendate.month, chosendate.day, chosendate.hour)
end = datetime(chosendate.year, chosendate.month, chosendate.day, chosendate.hour, 59, 59)
return start, end
def choose_dateformat(self, periodicity):
return {'hour':'%m%Y%d%H',
'day': '%m%Y%d',
'month': '%m%Y'}[periodicity]
def update_stats(self, session, args):
''' parses awstats and creates or updates the corresponding
data in the cubicweb instance'''
awstatsdir = session.vreg.config.get('awstats-dir', '/var/lib/awstats')
domain = session.vreg.config.get('awstats-domain', '')
periodicity = session.vreg.config.get('awstats-periodicity', 'day') #FIXME s/day/month/
assert periodicity in ('hour', 'day', 'month')
if args:
chosendate = datetime.strptime(args[0], '%m/%Y')
# FIXME - adapt according to periodicity
input_format = {'month':'%m/%Y',
'day': '%d/%m/%Y',
'hour': '%d/%m/%Y-%Hh'}[periodicity]
chosendate = datetime.strptime(args[0], input_format)
# TODO - probably need a command to update stats from day X to day Y...
else:
chosendate = datetime.now()
stats_period = self.get_current_stats_period(session, chosendate)
awstatsdir = session.vreg.config.get('awstats-dir', '/var/lib/awstats')
domain = session.vreg.config.get('awstats-domain', '')
filename = 'awstats%s%s.txt' % (chosendate.strftime('%m%Y'), domain and '.%s' % domain)
stats_period = self.get_current_stats_period(session, chosendate, periodicity)
dateformat_in_file = self.choose_dateformat(periodicity)
filename = 'awstats%s%s.txt' % (chosendate.strftime(dateformat_in_file), domain and '.%s' % domain)
stats_dict = extract_stats_dict(awstatsdir, filename)
normal_dict, rdf_dict = url_count_from_stats(stats_dict)
is_rdf = False
......@@ -169,7 +201,7 @@ Number of stat objects skipped : %(skipped)s
def run(self, args):
appid = args.pop(0)
cw_cnx, session = self._init_cw_connection(appid)
session.set_pool()
session.set_cnxset()
self.update_stats(session, args)
session.commit()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment