import logging
import requests
from apscheduler.schedulers.background import BlockingScheduler
logging.basicConfig(level=logging.INFO)
LOCAL_PORT = 4000
LOCAL_HOST = '127.0.0.1'
def loop_export_task():
url = "http://{}:{}/data/export".format(LOCAL_HOST, LOCAL_PORT)
logging.info(url)
r = requests.get(url)
logging.info(r.text)
if __name__ == '__main__':
scheduler = BlockingScheduler(timezone="Asia/Shanghai")
# 每天凌晨1點執行
scheduler.add_job(loop_export_task, 'cron', day_of_week='*', hour='1', minute='0', second='0')
scheduler.start()
logging.info('The Next Run Time is: ', scheduler.get_jobs()[0].next_run_time)
@bp.route('/data/export')
def export_data():
ipmac_sql = """
select macaddr, recordtime, eventdetail, eventtype, ipaddr, pkttime, CONCAT(packet,'') as packet,
starttime, count_num, action, hashid as hash_id, endtime
from events_ipmac where date(recordtime)=date_sub(curdate(),interval 1 day);
"""
sql_list = [ipmac_sql,]
timestamp = int(time.time())
_, result = pool_connect_db.select_all(sql)
filepath = os.path.join(BASE_PATH, 'ipmac_sql')
if not os.path.exists(filepath):
os.makedirs(filepath)
with open('%s/data.json' % filepath, 'wb') as _file:
json.dump(result, _file)
return jsonify({'code': 200, 'msg': 'export data success'})