def save_data(date, request, filled, impression, click, filled_rate, ctr, ecpm, revenue): global app conn = app['mysql_connection'] if not conn: conn = toolkit.mysql_open_connection(config.mysql_config) if not conn: logging.error( "save_data:Error:mysql_open_connection:[%s,%s,%s,%s,%s,%s,%s,%s,%s]" % (date, request, filled, impression, click, filled_rate, ctr, ecpm, revenue)) return stat = save_to_db(conn, date, request, filled, impression, click, filled_rate, ctr, ecpm, revenue) if stat: app['success_count'] += 1 app['mysql_write_count'] += 1 # 没100次写数据库,需要关闭连接 if app['mysql_write_count'] > app['mysql_max_count']: toolkit.mysql_close_connection(conn) app['mysql_connection'] = None else: # 每当写数据库失败时,关闭连接,重新刷新连接 app['fail_count'] += 1 toolkit.mysql_close_connection(conn) app['mysql_connection'] = None
def sql_get_revenue(date, platform): TAG = "SQL_GET_DATE_LIST" sql = "SELECT sum(revenue) FROM ad_summary WHERE `date` = '%s' AND `platform` = '%s';" % (date, platform) mysql_config = config.mysql_config conn = toolkit.mysql_open_connection(mysql_config, TAG) if not conn: logging.error("SQL_GET_DATE_LIST:Error:mysql_open_connection") return None, False result, success = toolkit.mysql_query(conn, sql, TAG) logging.info("sql_get_revenue,result:%s" % result) toolkit.mysql_close_connection(conn) if not success or len(result) < 1: logging.error("SQL_GET_DATE_LIST:Fail:mysql_query") return None, False else: return result[0][0], True
def sql_get_date_list(): TAG = "SQL_GET_DATE_LIST" date_list = [] sql = "SELECT DISTINCT DATE FROM ad_summary;" mysql_config = config.mysql_config conn = toolkit.mysql_open_connection(mysql_config, TAG) if not conn: logging.error("SQL_GET_DATE_LIST:Error:mysql_open_connection") return None result, success = toolkit.mysql_query(conn, sql, TAG) toolkit.mysql_close_connection(conn) if not success: logging.error("SQL_GET_DATE_LIST:Fail:mysql_query") return None for r in result: date_list.append(r[0]) print "date_list", date_list return date_list
def sql_get_platform_list(): TAG = "SQL_GET_PLATFORM_LIST" platform_list = [] sql = "SELECT DISTINCT platform FROM ad_summary;" mysql_config = config.mysql_config conn = toolkit.mysql_open_connection(mysql_config, TAG) if not conn: logging.error("SQL_GET_PLATFORM_LIST:Error:mysql_open_connection") return None result, success = toolkit.mysql_query(conn, sql, TAG) toolkit.mysql_close_connection(conn) # print "result", result if not success: logging.error("SQL_GET_PLATFORM_LIST:FAIL:mysql_query") return None for r in result: platform_list.append(r[0]) print "platform_list", platform_list return platform_list
def save_data(data): global app conn = app['mysql_connection'] if not conn: conn = toolkit.mysql_open_connection(config.mysql_config) if not conn: logging.error("save_data:Error:mysql_open_connection:[%s]" % data) return stat = save_to_db(conn, data) if stat: app['success_count'] += 1 app['mysql_write_count'] += 1 # 没100次写数据库,需要关闭连接 if app['mysql_write_count'] > app['mysql_max_count']: toolkit.mysql_close_connection(conn) app['mysql_connection'] = None else: # 每当写数据库失败时,关闭连接,重新刷新连接 app['fail_count'] += 1 toolkit.mysql_close_connection(conn) app['mysql_connection'] = None