Skip to content

Commit 40c3374

Browse files
authored
Merge pull request #15 from pone-software/patricks_mint_scripts
Get info from MongoDB script
2 parents 09ed36b + 4c25af6 commit 40c3374

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

scripts/get_info_from_db.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from sshtunnel import SSHTunnelForwarder
2+
from pymongo import MongoClient
3+
from dbetto import AttrsDict
4+
import sys
5+
6+
def get_data(database='tum',
7+
collection='Devices',
8+
query:dict = {'uid':{"$regex": 'p-1-1-om-hs-*'} },
9+
ssh_host = 'production.pacific-neutrino.org',
10+
ssh_user = '',
11+
ssh_password = '',
12+
ssh_port = 22,
13+
remote_bind_address = ('127.0.0.1', 27017), # MongoDB on remote server
14+
mongo_username = '',
15+
mongo_password = '',
16+
) -> AttrsDict:
17+
"""queries production database
18+
19+
Parameters
20+
----------
21+
database : str
22+
Database to query
23+
collection : str
24+
Collection to query
25+
query : dict
26+
MongoDB query
27+
ssh_host : str
28+
Address of remote server.
29+
ssh_port : int
30+
Port of remote server
31+
remote_bind_address : tuple
32+
Tuple of mongoDB binds on local machine (address , port)
33+
mongo_username : str
34+
username of the mongoDB
35+
mongo_password : str
36+
password of the mongoDB
37+
38+
Returns
39+
-------
40+
AttrsDict
41+
Attributized dict with entries matching the query result {mongoid:db_entry}
42+
"""
43+
result = {}
44+
# Start SSH tunnel
45+
with SSHTunnelForwarder(
46+
(ssh_host, ssh_port),
47+
ssh_username=ssh_user,
48+
ssh_password=ssh_password,
49+
remote_bind_address=remote_bind_address,
50+
local_bind_address=('127.0.0.1', 27017) # optional: auto-assign with (None, some_port)
51+
) as tunnel:
52+
53+
# Connect to MongoDB via the forwarded local port
54+
client = MongoClient(
55+
f'mongodb://{mongo_username}:{mongo_password}@{'127.0.0.1'}:{tunnel.local_bind_port}'
56+
)
57+
58+
db = client[database]
59+
for doc in db[collection].find(query):
60+
id = str(doc['_id'])
61+
if id in result.keys():
62+
raise RuntimeError(f"uid {id} defined multiple times - Check collection {collection} in database {database} sanity.")
63+
64+
del doc['_id']
65+
if 'subdevices' in doc.keys() and type(doc['subdevices']) == list:
66+
doc['subdevices'] = {i['uid']:i for i in doc['subdevices']}
67+
68+
result[id]=doc
69+
70+
return AttrsDict(dict(sorted(result.items())))
71+
72+
if __name__ == "__main__":
73+
arguments = sys.argv[1:]
74+
hemisphere = arguments[0] #'p-1-1-om-hs-31'
75+
db_devices =get_data(query={'uid':{"$regex": 'p-1-1-*'} })
76+
db_measurements = get_data(collection='Measurements_Pmt', query={'measurement_location':'TUM', 'measurement_type': 'Nominal voltage'})
77+
78+
pmts = db_devices.map('uid')[hemisphere].subdevices.map('device_type',unique=False)['pmt-unit'].map('uid')
79+
settings = AttrsDict()
80+
for k in pmts.keys():
81+
val = db_measurements.group('devices_used.uid')[k][0].result
82+
unc = db_measurements.group('devices_used.uid')[k][0].result_unc
83+
unit = db_measurements.group('devices_used.uid')[k][0].units
84+
85+
settings[k] = {"position":int(pmts[k].position),"voltage":val}
86+
print(settings)

0 commit comments

Comments
 (0)