def __recent_valid_deals__(): """ 从本地读取折扣tag信息,如果没有,就从mysql读 :return 结构如下 { 100:{ "is_hot":True, "tags":["lining","anta"] }, 200:{ "is_hot":True, "tags":["lining","man"] } } """ if os.path.exists(__tmp_deal_info_path__()): info_d = {} def func(line): js = json.loads(line) info_d[int(js['id'])] = js['info'] file_util.read_file(__tmp_deal_info_path__(), func) return __check_deal_info__(info_d) info_d = __read_from_db__() data = [] for deal_id, info in info_d.items(): d = {'id': deal_id, 'info': info} l = my_json.dumps(d) data.append(l) file_util.cover(__tmp_deal_info_path__(), data) return __check_deal_info__(info_d)
def get_by_name(name: str): sql = "select id,name,type,value,description,update_time from recommend_config where name = '%s'" % name lst = mysql_local.execute_sql(sql, db) if len(lst) == 0: return {} id, name, type, value, description, update_time = lst(sql, db)[0] return my_json.dumps({ 'id': id, 'name': name, 'type': type, 'value': value, 'description': description, 'update_time': update_time })
def to_local(): now = int(time.time()) sql = "select id, category, is_hotpick from deal_index where cn_state = 'published' and published_time >= %s and dead_time >= %s" % ( str(now - 14 * 24 * 60 * 60), str(now)) db = 'dealmoon' data_list = [] for x in mysql_local.execute_sql(sql, db): id, category, is_hotpick = x d = {'id': id, 'category': category, 'is_hotpick': is_hotpick} data_list.append(my_json.dumps(d)) file_util.cover(path=tmp_file, data=data_list)
def refresh(): """ 重新加载数据到本地 """ sql = "select id, parent_id, label_en, label_cn,title_en, title_cn from category" db = 'dealmoon' data_list = [] for x in mysql_local.execute_sql(sql, db): id, parent_id, label_en, label_cn, title_en, title_cn = x j = { "id": id, "parent_id": parent_id, "label_en": label_en, "laben_cn": label_cn, "title_en": title_en, "title_cn": title_cn } data_list.append(my_json.dumps(j)) file_util.cover(local_file, data_list)
return { '平均每个折扣能够扩展': number_util.divide(incr_total, len(deal_tags_LST), round_num), '扩展后,每个折扣平均tag数': number_util.divide(last_total, len(deal_tags_LST), round_num) } def __stats_deal_tag__(deal_info_ddd): deal_num = len(deal_info_ddd) tag_num = 0 for deal_info in deal_info_ddd.values(): tags = deal_info['tags'] l = len(tags) tag_num += l return round(float(tag_num) / float(deal_num), 0) if __name__ == '__main__': # 读取折扣相关信息 deal_info_d = __recent_valid_deals__() # 读取相关tag信息 rel_d = __rel_tags__() # 分析匹配情况 with_tags_RATE, avg_tags_NUM, tag_inr_D = __stats__(deal_info_d, rel_d) print(my_json.dumps({ '折扣总数': len(deal_info_d), '折扣tag覆盖率': with_tags_RATE, '平均每个折扣tag数': avg_tags_NUM, '扩展后': tag_inr_D }))
2356300: { 'l': 'C', 'p': '2021/04/19 02:32:00', }, 2356925: { 'l': 'C', 'p': '2021/04/19 09:22:00' }, 2357568: { 'l': 'C', 'p': '2021/04/19 17:23:00' }, 2357380: { 'l': 'D', 'p': '2021/04/19 14:47:00', 'r': ['2021/04/20 06:08:00'] }, 2358665: { 'l': 'D', 'p': '2021/04/20 11:03:00', 'r': ['2021/04/20 18:46:00'] }, 2359363: { 'l': 'D', 'p': '2021/04/20 21:27:00', 'r': ['2021/04/21 09:47:00'] } } print(json.dumps(data))