Administrator
2023-12-15 3da5d1ef00cf7665c2d37f837e6845c92d22e31c
third_data/kpl_data_manager.py
@@ -1,4 +1,6 @@
import copy
import json
import logging
import os
import threading
import time
@@ -6,12 +8,13 @@
import requests
import constant
from db.redis_manager import RedisUtils
from db.redis_manager_delegate import RedisUtils
from log_module import async_log_util
from utils import tool
# 开盘啦历史涨停数据管理
from db import mysql_data, redis_manager
from log_module.log import logger_kpl_limit_up_reason_change
from db import mysql_data_delegate as mysql_data, redis_manager_delegate as redis_manager
from log_module.log import logger_kpl_limit_up_reason_change, logger_debug, logger_kpl_limit_up
from third_data import kpl_util, kpl_api
from third_data.code_plate_key_manager import LimitUpCodesPlateKeyManager, CodesHisReasonAndBlocksManager
@@ -44,6 +47,8 @@
    __kplPlatManager = KPLPlatManager()
    __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
    __CodesPlateKeysManager = CodesHisReasonAndBlocksManager()
    __current_code_reason_dict = {}
    __current_reason_codes_dict = {}
    @classmethod
    def __load_hist_and_blocks(cls, code):
@@ -74,6 +79,16 @@
        if records:
            cls.latest_origin_datas = records
            cls.__LimitUpCodesPlateKeyManager.set_today_limit_up([(r[0], r[5]) for r in records])
        code_reason_dict = {}
        reason_code_dict = {}
        for d in records:
            code_reason_dict[d[0]] = d[5]
            if d[5] not in reason_code_dict:
                reason_code_dict[d[5]] = set()
            reason_code_dict[d[5]].add(d[0])
        cls.__current_code_reason_dict = code_reason_dict
        cls.__current_reason_codes_dict = reason_code_dict
        # 涨停数据记录
        mysqldb = mysql_data.Mysqldb()
@@ -187,6 +202,24 @@
        bs = set([b[0] for b in results])
        return bs
    @classmethod
    def get_current_block(cls, code):
        return cls.__current_code_reason_dict.get(code)
    @classmethod
    def get_current_codes_by_block(cls, block):
        return cls.__current_reason_codes_dict.get(block)
    @classmethod
    def get_current_reason_codes_dict(cls):
        return copy.deepcopy(cls.__current_reason_codes_dict)
    @classmethod
    def get_current_reasons(cls):
        if cls.__current_reason_codes_dict:
            return cls.__current_reason_codes_dict.keys()
        return set()
class KPLDataManager:
    __latest_datas = {}
@@ -294,6 +327,7 @@
    if day in __limit_up_list_records_dict:
        datas = __limit_up_list_records_dict[day]
    else:
        logger_debug.info("从文件中获取前几天的实时涨停数据")
        datas = KPLDataManager().get_latest_from_file(KPLDataType.LIMIT_UP, 10)
        if datas:
            # 保存数据
@@ -308,9 +342,14 @@
def get_yesterday_limit_up_codes():
    yesterday_limit_up_data_records = get_current_limit_up_data_records(1)[0][1]
    yesterday_limit_up_data_records = get_yesterday_current_limit_up_records()
    yesterday_codes = set([x[0] for x in yesterday_limit_up_data_records])
    return yesterday_codes
def get_yesterday_current_limit_up_records():
    yesterday_limit_up_data_records = get_current_limit_up_data_records(1)[0][1]
    return yesterday_limit_up_data_records
# 运行拉取任务
@@ -323,14 +362,21 @@
        requests.post("http://127.0.0.1:9004/upload_kpl_data", json.dumps(root_data))
    def get_limit_up():
        last_time = 0
        while True:
            if tool.is_trade_time():
                try:
                    results = kpl_api.daBanList(kpl_api.DABAN_TYPE_LIMIT_UP)
                    results = kpl_api.getLimitUpInfo()
                    result = json.loads(results)
                    start_time = time.time()
                    __upload_data("limit_up", result)
                    if start_time - last_time >= 60:
                        last_time = time.time()
                        # 记录涨停数据
                        logger_kpl_limit_up.info(result)
                    # logger_kpl_limit_up_reason_change.info("上传耗时:{}", time.time() - start_time)
                except Exception as e:
                    pass
                    logging.exception(e)
            time.sleep(3)
    def get_bidding_money():
@@ -369,8 +415,8 @@
    threading.Thread(target=get_limit_up, daemon=True).start()
    threading.Thread(target=get_bidding_money, daemon=True).start()
    threading.Thread(target=get_market_industry, daemon=True).start()
    threading.Thread(target=get_market_jingxuan, daemon=True).start()
    # threading.Thread(target=get_market_industry, daemon=True).start()
    # threading.Thread(target=get_market_jingxuan, daemon=True).start()
if __name__ == "__main__":