|
| 1 | +#!/bin/python3 |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | +# Author: M. Reinmuth, B. Herfort |
| 4 | +#################################################################################################### |
| 5 | +import logging |
| 6 | +import time |
| 7 | +import os |
| 8 | +import json |
| 9 | +from mapswipe_workers.basic import BaseFunctions as b |
| 10 | +from psycopg2 import sql |
| 11 | +import argparse |
| 12 | + |
| 13 | + |
| 14 | +#################################################################################################### |
| 15 | +parser = argparse.ArgumentParser(description='Process some integers.') |
| 16 | + |
| 17 | +parser.add_argument('-mo', '--modus', nargs='?', default='development', |
| 18 | + choices=['development', 'production']) |
| 19 | +#################################################################################################### |
| 20 | + |
| 21 | + |
| 22 | +def check_firebase_psql(firebase, postgres): |
| 23 | + logging.info('Checking imports') |
| 24 | + |
| 25 | + fb_db = firebase.database() |
| 26 | + p_con = postgres() |
| 27 | + fb_imports = list(fb_db.child("imports").shallow().get().val()) |
| 28 | + sql_imports = '''SELECT import_id FROM imports ''' |
| 29 | + |
| 30 | + psql_imports = [r[0] for r in p_con.retr_query(sql_imports, None)] |
| 31 | + |
| 32 | + diff_imports = set(fb_imports).difference(set(psql_imports)) |
| 33 | + if diff_imports: |
| 34 | + logging.info('Following imports are missing in postgres:') |
| 35 | + for imp in diff_imports: |
| 36 | + logging.info('Import missing in postgresql: %s' % imp) |
| 37 | + |
| 38 | + fb_projects = list(fb_db.child("projects").shallow().get().val()) |
| 39 | + sql_projects = '''SELECT project_id FROM projects''' |
| 40 | + psql_projects = [str(r[0]) for r in p_con.retr_query(sql_projects, None)] |
| 41 | + diff_projects = set(fb_projects).difference(set(psql_projects)) |
| 42 | + if diff_projects: |
| 43 | + logging.info('Following projects are missing in postgres:') |
| 44 | + for prj in diff_projects: |
| 45 | + logging.info('Project missing in postgresql: %s' % prj) |
| 46 | + else: |
| 47 | + logging.info('No projects are missing in postgresql') |
| 48 | + |
| 49 | + fb_users = list(fb_db.child("users").shallow().get().val()) |
| 50 | + sql_users = '''SELECT user_id FROM users''' |
| 51 | + psql_users = [str(r[0]) for r in p_con.retr_query(sql_users, None)] |
| 52 | + diff_users = set(fb_users).difference(set(psql_users)) |
| 53 | + if diff_users: |
| 54 | + logging.info('Following users are missing in postgres:') |
| 55 | + for usr in diff_users: |
| 56 | + logging.info('User missing in postgresql: %s' % usr) |
| 57 | + else: |
| 58 | + logging.info('No users are missing in postgresql') |
| 59 | + |
| 60 | + logging.info('Starting check for groups\n Groups for projects which are not present in postgres will be skipped..') |
| 61 | + |
| 62 | + missing_groups = {} |
| 63 | + for project in psql_projects: |
| 64 | + logging.info('Checking project: %s/%s' %(psql_projects.index(project), len(psql_projects))) |
| 65 | + fb_groups = list(fb_db.child("groups").child(project).shallow().get().val()) |
| 66 | + sql_groups = '''SELECT group_id FROM groups WHERE project_id = %s''' |
| 67 | + data = [int(project)] |
| 68 | + psql_groups = [str(r[0]) for r in p_con.retr_query(sql_groups, data)] |
| 69 | + diff_groups = set(fb_groups).difference(set(psql_groups)) |
| 70 | + if diff_groups: |
| 71 | + logging.info('found missing group') |
| 72 | + missing_groups[project] = diff_groups |
| 73 | + if missing_groups: |
| 74 | + if not os.path.exists('data'): |
| 75 | + os.makedirs('data') |
| 76 | + with open('data/missing_groups.json', 'w') as mg: |
| 77 | + json.dumps(missing_groups, mg) |
| 78 | + |
| 79 | + for group_prj in missing_groups.keys(): |
| 80 | + logging.info('One or group for project %s us is missing in postgresql' % group_prj) |
| 81 | + logging.info('For detailed information on which group, please have look at the .json' |
| 82 | + ' in data/missing_groups.json') |
| 83 | + else: |
| 84 | + logging.info('No groups are missing in postgresql') |
| 85 | + |
| 86 | +#################################################################################################### |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + start_time = time.time() |
| 91 | + args = parser.parse_args() |
| 92 | + |
| 93 | + logging.basicConfig(format="%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s", |
| 94 | + datefmt="%Y-%m-%d %H:%M:%S", |
| 95 | + filename='./logs/utils_report.log', |
| 96 | + filemode="a", |
| 97 | + level=logging.INFO) |
| 98 | + logger = logging.getLogger(name="utils_report") |
| 99 | + |
| 100 | + logging.info("Operation started in mode %s." % args.modus) |
| 101 | + |
| 102 | + firebase, postgres = b.get_environment(args.modus) |
| 103 | + |
| 104 | + check_firebase_psql(firebase, postgres) |
| 105 | + end_time = time.time() - start_time |
| 106 | + |
| 107 | + logging.info("Operation done. Duration: %s" % end_time) |
0 commit comments