sqlmask_converter.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import datetime
  2. import sqlalchemy
  3. import pandas as pd
  4. import pymongo
  5. from config_info.config import DB_URL
  6. db_client = pymongo.MongoClient(DB_URL)
  7. prisma_db = db_client["prisma-32_db"]
  8. def sqlmask_converter_to_nosql(cluster, start_date, end_date, mask_type):
  9. """Переводим маску из SQL БД в бинарный вид для транспортировки в noSQL БД.
  10. SQL вид существует для 2012-02-01::2021-12-31"""
  11. conn = 'postgresql+psycopg2://postgres:qwerty@localhost:5432/prisma'
  12. engine = sqlalchemy.create_engine(conn)
  13. connect = engine.connect()
  14. mask_prisma = pd.read_sql(
  15. f"SELECT * FROM mask_{cluster}_params WHERE date >= "
  16. f"'{start_date.year}-{start_date.month:02}-{start_date.day:02}' AND date <= \
  17. '{end_date.year}-{end_date.month:02}-{end_date.day:02}' ORDER BY date asc;", connect)
  18. data_prisma_mask = mask_prisma[[f'{mask_type}{i}_mask' for i in range(16, 0, -1)]]
  19. for i in data_prisma_mask.index:
  20. mask_params = data_prisma_mask.iloc[i]
  21. collection_prisma = prisma_db[f'{str(mask_prisma["date"][i])}_12d']
  22. upd_result = collection_prisma.update_many({'cluster': cluster},
  23. {"$set": {
  24. f'mask_of_hit_counters_{mask_type}': int(
  25. ''.join(mask_params.astype(str)), 2),
  26. f'multiplicity_of_hit_counters_{mask_type}': sum(mask_params)}})
  27. print(f'Matched documents {mask_type}_mask_{cluster} - {upd_result.matched_count}')
  28. print(f'Modified documents {mask_type}_mask_{cluster} - {upd_result.modified_count}')
  29. if __name__ == '__main__':
  30. date_time_start = datetime.date(2012, 12, 1)
  31. date_time_stop = datetime.date(2021, 12, 31)
  32. sqlmask_converter_to_nosql(cluster=1, start_date=date_time_start, end_date=date_time_stop, mask_type='amp')
  33. sqlmask_converter_to_nosql(cluster=2, start_date=date_time_start, end_date=date_time_stop, mask_type='amp')
  34. sqlmask_converter_to_nosql(cluster=1, start_date=date_time_start, end_date=date_time_stop, mask_type='n')
  35. sqlmask_converter_to_nosql(cluster=2, start_date=date_time_start, end_date=date_time_stop, mask_type='n')
  36. print('test')