class LhbDfcfDataAnalysis:
    """
      This class is the base class of processor.
      """
    def __init__(self):
        """
        Parameters
        ----------
        """
        self.config_parser = ConfigParserUtil.get_config_parser(
            "config/env.cfg")
        self.mysql_database = None
        self.mysql_host = None
        self.mysql_user = None
        self.mysql_password = None
        self.mysql_args = None
        self.database_instance = None
        self.logger = get_logger(name="lhb_dfcf_data_analysis")
        self.parse_config()
        self.setup_mysql()

    def parse_config(self):
        """
        This method parses arguments from command line.
        Returns
        -------
        None
        """
        env = os.environ.get("ENV")  # ENV equals section name in env.cfg
        self.mysql_host = self.config_parser.get(env, "MYSQL_HOST")
        self.mysql_user = self.config_parser.get(env, "MYSQL_USER")
        self.mysql_password = self.config_parser.get(env, "MYSQL_PASSWORD")
        self.mysql_database = self.config_parser.get("lhb", "MYSQL_DATABASE")

    def setup_mysql(self):
        """
        This method sets up MySQL database connection instance.
        Returns
        -------
        None
        """
        self.mysql_args = {
            "host": self.mysql_host,
            "database": self.mysql_database,
            'user': self.mysql_user,
            "password": self.mysql_password
        }
        self.database_instance = LhbDB(self.mysql_args)
        self.database_instance.connect_db()

    def get_summary_info_by_start_date_and_end_date(self, start_date,
                                                    end_date):
        return self.database_instance.select_summary_info_dfcf_by_date(
            start_date, end_date)

    def get_summary_info_by_stock_id(self, stock_id):
        return self.database_instance.select_summary_info_dfcf_by_stock_id(
            stock_id)
Exemple #2
0
class LhbSummaryDfcfProcessor(Processor):
    def __init__(self):
        super().__init__()
        self.parse_config()
        logging.basicConfig(format='%(asctime)s %(message)s')
        self.logger = get_logger('lhb_summary_dfcf_processor.log')

    def parse_config(self):
        """
        This method parses config from env.cfg
        Returns
        -------
        None
        """
        super().parse_config()
        self.redis_key = self.config_parser.get("lhb",
                                                "LHB_SUMMARY_DFCF_REDIS_KEY")
        self.mysql_database = self.config_parser.get("lhb", "MYSQL_DATABASE")

    def insert_lhb_summary_table(self, items):
        """
        This method inserts a list of items into table cinema_info
        Parameters
        ----------
        items: list of cinema items to insert

        Returns
        -------
        None
        """
        for item in items:
            row = (item['lhb_date'], item['stock_id'], item['stock_name'],
                   item['close_price'] if item['close_price'] != '' else -1,
                   decimal.Decimal("%.2f" % float(item['change_percent']))
                   if item['change_percent'] != '' else -10000,
                   item['lhb_net_value'] if item['lhb_net_value'] != '' else 0,
                   item['lhb_buy_value'] if item['lhb_buy_value'] != '' else 0,
                   item['lhb_sell_value'] if item['lhb_sell_value'] != '' else
                   0, item['lhb_total_value']
                   if item['lhb_total_value'] != '' else 0,
                   item['trade_amount'] if item['trade_amount'] != '' else -1,
                   decimal.Decimal("%.2f" % float(item['net_value_percent']))
                   if item['net_value_percent'] != '' else -1,
                   decimal.Decimal("%.2f" % float(item['total_value_percent']))
                   if item['total_value_percent'] != '' else -1,
                   decimal.Decimal("%.2f" % float(item['turnover_ratio']))
                   if item['turnover_ratio'] != '' else -1,
                   item['reason'].encode('utf-8'),
                   get_current_timestamp("Asia/Shanghai"))
            try:
                self.database_instance.insert_summary_info_dfcf(row)
            except Exception as e:
                print(item)
                self.logger.info(item)
                self.logger.error(e)

    def process_items(self):
        """
        Process items from a redis queue.
        """

        processed_item_number = 0

        while processed_item_number < self.limit:
            items = self.get_batch_items()
            if len(items) == 0:
                time.sleep(self.sleep_secs)
                continue
            self.insert_lhb_summary_table(items)
            processed_item_number += len(items)

        self.database_instance.close_db()
        self.session.close()

    def setup_mysql(self):
        """
        This method sets up MySQL database connection instance.
        Returns
        -------
        None
        """

        super().setup_mysql()
        self.database_instance = LhbDB(self.mysql_args)
        self.database_instance.connect_db()
class LhbDetailProcessor(Processor):
    def __init__(self):
        super().__init__()
        self.parse_config()
        logging.basicConfig(format='%(asctime)s %(message)s')
        self.logger = logging.getLogger('lhb_detail_processor')

    def parse_config(self):
        """
        This method parses config from env.cfg
        Returns
        -------
        None
        """
        super().parse_config()
        self.redis_key = self.config_parser.get("lhb", "LHB_DETAIL_REDIS_KEY")
        self.mysql_database = self.config_parser.get("lhb", "MYSQL_DATABASE")

    def insert_lhb_detail_table(self, items):
        """
        Parameters
        ----------
        items: list of cinema items to insert

        Returns
        -------
        None
        """
        for item in items:
            row = (item['stock_id'],
                   item['end_date'],
                   item['reason'].encode('utf-8'),
                   item['change_percent'],
                   item['buy_value'],
                   item['sell_value'],
                   item['net_value'],
                   get_current_timestamp("Asia/Shanghai"))
            self.database_instance.insert_detail_info(row)

    def process_items(self):

        """
        Process items from a redis queue.
        """

        processed_item_number = 0

        while processed_item_number < self.limit:
            items = self.get_batch_items()
            if len(items) == 0:
                time.sleep(self.sleep_secs)
                continue
            self.insert_lhb_summary_table(items)
            processed_item_number += len(items)

        self.database_instance.close_db()
        self.session.close()

    def setup_mysql(self):

        """
        This method sets up MySQL database connection instance.
        Returns
        -------
        None
        """

        super().setup_mysql()
        self.database_instance = LhbDB(self.mysql_args)
        self.database_instance.connect_db()