wjtx_log/wjtx_log2tga.py
2020-02-12 14:27:31 +08:00

86 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name wjtx_log2tga
Description :
Author : pengtao
date 2020/2/12
-------------------------------------------------
Change Activity:
2020/2/12:
-------------------------------------------------
"""
__author__ = 'pengtao'
from common.plog import define_logger
from common.mmysql import MysqlBase
from common.mtga import FromTga
import logging
from conf.wjtx_log_conf import *
import datetime
define_logger("/data/logs/ops/wjtx_log2tga.log")
log = logging.getLogger(__name__)
class Db2TGA:
def __init__(self, day):
self.tga = FromTga(tga_url, key)
self.tga.init_tga_write(tgaid)
self.mysql = MysqlBase(**mysql_conn)
self.day = day
def write2tga(self, data, event_name):
self.tga.put_event_data(data, event_name=event_name)
def check_data(self, data):
return data
def collect_table(self, table_name, rows):
data = []
row_name = " ".jion(rows)
sql = f"select {row_name} from {table_name} where time between '{self.day} 00:00:00' and '{self.day} 23:59:59'"
out = self.mysql.query(sql)
if out:
for line in out:
try:
temp = {}
for i in range(0, len(rows)):
temp[rows[i]] = line[i]
data.append(temp)
except Exception:
log.error(f"split {line} failed!", exc_info=True)
else:
log.error(f"get data from {table_name} failed!")
return data
def run(self):
if not datetime.datetime.strptime(self.day, "%Y%m%d"):
log.error(f"{self.day} unformat!", exc_info=True)
return False
for table in db_tables:
if table.split('_')[-1] == "$date":
table_name = f"{table.split('_')[0]}_{self.day}"
rows = db_row.get(table.split('_')[0], None)
event_name = table.split('_')[0]
else:
table_name = table
rows = db_row.get(table)
event_name = table
data = self.collect_table(table_name, rows)
new = self.check_data(data)
self.write2tga(new, event_name)
def main():
import sys
day = sys.argv[1]
if not day:
raise Exception(f"pls check inputs days={day}")
db2tga = Db2TGA(day)
db2tga.run()
if __name__ == "__main__":
main()