Пример #1
0
    def __init__(self, publish):
        log_name = "proxy_sever"
        config_path = "./conf/config_test.conf"
        if publish == 1:
            config_path = "./conf/config.conf"
                    
        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler(log_name, logpath=log_path, debug=publish)

        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.redis_client = RedisClient(config_path, "REDIS", self.logger)

        self.redis_client.connect()
        self.mysql_db.connect()

        tornado.web.Application.__init__(self, [
            (r"/status", StatusCheck),
            (r"/Ticket/reqTicket.json", ReqTicketHandler),
            (r"/Ticket/reqOrderTicket.json", ReqOrderTicketHandler),
            (r"/Ticket/orderCancel.json", OrderCancel),
            (r"/TicketOrder/callback", ReqTicketCallBack),
            (r"/test", Test1Handler),
        ], debug=True)
Пример #2
0
    def __init__(self):
        log_name = "proxy_sever"
        config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler(log_name,
                                     logpath=log_path,
                                     debug=tornado.options.options.debug)

        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.redis_client = RedisClient(config_path, "REDIS", self.logger)

        self.redis_client.connect()
        self.mysql_db.connect()

        tornado.web.Application.__init__(self, [
            (r"/status", StatusCheck),
            (r"/Ticket/reqOrderTicket.json", ReqOrderTicketHandler),
            (r"/Ticket/orderCancel.json", OrderCancel),
            (r"/admin/update/balance", UpdateBalanceHandler),
            (r"/test", Test1Handler),
        ])
Пример #3
0
    def __init__(self, publish):
        log_name = "ticket_web_sever"
        config_path = "./conf/config_test.conf"
        if publish == 1:
            config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)
            
        self.ticket_token = {
                    "appid":      "14324152292", 
                    "appsecret":  "59a69169ff81415dfc30ac652353f69f", 
                    "extra_code": "abx23579436", 
                    "ticket_uid": "proxy_test_uid"
                    }

        self.logger = logger_handler(log_name, logpath=log_path, debug=publish)

        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)            
        self.redis_client = RedisClient(config_path, "REDIS", self.logger)
        
        self.mysql_db.connect()
        self.redis_client.connect()
                           
        tornado.web.Application.__init__(self, [
            (r"/Ticket/uploadIdImg.json", UploadImgHandler),
            (r"/Ticket/reqOrderTicket.json", OrderRequestHandler),
            (r"/Ticket/orderCancel.json", OrderCancelRequestHandler),
            (r"/Ticket/report.json", TicketReportHandler),
            (r"/TicketOrder/callback", ReqTicketCallBack),
        ], debug=True)
Пример #4
0
    def __init__(self):
        log_name = "crontab_update"
        config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler(log_name, logpath=log_path, debug=1)
        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.admin_url = "http://127.0.0.1:8080/admin/update/balance"
Пример #5
0
    def __init__(self, publish):
        config_path = "./conf/config_test.conf"
        if publish == 1:
            config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler("crontab_stats",
                                     logpath=log_path,
                                     debug=1)

        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.redis_client = RedisClient(config_path, "REDIS", self.logger)

        self.redis_client.connect()
        self.mysql_db.connect()
Пример #6
0
class CrontabBalance():
    def __init__(self):
        log_name = "crontab_update"
        config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler(log_name, logpath=log_path, debug=1)
        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.admin_url = "http://127.0.0.1:8080/admin/update/balance"

    def crontab_update(self, uid):
        end_time = datetime.now()
        start_time = end_time + timedelta(days=-1)

        stime = start_time.strftime("%Y-%m-%d 00:00:00")
        utime = end_time.strftime("%Y-%m-%d 00:00:00")

        param = {"start_time": stime, "end_time": utime, "uid": uid}
        url = self.admin_url + "?" + urllib.parse.urlencode(param)

        self.logger.info("url: %s" % url)

        resp_headers, resp_data, status_code, err = request_query(url)
        if err is not None:
            self.logger.error(err)
            return err

        self.logger.info("status_code: %d" % status_code)
        if status_code < 200 or status_code > 400:
            self.logger.error(resp_data)
            return "reqest failed"

        self.logger.info("resp_data: %s" % resp_data)

        try:
            hdata = json.loads(resp_data)
        except Exception as err:
            self.logger.error(err)
            return err

        if hdata["errcode"] < 0:
            self.logger.error(hdata["errmsg"])
            return hdata["errmsg"], None
        else:
            self.logger.info(hdata["errmsg"])

        self.logger.info("update uid:%s blance sucess" % uid)
        return None

    def crontab_uids(self):
        if self.mysql_db.connect() is not None:
            return

        sql = "select uid from account_balance group by uid"
        qs, err = self.mysql_db.execute_query_sql(sql)
        if err is not None:
            self.logger.error(err)
            return

        cuids = [{"uid": q[0], "status":-1} for q in qs]
        self.logger.info("uids: %s" % cuids)

        while True:
            htime = datetime.now().strftime("%H")
            self.logger.info("cur time: %s" % datetime.now())
            if int(htime) < 1 or int(htime) > 3:
                time.sleep(30)
                break

            for cuid in cuids:
                if cuid["status"] == 0:
                    continue

                if self.crontab_update(cuid["uid"]) is None:
                    cuid["status"] = 0
            time.sleep(30)
Пример #7
0
 def __init__(self, *args, **kwargs):
     """Initialize MySql client."""
     self._mysql_client = MySqlClient(config, logger)
     super().__init__(*args, **kwargs)
Пример #8
0
class TaskHandler(BaseHTTPRequestHandler):
    """Class handler of client requests."""
    def __init__(self, *args, **kwargs):
        """Initialize MySql client."""
        self._mysql_client = MySqlClient(config, logger)
        super().__init__(*args, **kwargs)

    def response(self, status, message):
        """Send response with headers.

        :Parameters:
            - `status`: http status code.
            - `message`: a string with custom message.
        """
        self.send_response(status, message)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

    def do_GET(self):
        """Handle GET command."""
        if self.path == 'get_job':
            # Get job for client.
            try:
                new_job = self._mysql_client.get_job()
                if new_job:
                    self.response(200, 'Get job')
                else:
                    self.response(200, 'No Available jobs')
                # Send job info to client.
                self.wfile.write(json.dumps(new_job).encode())
            except MySqlException:
                self.send_error(404, "Failed to GET job")
        else:
            self.send_error(404, 'Not found')

    def do_POST(self):
        """Handle POST command."""
        if self.path == 'task':
            # Create new task.
            content_len = int(self.headers.get('Content-Length'))
            post_body = self.rfile.read(content_len)
            try:
                job_info = json.loads(post_body)
                host = self.client_address[0]
                job_info['client_host'] = host
                self._mysql_client.create_job(job_info)
                self.response(201, 'POST task')
                # Send file content to client.
                self.wfile.write(post_body)
            except MySqlException:
                self.send_error(404, 'Failed to POST job')
            except ValueError as error:
                logger.error(f"Error when loading json {post_body}:\n{error}")
                self.send_error(400, 'Bad request')
        else:
            self.send_error(404, 'Not found')
        return

    def do_PUT(self):
        """Handle PUT command."""
        if self.path == 'job_result':
            # Update task
            content_len = int(self.headers.get('Content-Length'))
            put_body = self.rfile.read(content_len)
            job_info, result_info = json.loads(put_body)
            job_info['status'] = 'finished'
            try:
                self._mysql_client.update_job(job_info, result_info)
                self.response(200, 'Task updated')
            except MySqlException as error:
                logger.error(f'MySqlError: {error}')
                self.send_error(404, 'Failed to update task')
        else:
            self.send_error(404, 'Not found')
Пример #9
0
class CrontabStats():
    def __init__(self, publish):
        config_path = "./conf/config_test.conf"
        if publish == 1:
            config_path = "./conf/config.conf"

        try:
            self.config = configparser.ConfigParser()
            self.config.read(config_path)
            log_path = self.config.get("LOG", "path")
        except Exception as e:
            raise Exception(e)

        self.logger = logger_handler("crontab_stats",
                                     logpath=log_path,
                                     debug=1)

        self.mysql_db = MySqlClient(config_path, "MYSQL", self.logger)
        self.redis_client = RedisClient(config_path, "REDIS", self.logger)

        self.redis_client.connect()
        self.mysql_db.connect()

    def stats_curday_balance(self, uid, start_time, end_time):
        data = {
            "order_balance": None,
            "cancel_balance": None,
            "updateTime": None
        }

        sql = "select sum(ticketPrices),updateTime from order_ticket where uid='%s' and status=1 and updateTime>='%s' and updateTime<'%s'" % (
            uid, start_time, end_time)
        self.logger.info(sql)

        qs, err = self.mysql_db.execute_query_sql(sql)
        if err is not None:
            return data, err

        self.logger.info("order balance: %s" % str(qs))
        if tuple(qs[0]) == (None, None):
            return data, None

        data["order_balance"] = qs[0][0]
        data["updateTime"] = qs[0][1].strftime("%Y-%m-%d %H:%M:%S")

        sql = "select sum(ticketPrices),updateTime from order_cancel where uid='%s' and cancelStatus=1 and updateTime>='%s' and updateTime<'%s'" % (
            uid, start_time, end_time)
        self.logger.info(sql)
        qs, err = self.mysql_db.execute_query_sql(sql)
        if err is not None:
            return data, err

        self.logger.info("cancel balance: %s" % str(qs))
        if tuple(qs[0]) == (None, None):
            return data, None

        data["cancel_balance"] = qs[0][0]
        data["updateTime"] = qs[0][1].strftime("%Y-%m-%d %H:%M:%S")

        return data, None

    def cmp_update_time(self, update_time, start_time, end_time):
        s_time = datetime.strptime(update_time.strftime("%Y-%m-%d"),
                                   "%Y-%m-%d")
        stime = datetime.strptime(start_time.split(" ")[0], "%Y-%m-%d")
        etime = datetime.strptime(end_time.split(" ")[0], "%Y-%m-%d")

        self.logger.info("s_time:%s stime: %s etime:%s" %
                         (s_time, stime, etime))
        if s_time >= stime or s_time >= etime:
            return True
        return False

    def valid_reqeust_time(self, start_time, end_time):
        if validate_date_str(start_time, "%Y-%m-%d %H:%M:%S") == False or \
                        validate_date_str(end_time, "%Y-%m-%d %H:%M:%S") == False:
            return True

        c1 = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        c2 = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

        self.logger.info("c1:%s c2:%s" % (c1, c2))
        if c1 + timedelta(days=1) < c2 or c1 > c2:
            return True
        return False

    def stats_blanace(self, uid):
        now_time = datetime.now()
        start_time = (now_time +
                      timedelta(days=-1)).strftime("%Y-%m-%d 00:00:00")
        end_time = now_time.strftime("%Y-%m-%d 00:00:00")

        if self.valid_reqeust_time(start_time, end_time):
            self.logger.error(r"时间参数越界")
            return

        sql = "select totalBalance,updateTime from account_balance where uid='%s' order by updateTime desc limit 1" % uid
        qs, err = self.mysql_db.execute_query_sql(sql)
        if err is not None:
            self.logger.error(err)
            return

        if tuple(qs[0]) == (None, None):
            self.logger.error(r"非法uid")
            return

        total_balance = qs[0][0]
        update_time = qs[0][1]

        self.logger.info("total balance: %s update_time:%s" %
                         (total_balance, update_time))
        if self.cmp_update_time(update_time, start_time, end_time) == True:
            self.logger.info(r"己经更新过余额")
            return

        data, err = self.stats_curday_balance(uid, start_time, end_time)
        if err is not None:
            self.logger.info(err)
            return

        self.logger.info("query balance: %s" % json.dumps(data))
        if data["order_balance"] is None and data["cancel_balance"] is None:
            self.logger.info("%s-%s无交易余额" % (start_time, end_time))
            return

        trans_balance = 0.0
        if data["order_balance"] is not None:
            trans_balance = float(data["order_balance"])

        if data["cancel_balance"] is not None:
            trans_balance = trans_balance - float(data["cancel_balance"])

        balance = total_balance - trans_balance
        self.logger.info("total_balance: %f trans_balance: %f balance: %f" %
                         (total_balance, trans_balance, balance))
        hdata = {
            "totalBalance": balance,
            "lastTransMoney": trans_balance,
            "uid": uid,
            "updateTime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "statsTime": data["updateTime"],
            "operator": 1
        }

        self.logger.info("hdata: %s" % json.dumps(hdata))

        if self.redis_client.hset("ticket-uid", uid, balance) == False:
            self.logger.info(r"更新交易余额失败")
            return

        if self.redis_client.set("ticket_balance_uid_%s" % uid,
                                 balance) is None:
            self.logger.info(r"更新缓存余额失败")
            return

        if self.mysql_db.insert("account_balance", hdata) is not None:
            self.logger.info(r"更新余额失败")
            return

        if balance < 0.0:
            self.logger.info(r"余额不足:%s" % hdata)
        else:
            self.logger.info(r"交易正常: " % hdata)
        self.logger.info("=====================end")

    def crontab_uids(self):
        sql = "select uid from account_balance group by uid"
        qs, err = self.mysql_db.execute_query_sql(sql)
        if err is not None:
            self.logger.error(err)
            return

        cuids = [{"uid": q[0], "status": -1} for q in qs]

        while True:
            htime = datetime.now().strftime("%H")
            if int(htime) < 1 or int(htime) > 3:
                time.sleep(300)
                break

            self.logger.info("crontab time: %s" % datetime.now())
            for cuid in cuids:
                if cuid["status"] == 0:
                    continue

                if self.stats_blanace(cuid["uid"]) is None:
                    cuid["status"] = 0
            time.sleep(300)