# -*- coding:utf-8 -*- import pymysql import pymysql.cursors from flask import g from app.db_func_base import * class GWPerfCloudDbHelper(TableDbHelperBase): def __init__(self, pool): self.db_pool = pool self.DB_Name = 'gwperf' self.set_dbname('gwperf') ################################################################ def query_job_report_data(self, jobid, date, query_type): sql = "select * from gw_reports where jobid = '%s' and type = '%s' and day in (%s) order by day asc" % (jobid, query_type, date) return self.do_select(sql) def query_usable_job_report_date(self, jobid): sql = "select distinct day, type from gw_reports where jobid = '%s' order by day asc" % (jobid) return self.do_select(sql) def query_gw_perf_list(self, date_list_str, waveid, tp_start): sql = "select * from gw_perfs where waveid = '%s' and day in (%s) and tp_start = '%s'" % (waveid, date_list_str, tp_start) return self.do_select(sql) def query_gw_perf_usable_date_list(self, waveid): sql = "select distinct day from gw_perfs where waveid = '%s' order by day asc" % (waveid) return self.do_select(sql) def query_gw_perf_list_with_type(self, date_list_str, waveid, tp_start, tp_type): sql = "select * from gw_perfs where waveid = '%s' and day in (%s) and tp_start = '%s' and wave_dir = %s" % (waveid, date_list_str, tp_start, tp_type) return self.do_select(sql) def query_gw_odi_list(self, date_list_str, waveid, tp_start): sql = "select * from gw_odis where waveid = '%s' and day in (%s) and tp_start = '%s'" % (waveid, date_list_str, tp_start) return self.do_select(sql) def query_gw_flow_usable_date_list(self, waveid): sql = "select distinct day from gw_flows where waveid = '%s' order by day asc" % (waveid) return self.do_select(sql) def query_gw_flow_list(self, date_list_str, waveid): sql = "select * from gw_flows where waveid = '%s' and day in (%s)" % (waveid, date_list_str) return self.do_select(sql) def query_job_usable_date_list(self, jobid): sql = "select distinct day, type from gw_reports where jobid = '%s' order by day asc" % (jobid) return self.do_select(sql) def query_gw_midway_list(self, date_list_str, waveid): sql = "select * from gw_midways where waveid = '%s' and day in (%s)" % (waveid, date_list_str) return self.do_select(sql) def query_midway_usable_date_list(self, waveid): sql = "select distinct day from gw_midways where waveid = '%s' order by day asc" % (waveid) return self.do_select(sql) def query_job_report_gw_tp_problem(self, day, day_type, jobid): sql = "select * from gw_tp_problems where day = '%s' and day_type = '%s' and jobid = '%s'" % (day, day_type, jobid) return self.do_select(sql) def insert_job_report_gw_tp_problem(self, values:[]): sql = """insert into gw_tp_problems (jobid, waveid, tp_type, weekday, time_range, day, day_type, high_stop_times, coor_stop_times, split, merge, odi, delay, bad_week, bad_tp, low_move_speed, instability_speed, bad_tp_type) values (%s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" return self.do_executemany(sql, values) def query_gw_survey_list(self, key): sql = "select * from gw_gsrs where `key` = '%s'" % (key) return self.do_select(sql) def insert_old_data_to_mysql(self, values:[]): sql = """insert into gw_gsrs (`key`, waveid, jobid, data) values (%s,%s, %s, %s)""" return self.do_executemany(sql, values) def query_job_report_by_date(self, jobid, date, query_type): conn, cursor = self.connect() try: where = "and day = %s" if query_type == 'week': where = "and day <= %s" sql = f'''select * from gw_reports where jobid = %s and type = %s {where} order by day desc limit 1''' print(cursor.mogrify(sql, (jobid, query_type, date))) cursor.execute(sql, (jobid, query_type, date)) result = cursor.fetchone() self.close(conn, cursor) return result, None except Exception as error: return None, error def query_usable_job_max_day(self, jobid, query_type): conn, cursor = self.connect() try: sql = "select max(day) `date` from gw_reports where jobid = %s and `type` = %s" print(cursor.mogrify(sql, (jobid, query_type))) cursor.execute(sql, (jobid, query_type)) result = cursor.fetchone() self.close(conn, cursor) return result, None except Exception as error: return None, error def query_all_gw_cross_examine_problems(self, day): if day == 0: return [] sql = """ select waveid, start_hm, end_hm, weekday, coordinate_dir, index_reason, index_details, xxlight_reason, xxlight_details from gw_examine_problems where day = '%s' """ % (day) return self.do_select(sql) def query_gw_cross_examine_problems(self, day, waveid): sql = """ select waveid, start_hm, end_hm, weekday, coordinate_dir, index_reason, index_details, xxlight_reason, xxlight_detail from gw_examine_problems where day = '%s' and waveid = '%s' """ % (day, waveid) return self.do_select(sql) def query_cross_offset_by_light(self, waveid, crossids, day, start_hm): conn, cursor = self.connect() try: sql = '''SELECT gcobl.waveid, gcobl.crossid, gcobl.cross_seq, gcobl.`day`, gcobl.start_hm, gcobl.end_hm, gcobl.cycle, gcobl.gst_offset FROM gw_cross_offset_by_light as gcobl join ( SELECT waveid, crossid, `day`, MAX(start_hm) AS max_start_hm FROM gw_cross_offset_by_light WHERE waveid = %s AND crossid IN %s AND `day` = %s AND start_hm <= %s GROUP BY crossid ) as t1 on gcobl.waveid = t1.waveid and gcobl.crossid = t1.crossid and gcobl.day = t1.day and gcobl.start_hm = t1.max_start_hm order by gcobl.cross_seq asc''' print(cursor.mogrify(sql, (waveid, crossids, day, start_hm))) cursor.execute(sql, (waveid, crossids, day, start_hm)) result = cursor.fetchall() self.close(conn, cursor) return result, None except Exception as error: return None, error def query_cross_offset_by_light(self, waveid, crossids, day, start_hm): conn, cursor = self.connect() try: sql = '''SELECT gcobl.waveid, gcobl.crossid, gcobl.cross_seq, gcobl.`day`, gcobl.start_hm, gcobl.end_hm, gcobl.cycle, gcobl.gst_offset FROM gw_cross_offset_by_light as gcobl join ( SELECT waveid, crossid, `day`, MAX(start_hm) AS max_start_hm FROM gw_cross_offset_by_light WHERE waveid = %s AND crossid IN %s AND `day` = %s AND start_hm <= %s GROUP BY crossid ) as t1 on gcobl.waveid = t1.waveid and gcobl.crossid = t1.crossid and gcobl.day = t1.day and gcobl.start_hm = t1.max_start_hm order by gcobl.cross_seq asc''' print(cursor.mogrify(sql, (waveid, crossids, day, start_hm))) cursor.execute(sql, (waveid, crossids, day, start_hm)) result = cursor.fetchall() self.close(conn, cursor) return result, None except Exception as error: return None, error def query_cross_tps_by_light(self, crossids, day): conn, cursor = self.connect() try: sql = '''select day, crossid,start_hm,end_hm,cycle,tp_info from cross_tps_by_light where crossid in %s and day = %s order by start_hm asc''' print(cursor.mogrify(sql, (crossids, day))) cursor.execute(sql, (crossids, day)) result = cursor.fetchall() self.close(conn, cursor) return result, None except Exception as error: return None, error def query_gw_cross_data_test(self, waveid , start_hm, end_hm, weekday, tp_type, day): sql = """ select * from gw_examine_problems where waveid = '%s' and start_hm = %s and end_hm = %s and weekday = %s and coordinate_dir = %s and day = %s """ return self.do_select(sql % (waveid, start_hm, end_hm, weekday, tp_type, day)) # if __name__ == '__main__': # tt_5min = get_latest_5min_timestamp() # print(tt_5min) # print(get_today_str())