-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstart_dev.py
140 lines (110 loc) · 5.51 KB
/
start_dev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# USE start_{db}_update.py files instead to start database updates
'''
import glob
import os
import sys
workspace = os.path.dirname(os.path.abspath(__file__))
sys.path.append(workspace)
sys.path.append('/home/leadmandj/RDAS/')
sys.path.append(os.getcwd())
print(sys.path)
import sysvars
import datetime
from subprocess import *
from time import sleep
print(os.getcwd())
import argparse
from datetime import date,datetime
from AlertCypher import AlertCypher
from RDAS_GARD.methods import get_node_counts
prefix=sysvars.db_prefix
config_selection = {'ct':[prefix+'rdas.ctkg_update', 'ct_interval'], 'pm':[prefix+'rdas.pakg_update', 'pm_interval'], 'gnt':[prefix+'rdas.gfkg_update', 'gnt_interval']}
# this line below no longer needed because the neo4j databases has change their names.
# config_selection = {'ct':['clinical_update', 'ct_interval'], 'pm':['pubmed_update', 'pm_interval'], 'gnt':['grant_update', 'gnt_interval']}
def check_update(db, db_type):
"""
Checks if an update is needed for a specified database based on the configured update interval.
Parameters:
- database_abbreviation (str): Abbreviation for the database type (e.g., 'ct', 'pm', 'gnt').
Returns:
- bool: True if an update is needed, False otherwise.
"""
# Get the current date and time
today = datetime.now()
selection = config_selection[db_type]
print("selection::",selection)
# Get the last update date from the configuration
last_update = db.getConf('DATABASE',selection[0])
last_update = datetime.strptime(last_update,"%m/%d/%y")
print("last_update::",last_update)
# Calculate the time difference between today and the last update
delta = today - last_update
interval = db.getConf('DATABASE',selection[1])
interval = int(interval)
print("interval::",interval)
# Get the update interval from the configuration
last_update = datetime.strftime(last_update,"%m/%d/%y")
# Check if an update is needed based on the interval
if delta.days > interval:
return [True,last_update]
else:
return [False,last_update]
# Connect to the system database
db = AlertCypher('system')
#cred = credentials.Certificate(sysvars.firebase_key_path)
#firebase_admin.initialize_app(cred)
#firestore_db = firestore.client()
while True:
# Initialize a dictionary to track update status for each database
current_updates = {k:False for k,v in sysvars.db_abbrevs.items()}
# print("\n","current_updates::", current_updates)
print('Checking for Updates')
# Check update status for each database
for db_abbrev in sysvars.db_abbrevs:
current_updates[db_abbrev] = check_update(db, db_abbrev)[0]
print('Triggering Database Updates')
today = datetime.now()
has_updates={}
for k,v in current_updates.items():
print("updates:::",k,v)
if v[0] == True:
last_update=v[1]
print("check_last_updates::", last_update)
full_db_name = sysvars.db_abbrevs[k]
print(f'{full_db_name} Update Initiated')
has_updates[full_db_name]=True
p = Popen(['python3', 'driver_manual.py', '-db', f'{config_selection[k]}', '-m', 'update'], encoding='utf8')
p.wait()
# Update the node counts on the GARD Neo4j database (numbers used to display on the UI)
print('Updating Node Counts on GARD db')
get_node_counts()
target_address = sysvars.rdas_urls['dev']
db.run(f'STOP DATABASE gard')
p = Popen(['ssh', '-i', f'~/.ssh/id_rsa', f'{sysvars.current_user}@{target_address}', 'python3', '~/RDAS/remote_dump_and_transfer.py' ' -dir', 'gard'], encoding='utf8')
p.wait()
db.run(f'START DATABASE gard')
db.run(f'STOP DATABASE {full_db_name}')
p = Popen(['ssh', '-i', f'~/.ssh/id_rsa', f'{sysvars.current_user}@{target_address}', 'python3', '~/RDAS/remote_dump_and_transfer.py' ' -dir', f'{full_db_name}'], encoding='utf8')
p.wait()
db.run(f'START DATABASE {full_db_name}')
# Creates a backup file for the current state of the GARD database, puts that file in the transfer directory
print('Dumping GARD db')
p = Popen(['python3', 'generate_dump.py', '-dir', 'gard', '-t'], encoding='utf8')
p.wait()
# Creates a backup file for the current state of the database being updated in this iteration, puts that file in the transfer directory
print(f'Dumping {full_db_name} db')
p = Popen(['sudo', 'python3', 'generate_dump.py', f'-dir {full_db_name}', '-b', '-t', '-s dev'], encoding='utf8')
p.wait()
print(f'Transfering GARD dump to TEST server')
p = Popen(['sudo', 'python3', 'file_transfer.py', f'-dir {full_db_name}', '-s test'], encoding='utf8')
p.wait()
# Transfers the current databases of this iteration's backup file to the Testing Server's transfer folder
print(f'Transfering {full_db_name} dump to TEST server')
p = Popen(['sudo', 'python3', 'file_transfer.py', f'-dir {full_db_name}', '-s test'], encoding='utf8')
p.wait()
print(f'Update of {full_db_name} Database Complete...')
if True in has_updates.values():
ses_firebase.trigger_email(firestore_db,has_updates, date_start=last_update,date_end=datetime.strftime(today,"%m/%d/%y"))
print('database update and email sending has finished')
sleep(3600)
'''