noSQL_db_Prisma.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import datetime
  2. import pymongo
  3. from config_info.config import *
  4. from file_reader.file_reader import FileReader
  5. # from pathlib import Path
  6. # noinspection DuplicatedCode
  7. class NoSQLPrisma:
  8. __DB_URL = DB_URL
  9. __db_client = pymongo.MongoClient(__DB_URL)
  10. __prisma_db = __db_client["prisma-32_db"]
  11. def __init__(self, cluster, single_date):
  12. self.cluster = cluster
  13. self.single_date = single_date
  14. self.file_reader = FileReader(cluster=self.cluster, single_date=self.single_date,
  15. path_to_files=f'z:\\PRISMA-32\\DataArchive\\P{self.cluster}\\data{self.single_date.year}')
  16. # def __del__(self):
  17. # pass
  18. def dinods_data_copier(self, event_datetime, trigger, det_params, dinode):
  19. try:
  20. new_record = {
  21. '_id': f'{event_datetime.date()}_{self.cluster:02}_{dinode:02}d_{int(event_datetime.hour):02}:' +
  22. f'{int(event_datetime.minute):02}:{int(event_datetime.second):02}.' +
  23. f'{str(event_datetime.microsecond)[:3]}.000.000',
  24. 'time_ns': int((int(event_datetime.hour) * 3600 + int(event_datetime.minute) * 60 + int(
  25. event_datetime.second)) * 10e8 + int(event_datetime.microsecond) * 1000),
  26. 'cluster': self.cluster,
  27. 'trigger': int(trigger),
  28. 'detectors': det_params
  29. }
  30. collection_prisma = NoSQLPrisma.__prisma_db[f'{str(event_datetime.date())}_{dinode}d']
  31. ins_result = collection_prisma.insert_one(new_record)
  32. print(f'Copied - {ins_result.inserted_id}')
  33. except pymongo.errors.DuplicateKeyError:
  34. print(f'Ошибка - {event_datetime.date()}-{event_datetime.time()}')
  35. def prisma_12d_past_data_copier(self):
  36. n_file_today, n_file_day_after = self.file_reader.reading_n_file()
  37. t_file = self.file_reader.reading_t_file()
  38. n_file_today = n_file_today.merge(t_file)
  39. self.make_parameters_from_df_12_d(n_file_today, self.single_date)
  40. if any(n_file_day_after):
  41. n_file_day_after = n_file_day_after.merge(t_file)
  42. self.make_parameters_from_df_12_d(n_file_day_after,
  43. self.single_date + datetime.timedelta(
  44. days=1))
  45. def prisma_7d_past_data_copier(self):
  46. n7_file_today, n7_file_day_after = self.file_reader.reading_n7_file()
  47. self.make_parameters_from_df_7_d(n7_file_today, self.single_date)
  48. if any(n7_file_day_after):
  49. self.make_parameters_from_df_7_d(n7_file_day_after,
  50. self.single_date + datetime.timedelta(days=1))
  51. def make_parameters_from_df_12_d(self, df, date):
  52. for index in range(len(df.index)):
  53. params = list(df.iloc[index])
  54. event_time = str(datetime.timedelta(seconds=params[0]))
  55. event_datetime = datetime.datetime(date.year, date.month, date.day,
  56. int(event_time.split(':')[0]),
  57. int(event_time.split(':')[1]), int(float(event_time.split(':')[2])),
  58. int(round(
  59. float(event_time.split(':')[2]) - int(
  60. float(event_time.split(':')[2])),
  61. 2) * 10 ** 6)) - datetime.timedelta(hours=4)
  62. trigger = params[3]
  63. amp = [int(params[j]) for j in range(4, 36, 2)]
  64. n = [int(params[j]) for j in range(5, 37, 2)]
  65. n_time_delay = params[36]
  66. detector = params[37]
  67. n_in_step = params[38]
  68. det_params = {}
  69. for i in range(1, 17):
  70. n_time_delay_by_det = []
  71. detector_index = [ind for ind, v in enumerate(detector) if v == i]
  72. for j in detector_index:
  73. n_time_delay_by_det.extend([n_time_delay[j]] * int(n_in_step[j]))
  74. # В БД будут оставаться пустые списки при нуле нейтронов, надо ли это фиксить?
  75. det_params[f'det_{i:02}'] = {
  76. 'amplitude': amp[i - 1],
  77. 'neutrons': n[i - 1],
  78. 'time_delay': n_time_delay_by_det
  79. }
  80. self.dinods_data_copier(event_datetime=event_datetime, trigger=trigger,
  81. det_params=det_params, dinode=12)
  82. return None
  83. def make_parameters_from_df_7_d(self, df, date):
  84. for index in range(len(df.index)):
  85. params = list(df.iloc[index])
  86. event_time = str(datetime.timedelta(seconds=params[0])) # перевод в utc-формат
  87. event_datetime = datetime.datetime(date.year, date.month, date.day,
  88. int(event_time.split(':')[0]),
  89. int(event_time.split(':')[1]), int(float(event_time.split(':')[2])),
  90. int(round(
  91. float(event_time.split(':')[2]) - int(
  92. float(event_time.split(':')[2])),
  93. 2) * 10 ** 6)) - datetime.timedelta(hours=4)
  94. trigger = params[2]
  95. amp = [int(params[j]) for j in range(3, 19)]
  96. det_params = {}
  97. for i in range(1, 17):
  98. det_params[f'det_{i:02}'] = {
  99. 'amplitude': amp[i - 1]
  100. }
  101. self.dinods_data_copier(event_datetime=event_datetime, trigger=trigger,
  102. det_params=det_params, dinode=7)
  103. return None