#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: Deepcold @file: DailyEvent.py @time: 2019/6/19 10:40 """ import datetime from pprint import pprint from bin.utils.covert_dict_object import dict_to_object from .common.handleDate import get_date from .db.getData.fromEs import GetDataFromEs from .db.getData.fromMongo import GetDataFromMongo from .db.getData.fromMysql import GetDataFromMysql from .db.saveDate.saveEventChance import saveEventChance class DailyEvent(object): def __init__(self, config): # 将开始日期读进来 self.start_date = get_date() self.push_date = datetime.date.today() self.config = config self.save_event_chance = saveEventChance(config) def run(self): # 遍历配置中的数据库 for _event_type, _content in self.config.ALL_EVENT_TYPE.items(): if "DEFENDANT" in _event_type: party = "DEFENDANT" else: party = "PLAINTIFF" event_type = self.config.ALL_TYPE[_event_type] # 获取事件类型 for _event_sub_type, _table in _content.items(): event_sub_type = self.config.ALL_TYPE[_event_sub_type] # 获取事件子类型 for table_name, value in _table.items(): db_config = {} db = value.DB if "QUERY_CONDITION" in value.keys(): query_condition = value.QUERY_CONDITION else: query_condition = None if "SAVE_TABLE" in value.keys(): save_table = value.SAVE_TABLE else: save_table = None event_config = self.config.DATABASE[db] db_type = event_config.DB_TYPE # 数据库类型 db_info = event_config.DB_INFO # 数据库信息 table = event_config[table_name] # 数据表信息 # 封装成字典 db_config["event_type"] = event_type db_config["event_sub_type"] = event_sub_type db_config["query_condition"] = query_condition db_config["db_type"] = db_type db_config["db_info"] = db_info db_config[table_name] = table db_config["table_name"] = table_name db_config["party"] = party db_config["save_table"] = save_table db_config = dict_to_object(db_config) sql_data = self.get_data(db_config) self.save_event_chance.save(sql_data, db_config) pass def get_data(self, db_config): """ :param db_config: :return: """ db_type = db_config.db_type if db_type == "mysql": mysql = GetDataFromMysql(db_config, self.push_date) sql_data = mysql.get_data(self.start_date) pass elif db_type == "mongodb": mongo = GetDataFromMongo(db_config, self.push_date) sql_data = mongo.get_data(self.start_date) pass elif db_type == "es": es = GetDataFromEs(db_config, self.push_date) sql_data = es.get_data(self.start_date) pass else: sql_data = [] return sql_data