WaveTrans.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json
  2. import multiprocessing
  3. from service.plt_service import get_all_wind
  4. from service.trans_service import get_wave_conf, save_df_to_db, get_or_create_wave_table, \
  5. get_wave_data, delete_exist_wave_data
  6. from utils.file.trans_methods import *
  7. from utils.systeminfo.sysinfo import get_available_cpu_count_with_percent
  8. class WaveTrans(object):
  9. def __init__(self, field_code, read_path, save_path: str):
  10. self.field_code = field_code
  11. self.read_path = read_path
  12. self.save_path = save_path
  13. self.begin = datetime.datetime.now()
  14. def get_data_exec(self, func_code, arg):
  15. exec(func_code)
  16. return locals()['get_data'](arg)
  17. def del_exists_data(self, df):
  18. min_date, max_date = df['time_stamp'].min(), df['time_stamp'].max()
  19. db_df = get_wave_data(self.field_code + '_wave', min_date, max_date)
  20. exists_df = pd.merge(db_df, df,
  21. on=['wind_turbine_name', 'time_stamp', 'sampling_frequency', 'mesure_point_name'],
  22. how='inner')
  23. ids = [int(i) for i in exists_df['id'].to_list()]
  24. if ids:
  25. delete_exist_wave_data(self.field_code + "_wave", ids)
  26. def run(self):
  27. all_files = read_files(self.read_path, ['csv'])
  28. print(len)
  29. # 最大取系统cpu的 1/2
  30. split_count = get_available_cpu_count_with_percent(1 / 2)
  31. all_wind, _ = get_all_wind(self.field_code, False)
  32. get_or_create_wave_table(self.field_code + '_wave')
  33. wave_conf = get_wave_conf(self.field_code)
  34. base_param_exec = wave_conf['base_param_exec']
  35. map_dict = {}
  36. if base_param_exec:
  37. base_param_exec = base_param_exec.replace('\r\n', '\n').replace('\t', ' ')
  38. print(base_param_exec)
  39. if 'import ' in base_param_exec:
  40. raise Exception("方法不支持import方法")
  41. mesure_poins = [key for key, value in wave_conf.items() if str(key).startswith('conf_') and value]
  42. for point in mesure_poins:
  43. map_dict[wave_conf[point]] = point.replace('conf_', '')
  44. map_dict['rotational_speed'] = 'rotational_speed'
  45. with multiprocessing.Pool(split_count) as pool:
  46. file_datas = pool.starmap(self.get_data_exec, [(base_param_exec, i) for i in all_files])
  47. print("读取文件耗时:", datetime.datetime.now() - self.begin)
  48. result_list = list()
  49. for file_data in file_datas:
  50. wind_turbine_name, time_stamp, sampling_frequency, rotational_speed, mesure_point_name, mesure_data = \
  51. file_data[0], file_data[1], file_data[2], file_data[3], file_data[4], file_data[5]
  52. if mesure_point_name in map_dict.keys():
  53. result_list.append(
  54. [wind_turbine_name, time_stamp, sampling_frequency, 'rotational_speed', [float(rotational_speed)]])
  55. result_list.append(
  56. [wind_turbine_name, time_stamp, sampling_frequency, mesure_point_name, mesure_data])
  57. df = pd.DataFrame(result_list,
  58. columns=['wind_turbine_name', 'time_stamp', 'sampling_frequency', 'mesure_point_name',
  59. 'mesure_data'])
  60. df['time_stamp'] = pd.to_datetime(df['time_stamp'], errors='coerce')
  61. df['mesure_point_name'] = df['mesure_point_name'].map(map_dict)
  62. df.dropna(subset=['mesure_point_name'], inplace=True)
  63. df['wind_turbine_number'] = df['wind_turbine_name'].map(all_wind).fillna(df['wind_turbine_name'])
  64. df['mesure_data'] = df['mesure_data'].apply(lambda x: json.dumps(x))
  65. df.sort_values(by=['time_stamp', 'mesure_point_name'], inplace=True)
  66. self.del_exists_data(df)
  67. save_df_to_db(self.field_code + '_wave', df, batch_count=1000)
  68. print("总耗时:", datetime.datetime.now() - self.begin)