diff --git a/handler/reflush_ad.py b/handler/reflush_ad.py index ba5f661..2362302 100644 --- a/handler/reflush_ad.py +++ b/handler/reflush_ad.py @@ -4,143 +4,18 @@ import sys from flask import Flask, jsonify from flask_restful import reqparse, abort, Api, Resource import logging -from myredis.myredis import my_redis -from mysql.mmysql import MysqlBase -from prod_config import mysql_promotion_config -import datetime -import pdb -import json -import copy -import time +from ops.ad_produce import produce_task log = logging.getLogger(__name__) parser = reqparse.RequestParser() -parser.add_argument('locationid') +# parser.add_argument('locationid') class RelushADinfo(Resource): def __init__(self): - self.args = parser.parse_args() - self.mydb = MysqlBase(**mysql_promotion_config) - self.expired = 60 * 2 * 60 - self.remove_list = ('ad_num', 'gameid') + pass def get(self): - locationid = self.args['locationid'] - if locationid: - need_change_keys = set() - key_word = f"ad::*_{locationid}_*::*::{locationid}" - adlists = my_redis.keys(key_word) - if adlists: - for key in adlists: - need_change_keys.update(my_redis.smembers(key)) - - if need_change_keys: - for one in need_change_keys: - key = f"adinfo::{one}::info" - log.info(f"reflush key={key} set expire!") - my_redis.expire(key, 1) - time.sleep(1) - adid = one.split('_')[0] - locationid = one.split('_')[1] - areas = self._get_areas(locationid) - if areas: - for area in areas: - info = self._get_adinfo(adid) - if info: - info['area'] = area - info['locationid'] = locationid - full_info = self._update_localtion(locationid, info) - for item in self.remove_list: - full_info.pop(item) - my_redis.hmset(key, full_info) - log.info(f"reflush key ={key} with {full_info}") - my_redis.expire(key, self.expired) - return jsonify({'code': 200, 'message': f"reflush key locationid={locationid}!"}) - else: - return jsonify({'code': 200, 'message': f"key not found with locationid={locationid}!"}) - - - def _get_areas(self, locationid): - sql = f"SELECT area FROM `location` WHERE id={locationid}" - data = self.mydb.query(sql) - if data: - return data[0][0].replace("[", "").replace("]", "").replace('"', "").split(',') - else: - log.error(f"get area from db failed,location={locationid}!") - return None - - def _get_adinfo(self, id): - sql = f"""select - id, - name, - ad_num, - ad_title, - ad_body, - ad_image, - jump_param, - ad_sort, - companyid, - gameid, - channelid, - jump_status, - locationid , - ad_property, - createtime - from - ad - where - status=1 - and in_used=1 - and id={id} """ - - - data = self.mydb.query(sql) - if data: - try: - line = data[0] - except Exception: - log.error(f"split data {data} failed!", exc_info=True) - if line: - try: - item = {} - item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item[ - 'jump_param'], item['ad_sort'], item['companyid'], item['gameid'], item['channelid'], item[ - 'jump_status'], item['locationid'], item['ad_property'], item['createtime'] = line - item['createtime'] = datetime.datetime.strftime(item['createtime'], "%Y-%m-%d %H:%M:%S") - if item.get('jump_param', "") and (item['ad_property'].find("jump_param") == -1): - temp = {} - temp['jump_param'] = item.get('jump_param', "") - if not item['ad_property']: - item['ad_property'] = {} - if not isinstance(item.get('ad_property', {}), dict): - if not item.get('ad_property', {}): - item['ad_property'] = {}.update(temp) - else: - item['ad_property'] = json.loads( - item.get('ad_property', {}).replace("‘", '"').replace("’", '"')) - item['ad_property'].update(temp) - item['ad_property'] = json.dumps(item['ad_property']) - return item - except Exception: - log.error(f"write {item}", exc_info=True) - return None - else: - return None - else: - return None - - - - def _update_localtion(self, id, line): - new = copy.deepcopy(line) - location_sql = f"select x,y,x_offset,y_offset,type,mode,ld_property from location WHERE in_used=1 and id={id}" - data = self.mydb.query(location_sql) - try: - new['x'], new['y'], new['x_offset'], new['y_offset'], new['type'], new['mode'], new['ld_property'] = data[0] - new['area'] = f"{new['area']},{new['x']},{new['y']},{new['x_offset']},{new['y_offset']}" - except Exception: - log.error(f"get localtion info by sql={location_sql} error!", exc_info=True) - return None - return new + produce_task() + return jsonify({'code': 200, 'message': "reflush AD cache success!"})