From d4e996b33b9e876ccce808d8fd68a43774ff1421 Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Mon, 30 Oct 2017 01:29:42 +0300 Subject: [PATCH] update --- wotstats/tasks/__init__.py | 107 ++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 42 deletions(-) diff --git a/wotstats/tasks/__init__.py b/wotstats/tasks/__init__.py index 49e5239..94889fb 100644 --- a/wotstats/tasks/__init__.py +++ b/wotstats/tasks/__init__.py @@ -26,25 +26,30 @@ def get_stats_tokenized_users(): print 111 -@celery.task(name='wotstats.tasks.hello') -def hello(): +@celery.task(name='wotstats.tasks.crontab_rush_preparation') +def crontab_rush_preparation(): + """""" if Rush.query.filter_by(status='preparation').count() == 0: now = datetime.now().replace(minute=0, second=0) new_rush = Rush() + new_rush.at_start = now + timedelta(hours=1) + new_rush.at_finish = now + timedelta(hours=24) try: - new_rush.at_start = now + timedelta(hours=1) - new_rush.at_finish = now + timedelta(hours=24) db.session.add(new_rush) db.session.commit() except Exception as e: print e db.session.rollback() + else: + return True + return False + +@celery.task(name='wotstats.tasks.crontab_rush_start') +def crontab_rush_start(): to_start = Rush.query.filter( - Rush.status == 'preparation', - Rush.at_start <= datetime.now() - ) + Rush.status == 'preparation', Rush.at_start <= datetime.now()) if to_start.count() > 0: p = to_start.first() # Count @@ -54,6 +59,7 @@ def hello(): if rush_accounts.count() == 0: p.status = 'canceled' db.session.commit() + return True else: for ra in rush_accounts.all(): # Get current stats by ra.account_id @@ -82,47 +88,62 @@ def hello(): pass p.status = 'started' db.session.commit() + return True + else: + return False + +@celery.task(name='wotstats.tasks.crontab_rush_finish') +def crontab_rush_finish(): to_finish = Rush.query.filter( - Rush.status == 'started', - Rush.at_finish <= datetime.now() - ) - if to_finish.count() > 0: - p = to_finish.first() - # Harvert stats for all rush members - for ra in RushAccounts.query.filter(RushAccounts.rush_id == p.id): - # TODO: Get WOT accoount data - # TODO: Get Stats from WOT server - # TODO: Save Stats - print ra.account_id - # Get current stats by ra.account_id - # Save battle stats to database - # - app_id = current_app.config['WG_APPLICATION_ID'] - url = 'https://api.worldoftanks.ru/wot/account/info/' - __ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, ra.account_id)).json() + Rush.status == 'started', Rush.at_finish <= datetime.now()) + if to_finish.count() == 0: + return False - account_statistics = __.get('data', {}).get("{}".format(ra.account_id)).get('statistics', {}) - account_data = __['data']["{}".format(ra.account_id)]['statistics']['all'] + finish_stats = dict() - data = { - 'battles': account_data['battles'], - 'wins': account_data['wins'], - 'losses': account_data['losses'], - 'draws': account_data['draws'], - } + p = to_finish.first() + # Harvert stats for all rush members + for ra in RushAccounts.query.filter(RushAccounts.rush_id == p.id): + # TODO: Get WOT accoount data + # TODO: Get Stats from WOT server + # TODO: Save Stats + account_id = ra.account_id + # Get current stats by ra.account_id + # Save battle stats to database + # + app_id = current_app.config['WG_APPLICATION_ID'] + url = 'https://api.worldoftanks.ru/wot/account/info/' + __ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, account_id)).json() - xx = RushAccounts.query.filter( - RushAccounts.rush_id == p.id, - RushAccounts.account_id == ra.account_id - ).first() - xx.finish_data = data - db.session.commit() - pass - # TODO: ... - p.status = 'finished' + account_statistics = __.get('data', {}).get("{}".format(ra.account_id)).get('statistics', {}) + account_data = __['data']["{}".format(account_id)]['statistics']['all'] + + data = { + 'battles': account_data['battles'], + 'wins': account_data['wins'], + 'losses': account_data['losses'], + 'draws': account_data['draws'], + } + + xx = RushAccounts.query.filter( + RushAccounts.rush_id == p.id, + RushAccounts.account_id == account_id + ).first() + xx.finish_data = data db.session.commit() + finish_stats[account_id] = { + 'wins': xx.finish_data['wins'] - account_data['wins'], + 'battles': xx.finish_data['battles'] - account_data['battles'] + } + + result = sorted(finish_stats.items(), key=lambda t: t[1]['wins'], reverse=True) + pass + # TODO: ... + p.status = 'finished' + db.session.commit() + # time.sleep(3) # current_app.log.error('wdwqd') # with current_app.test_request_context() as request: @@ -143,7 +164,9 @@ def mail_welcome(): @celery.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): # Calls test('hello') every 10 seconds. - sender.add_periodic_task(10.0, hello.s(), name='add every 10') + sender.add_periodic_task(10.0, crontab_rush_preparation.s(), name='prepare every 10') + sender.add_periodic_task(10.0, crontab_rush_start.s(), name='start every 10') + sender.add_periodic_task(10.0, crontab_rush_finish.s(), name='finish every 10') # Calls test('world') every 30 seconds # sender.add_periodic_task(30.0, test.s('world'), expires=10)