DailyEvent.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: Deepcold
  5. @file: DailyEvent.py
  6. @time: 2019/6/19 10:40
  7. """
  8. import datetime
  9. from pprint import pprint
  10. from bin.utils.covert_dict_object import dict_to_object
  11. from .common.handleDate import get_date
  12. from .db.getData.fromEs import GetDataFromEs
  13. from .db.getData.fromMongo import GetDataFromMongo
  14. from .db.getData.fromMysql import GetDataFromMysql
  15. from .db.saveDate.saveEventChance import saveEventChance
  16. class DailyEvent(object):
  17. def __init__(self, config):
  18. # 将开始日期读进来
  19. self.start_date = get_date()
  20. self.push_date = datetime.date.today()
  21. self.config = config
  22. self.save_event_chance = saveEventChance(config)
  23. def run(self):
  24. # 遍历配置中的数据库
  25. for _event_type, _content in self.config.ALL_EVENT_TYPE.items():
  26. if "DEFENDANT" in _event_type:
  27. party = "DEFENDANT"
  28. else:
  29. party = "PLAINTIFF"
  30. event_type = self.config.ALL_TYPE[_event_type] # 获取事件类型
  31. for _event_sub_type, _table in _content.items():
  32. event_sub_type = self.config.ALL_TYPE[_event_sub_type] # 获取事件子类型
  33. for table_name, value in _table.items():
  34. db_config = {}
  35. db = value.DB
  36. if "QUERY_CONDITION" in value.keys():
  37. query_condition = value.QUERY_CONDITION
  38. else:
  39. query_condition = None
  40. if "SAVE_TABLE" in value.keys():
  41. save_table = value.SAVE_TABLE
  42. else:
  43. save_table = None
  44. event_config = self.config.DATABASE[db]
  45. db_type = event_config.DB_TYPE # 数据库类型
  46. db_info = event_config.DB_INFO # 数据库信息
  47. table = event_config[table_name] # 数据表信息
  48. # 封装成字典
  49. db_config["event_type"] = event_type
  50. db_config["event_sub_type"] = event_sub_type
  51. db_config["query_condition"] = query_condition
  52. db_config["db_type"] = db_type
  53. db_config["db_info"] = db_info
  54. db_config[table_name] = table
  55. db_config["table_name"] = table_name
  56. db_config["party"] = party
  57. db_config["save_table"] = save_table
  58. db_config = dict_to_object(db_config)
  59. sql_data = self.get_data(db_config)
  60. self.save_event_chance.save(sql_data, db_config)
  61. pass
  62. def get_data(self, db_config):
  63. """
  64. :param db_config:
  65. :return:
  66. """
  67. db_type = db_config.db_type
  68. if db_type == "mysql":
  69. mysql = GetDataFromMysql(db_config, self.push_date)
  70. sql_data = mysql.get_data(self.start_date)
  71. pass
  72. elif db_type == "mongodb":
  73. mongo = GetDataFromMongo(db_config, self.push_date)
  74. sql_data = mongo.get_data(self.start_date)
  75. pass
  76. elif db_type == "es":
  77. es = GetDataFromEs(db_config, self.push_date)
  78. sql_data = es.get_data(self.start_date)
  79. pass
  80. else:
  81. sql_data = []
  82. return sql_data