示例#1
0
    def write(self, message, level, description='', color='grey'):

        if level not in self.log_level:
            self.write('level in function log.logging.write is invalid',
                       Log_Level.error, level)
            return False
        log_time = get_now_time_string()
        log_string = self.log_string_template % (
            log_time, self.group_id, str(self.account_id).ljust(12),
            level.ljust(10), message, description)

        if self.logging_mod == Log_Mod.console:
            print(colored(log_string, color))

        elif self.logging_mod == Log_Mod.file:
            self.file_preparing()
            f = open(self.log_file_name, 'a+', encoding='utf_8')
            f.write('\n' + log_string)
            f.close()

        elif self.logging_mod == Log_Mod.console_file:
            print(colored(log_string, color))

            self.file_preparing()
            f = open(self.log_file_name, 'a+', encoding='utf_8')
            f.write('\n' + log_string)
            f.close()

        elif self.logging_mod == Log_Mod.database:
            if self.database_preparing() is True:
                self.db_obj.insert_log(self.account_id, log_time, level,
                                       message, description,
                                       self.log_table_name)

        elif self.logging_mod == Log_Mod.console_database:
            print(colored(log_string, color))
            if self.database_preparing() is True:
                self.db_obj.insert_log(self.account_id, log_time, level,
                                       message, description,
                                       self.log_table_name)

        elif self.logging_mod == Log_Mod.console_database_file:
            print(colored(log_string, color))
            self.file_preparing()
            f = open(self.log_file_name, 'a+', encoding='utf_8')
            f.write('\n' + log_string)
            f.close()
            if self.database_preparing() is True:
                self.db_obj.insert_log(self.account_id, log_time, level,
                                       message, description,
                                       self.log_table_name)

        return True
示例#2
0
 def file_preparing(self):
     if self.log_file_name != '':
         if not os.path.exists(self.log_file_name):
             if os.path.dirname(self.log_file_name) != '':
                 if not os.path.exists(os.path.dirname(self.log_file_name)):
                     os.makedirs(os.path.dirname(self.log_file_name))
             f = open(self.log_file_name, 'w+', encoding='utf_8')
             f.write('this log file create at (%s) by %s\n' %
                     (get_now_time_string(), self.account_id))
             f.write(self.log_string_template %
                     ('timestamp'.ljust(30), 'group_id'.ljust(5),
                      'account_id'.ljust(20), 'log_level'.ljust(20),
                      'log_text', 'log_discription'))
             f.write('\n' + ('-' * 175))
             f.close()
             self.write('log file not exist', Log_Level.error,
                        self.log_file_name)
             self.write('log file created', Log_Level.info,
                        self.log_file_name)
                "date_m > {}".format(where_start_date_m)
            ],
            [share_info, share_info, None],
            [
                share_second_data, share_second_data,
                "date_time > {}".format(where_start_date_m * 1000000)
            ],
            [share_status, share_status, None],
            [
                share_sub_trad_data, share_sub_trad_data,
                "date_m > {}".format(where_start_date_m)
            ],
        ]
        obj_2.set_rull_table(obj_2_rule_table_where)

    start_cycle_time = get_now_time_second()

    #obj_1.select_query(force=force)
    obj_2.run(force=force)
    #obj_3.select_query(force=True)

    cycle_runtime = get_now_time_second() - start_cycle_time
    print('cycle_runtime: {}'.format(cycle_runtime))

    sleep_time = start_cycle_time + cycle_period_time - get_now_time_second()

    if sleep_time > 0:
        print('sleep sync select_process. start sleep: {0} sleep_time: {1}'.
              format(get_now_time_string(), sleep_time))
        sleep(sleep_time)
    def transfer_data_to_backup_tables(self):
        result = True
        # fail_hang_share
        res = self.backup_fail_hang_share()
        if res is not False:
            clean_table(self.local_db_info, 'fail_hang_share')

        # fail_integrity_share
        res = self.backup_fail_integrity_share()
        if res is not False:
            clean_table(self.local_db_info, 'fail_integrity_share')

        # fail_other_share
        res = self.backup_fail_other_share()
        if res is not False:
            clean_table(self.local_db_info, 'fail_other_share')

        # index_data
        # print('time: {0} :=> start backup share_status '.format(get_now_time_string()))
        # res = transfer_new_records(self.local_db_info, self.local_db_info, 'index_data', 'index_data_backup')
        #if res is not False:
        #    clean_table(self.local_db_info, 'index_data')

        # share_daily_data
        print('time: {0} :=> start backup share_daily_data'.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'share_daily_data',
                                        'share_daily_data_backup')
        if err is not False:
            clean_table(self.local_db_info, 'share_daily_data')

        # share_status
        print('time: {0} :=> start backup share_status '.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'share_status', 'share_status_backup')
        if err is not False:
            clean_table(self.local_db_info, 'share_status')

        # share_sub_trad_data
        print('time: {0} :=> start backup share_sub_trad_data'.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'share_sub_trad_data',
                                        'share_sub_trad_data_backup')
        if err is not False:
            clean_table(self.local_db_info, 'share_sub_trad_data')

        # shareholders_data
        print('time: {0} :=> start backup shareholders_data'.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'shareholders_data',
                                        'shareholders_data_backup')
        if err is not False:
            clean_table(self.local_db_info, 'shareholders_data')

        # share_second_data
        print('time: {0} :=> start backup share_second_data'.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'share_second_data',
                                        'share_second_data_backup')
        if err is not False:
            clean_table(self.local_db_info, 'share_second_data')

        # share_adjusted_data
        print('time: {0} :=> start backup share_adjusted_data'.format(
            get_now_time_string()))
        res, err = transfer_new_records(self.local_db_info, self.local_db_info,
                                        'share_adjusted_data',
                                        'share_adjusted_data_backup')
        if err is not False:
            clean_table(self.local_db_info, 'share_adjusted_data')
    def start(self):
        last_collect_share_info_time = 0
        last_collect_index_data_time = 0
        last_collect_trade_data_time = 0
        have_new_data = False

        print(self.step_collect_share_info)
        print(self.step_collect_index_data)
        print(self.step_collect_share_all_data)

        while True:
            start_cycle_time = get_now_time_second()

            # ---------------------------------------------
            # update symbol info
            now_time = get_now_time_datetime()

            if last_collect_share_info_time == 0:
                if self.step_collect_share_info is True:
                    res = self.cli.collect_share_info()
                else:
                    res = 0

                if res > 0:
                    last_collect_share_info_time = get_now_time_datetime()
                    have_new_data = True
            else:
                if now_time.day in [
                        1, 11, 21
                ] and (last_collect_share_info_time.year != now_time.year
                       or last_collect_share_info_time.month != now_time.month
                       or last_collect_share_info_time.day != now_time.day):
                    res = self.cli.collect_share_info()
                    if res > 0:
                        last_collect_share_info_time = get_now_time_datetime()
                        have_new_data = True

            # ---------------------------------------------
            # update index daily date
            int_now_time = now_time.hour * 10000 + now_time.minute * 100 + now_time.second
            if last_collect_index_data_time == 0:
                if self.step_collect_index_data is True:
                    res = self.cli.collect_index_data(mod=1)
                else:
                    res = 0

                # res = None
                if res is None:
                    last_collect_index_data_time = get_now_time_datetime()
                    have_new_data = True

            else:
                if int_now_time > self.tsetmc_update_time and (
                        now_time.year != last_collect_index_data_time.year
                        or now_time.month != last_collect_index_data_time.month
                        or now_time.day != last_collect_index_data_time.day):
                    res = self.cli.collect_index_data(mod=1)
                    if res is None:
                        last_collect_index_data_time = get_now_time_datetime()
                        have_new_data = True

            # ---------------------------------------------
            # update trade data
            int_now_time = now_time.hour * 10000 + now_time.minute * 100 + now_time.second
            if last_collect_trade_data_time == 0:
                if self.step_collect_share_all_data is True:
                    res = self.cli.run()
                else:
                    res = 0

                if res > 0:
                    last_collect_trade_data_time = get_now_time_datetime()
                    have_new_data = True

            else:
                if int_now_time > self.tsetmc_update_time and (
                        now_time.year != last_collect_trade_data_time.year
                        or now_time.month != last_collect_trade_data_time.month
                        or now_time.day != last_collect_trade_data_time.day):
                    res = self.cli.run()
                    if res > 0:
                        last_collect_trade_data_time = get_now_time_datetime()
                        have_new_data = True

            # ---------------------------------------------
            if have_new_data is True:  # have new record
                # save database update time
                self.cli.set_database_update_time(start_cycle_time)
                have_new_data = False

            sleep_time = start_cycle_time + self.cycle_period_time - get_now_time_second(
            )

            if sleep_time > 0:
                print('sleep sync process. start sleep: {0} sleep_time: {1}'.
                      format(get_now_time_string(), sleep_time))
                sleep(sleep_time)
def temp():
    cycle_period_time = 60 * 1

    client_id = client_setting.client_id
    cli = Client(client_id=client_id,
                 local_db_info=client_setting.local_db_info,
                 server_db_info=client_setting.server_db_info,
                 mod='')

    # cli.update_client_database_from_server()
    # cli.transfer_share_info()

    # cli.collect_index_data(mod=1)
    # cli.collect_share_info()
    # res = cli.run()
    # print(res)

    hour = 18
    minute = 0
    sec = 0
    tsetmc_update_time = hour * 10000 + minute * 100 + sec

    last_collect_share_info_time = 0
    last_collect_index_data_time = 0
    last_collect_trade_data_time = 0
    have_new_data = False

    while True:
        start_cycle_time = get_now_time_second()

        # ---------------------------------------------
        # update symbol info
        now_time = get_now_time_datetime()

        if last_collect_share_info_time == 0:
            res = cli.collect_share_info()
            # res = 10
            if res > 0:
                last_collect_share_info_time = get_now_time_datetime()
                have_new_data = True
        else:
            if now_time.day in [
                    1, 11, 21
            ] and (last_collect_share_info_time.year != now_time.year
                   or last_collect_share_info_time.month != now_time.month
                   or last_collect_share_info_time.day != now_time.day):
                res = cli.collect_share_info()
                if res > 0:
                    last_collect_share_info_time = get_now_time_datetime()
                    have_new_data = True

        # ---------------------------------------------
        # update index daily date
        int_now_time = now_time.hour * 10000 + now_time.minute * 100 + now_time.second
        if last_collect_index_data_time == 0:
            res = cli.collect_index_data(mod=1)
            # res = None
            if res is None:
                last_collect_index_data_time = get_now_time_datetime()
                have_new_data = True

        else:
            if int_now_time > tsetmc_update_time and (
                    now_time.year != last_collect_index_data_time.year
                    or now_time.month != last_collect_index_data_time.month
                    or now_time.day != last_collect_index_data_time.day):
                res = cli.collect_index_data(mod=1)
                if res is None:
                    last_collect_index_data_time = get_now_time_datetime()
                    have_new_data = True

        # ---------------------------------------------
        # update trade data
        int_now_time = now_time.hour * 10000 + now_time.minute * 100 + now_time.second
        if last_collect_trade_data_time == 0:
            res = cli.run()
            # res = 10
            if res > 0:
                last_collect_trade_data_time = get_now_time_datetime()
                have_new_data = True

        else:
            if int_now_time > tsetmc_update_time and (
                    now_time.year != last_collect_trade_data_time.year
                    or now_time.month != last_collect_trade_data_time.month
                    or now_time.day != last_collect_trade_data_time.day):
                res = cli.run()
                if res > 0:
                    last_collect_trade_data_time = get_now_time_datetime()
                    have_new_data = True

        # ---------------------------------------------
        if have_new_data is True:  # have new record
            # save database update time
            cli.set_database_update_time(start_cycle_time)
            have_new_data = False

        sleep_time = start_cycle_time + cycle_period_time - get_now_time_second(
        )

        if sleep_time > 0:
            print(
                'sleep sync process. start sleep: {0} sleep_time: {1}'.format(
                    get_now_time_string(), sleep_time))
            sleep(sleep_time)