forked from NSLS-II/lsdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_lib.py
executable file
·814 lines (602 loc) · 23.2 KB
/
db_lib.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import os
import time
import six
import uuid
import amostra.client.commands as acc
import conftrak.client.commands as ccc
from analysisstore.client.commands import AnalysisClient
import conftrak.exceptions
import logging
logger = logging.getLogger(__name__)
#12/19 - Skinner inherited this from Hugo, who inherited it from Matt. Arman wrote the underlying DB and left BNL in 2018.
# TODO: get the beamline_id from parameter
BEAMLINE_ID = '17ID1'
sample_ref = None
container_ref = None
request_ref = None
configuration_ref = None
mds_ref = None
analysis_ref = None
main_server = os.environ['MONGODB_HOST']
services_config = {
'amostra': {'host': main_server, 'port': '7770'},
'conftrak': {'host': main_server, 'port': '7771'},
'metadataservice': {'host': main_server, 'port': '7772'},
'analysisstore': {'host': main_server, 'port': '7773'}
}
def db_connect(params=services_config):
global sample_ref,container_ref,request_ref,configuration_ref,analysis_ref
"""
recommended idiom:
"""
sample_ref = acc.SampleReference(**params['amostra'])
container_ref = acc.ContainerReference(**params['amostra'])
request_ref = acc.RequestReference(**params['amostra'])
configuration_ref = ccc.ConfigurationReference(**services_config['conftrak'])
analysis_ref = AnalysisClient(services_config['analysisstore'])
logger.info(analysis_ref)
# should be in config :(
primaryDewarName = 'primaryDewarJohn'
#connect on import
db_connect()
def setCurrentUser(beamline,userName): #for now username, although these should be unique
setBeamlineConfigParam(beamline,"user",userName)
def getCurrentUser(beamline): #for now username, although these should be unique
return getBeamlineConfigParam(beamline,"user")
def setPrimaryDewarName(dewarName):
global primaryDewarName
primaryDewarName = dewarName
def searchBeamline(**kwargs):
try:
return list(configuration_ref.find(key="beamline", **kwargs))
except StopIteration:
return None
def getBeamlineByNumber(num):
"""eg. 17id1, 17id2, 16id1"""
try:
return list(configuration_ref.find(key="beamline", number=num))
except StopIteration:
return None
def createContainer(name, capacity, owner, kind, **kwargs): #16_pin_puck, automounterDewar, shippingDewar
"""
container_name: string, name for the new container, required
kwargs: passed to constructor
"""
if capacity is not None:
kwargs['content'] = [""]*capacity
uid = container_ref.create(name=name, owner=owner, kind=kind, **kwargs)
return uid
def updateContainer(cont_info): #really updating the contents
cont = cont_info['uid']
q = {'uid': cont_info.pop('uid', '')}
cont_info.pop('time', '')
container_ref.update(q, {'content':cont_info['content']})
return cont
def createSample(sample_name, owner, kind, proposalID=None, **kwargs):
"""
sample_name: string, name for the new sample, required
kwargs: passed to constructor
"""
# initialize request count to zero
if 'request_count' not in kwargs:
kwargs['request_count'] = 0
uid = sample_ref.create(name=sample_name, owner=owner,kind=kind,proposalID=proposalID,**kwargs)
return uid
def incrementSampleRequestCount(sample_id):
"""
increment the 'request_count' attribute of the specified sample by 1
"""
# potential for race here?
#skinner - I don't understand this line sample_ref.update(query={'uid': sample_id}, update={'$inc': {'request_count': 1}})
reqCount = getSampleRequestCount(sample_id)+1
sample_ref.update({'uid': sample_id},{'request_count':reqCount})
return getSampleRequestCount(sample_id)
def getSampleRequestCount(sample_id):
"""
get the 'request_count' attribute of the specified sample
"""
s = getSampleByID(sample_id)
return s['request_count']
def getRequestsBySampleID(sample_id, active_only=True):
"""
return a list of request dictionaries for the given sample_id
"""
params = {'sample': sample_id}
if active_only:
params['state'] = "active"
reqs = list(request_ref.find(**params))
return reqs
def getSampleByID(sample_id):
"""
sample_id: required, integer
"""
s = list(sample_ref.find(uid=sample_id))
if (s):
return s[0]
else:
return {}
def getSampleNamebyID(sample_id):
"""
sample_id: required, integer
"""
s = getSampleByID(sample_id)
if (s==None):
return ''
else:
return s['name']
def getSamplesbyOwner(owner): #skinner
s = sample_ref.find(owner=owner)
return [samp['uid'] for samp in s]
def getSampleIDbyName(sampleName,owner):
"""
sample_id: required, integer
"""
samples = list(sample_ref.find(owner=owner,name=sampleName))
if (samples != []):
return samples[0]["uid"]
else:
return ""
def getContainerIDbyName(container_name,owner):
containers = list(container_ref.find(owner=owner,name=container_name))
if (containers != []):
return containers[0]["uid"]
else:
return ""
def getContainerNameByID(container_id):
"""
container_id: required, integer
"""
c = list(container_ref.find(uid=container_id))
return c[0]['name']
def createResult(result_type, owner,request_id=None, sample_id=None, result_obj=None, proposalID=None,
**kwargs):
"""
result_type: string
request_id: int
sample_id: int
result_obj: dict to attach
"""
header = analysis_ref.insert_analysis_header(result_type=result_type,owner=owner, uid=str(uuid.uuid4()),
sample=sample_id, request=request_id,
provenance={'lsdc':1}, result_obj=result_obj,proposalID=proposalID,time=time.time(),**kwargs)
logger.info("uuid of result inserted into analysisstore: %s" % header)
return header
def getResult(result_id):
"""
result_id: required, int
"""
header = list(analysis_ref.find_analysis_header(uid=result_id))
return header[0]
def getResultsforRequest(request_id):
"""
Takes an integer request_id and returns a list of matching results or [].
"""
resultGen = analysis_ref.find_analysis_header(request=request_id)
if (resultGen != None):
headers = list(resultGen)
return headers
else:
return []
def getResultsforSample(sample_id):
"""
Takes a sample_id and returns it's resultsList or [].
"""
headers = list(analysis_ref.find_analysis_header(sample=sample_id))
return headers
def getRequestByID(request_id, active_only=True):
"""
return a list of request dictionaries for the given request_id
"""
params = {'uid': request_id}
if active_only:
params['state'] = "active"
req = list(request_ref.find(**params))[0]
return req
def addResultforRequest(result_type, request_id, owner,result_obj=None, **kwargs):
"""
like createResult, but also adds it to the resultList of result['sample_id']
"""
sample = getRequestByID(request_id)['sample']
r = createResult(owner=owner,result_type=result_type, request_id=request_id, sample_id=sample, result_obj=result_obj, **kwargs)
return r
def addResulttoSample(result_type, sample_id, owner,result_obj=None, as_mongo_obj=False, proposalID=None,**kwargs):
"""
like addResulttoRequest, but without a request
"""
r = createResult(owner=owner,result_type=result_type, request_id=None, sample_id=sample_id, result_obj=result_obj, proposalID=proposalID,**kwargs)
return r
def addResulttoBL(result_type, beamline_id, owner,result_obj=None, proposalID=None,**kwargs):
"""
add result to beamline
beamline_id: the integer, 'beamline_id' field of the beamline entry
other fields are as for createRequest
"""
r = createResult(owner=owner,result_type=result_type, request_id=None, sample_id=None, result_obj=result_obj, beamline_id=beamline_id, proposalID=proposalID,**kwargs)
return r
def getResultsforBL(id=None, name=None, number=None):
"""
Retrieve results using either BL id, name, or number (tried in that order)
Returns a generator of results
"""
if id is None:
if name is None:
key = 'number'
val = number
else:
key = 'name'
val = name
query = {key: val}
b = searchBeamline(**query)
if b is None:
yield None
raise StopIteration
id = b['uid']
if id is None:
yield None
raise StopIteration
results = list(analysis_ref.find_analysis_header(beamline_id=id))
for r in results:
yield r
def addFile(data=None, filename=None):
"""
Put the file data into the GenericFile collection,
return the _id for use as an id or ReferenceField.
If a filename kwarg is given, read data from the file.
If a data kwarg is given or data is the 1st arg, store the data.
If both or neither is given, raise an error.
"""
#TODO: Decide what to do with this method
raise NotImplemented
'''
if filename is not None:
if data is not None:
raise ValueError('both filename and data kwargs given. can only use one.')
else:
with open(filename, 'r') as file: # do we need 'b' for binary?
data = file.read() # is this blocking? might not always get everything at once?!
elif data is None:
raise ValueError('neither filename or data kwargs given. need one.')
f = GenericFile(data=data)
f.save()
f.reload() # to fetch generated id
return f.to_dbref()
'''
def getFile(_id):
"""
Retrieve the data from the GenericFile collection
for the given _id or db_ref
Returns the data in Binary. If you know it's a txt file and want a string,
convert with str()
Maybe this will be automatically deref'd most of the time?
Only if they're mongoengine ReferenceFields...
"""
#TODO: Decide what to do with this method
raise NotImplemented
'''
try:
_id = _id.id
except AttributeError:
pass
f = GenericFile.objects(__raw__={'_id': _id}) # yes it's '_id' here but just 'id' below, gofigure
return _try0_dict_key(f, 'file', 'id', _id, None,
dict_key='data')
'''
def createRequest(request_type, owner, request_obj=None, as_mongo_obj=False, proposalID=None, **kwargs):
"""
request_type: required, name (string) of request type, dbref to it's db entry, or a Type object
request_obj: optional, stored as is, could be a dict of collection parameters, or whatever
priority: optional, integer priority level
anything else (priority, sample_id) can either be embedded in the
request_object or passed in as keyword args to get saved at the
top level.
"""
kwargs['request_type'] = request_type
kwargs['request_obj'] = request_obj
kwargs['owner'] = owner
kwargs['proposalID']=proposalID
uid = request_ref.create(**kwargs)
return uid
def addRequesttoSample(sample_id, request_type, owner,request_obj=None, as_mongo_obj=False, proposalID=None,**kwargs):
"""
sample_id: required, integer sample id
request_type: required, name (string) of request type, dbref to it's db entry, or a Type object
request_obj: optional, stored as is, could be a dict of collection parameters, or whatever
anything else (priority, sample_id) can either be embedded in the
request_object or passed in as keyword args to get saved at the
top level.
"""
kwargs['sample'] = sample_id
s = time.time()
r = createRequest(request_type, owner, request_obj=request_obj, as_mongo_obj=True, proposalID=proposalID,**kwargs)
t = time.time()-s
logger.info("add req = " + str(t))
return r
def insertIntoContainer(container_name, owner, position, itemID):
c = getContainerByName(container_name,owner)
if c is not None:
cnt = c['content']
cnt[position - 1] = itemID # most people don't zero index things
c['content'] = cnt
updateContainer(c)
return True
else:
logger.error("bad container name %s" % container_name)
return False
def emptyContainer(uid):
c = getContainerByID(uid)
if c is not None:
cnt = c['content']
for i in range (len(cnt)):
cnt[i] = ''
c['content'] = cnt
updateContainer(c)
return True
else:
logger.error("container not found")
return False
def getContainers(filters=None):
"""get *all* containers"""
if filters is not None:
c = list(container_ref.find(**filters)) #skinner - seems to break on compound filter
else:
c = list(container_ref.find())
return c
def getContainersByType(type_name, owner):
#TODO: group_name was not being used kept for compatibility
return getContainers(filters={"kind": type_name,"owner":owner})
def getAllPucks(owner): #shouldn't this be for owner?
# find all the types desended from 'puck'?
# and then we could do this?
return getContainersByType("16_pin_puck", owner)
def getPrimaryDewar(beamline):
"""
returns the mongo object for a container with a name matching
the global variable 'primaryDewarName'
"""
return getContainerByName(primaryDewarName,beamline)
def getContainerByName(container_name,owner):
c = getContainers(filters={'name': container_name,'owner':owner})[0] #skinner, this should return only one, not a list
return c
def getContainerByID(container_id):
c = getContainers(filters={'uid': container_id})[0]
return c
def getQueue(beamlineName):
"""
returns a list of request dicts for all the samples in the container
named by the global variable 'primaryDewarName'
"""
# seems like this would be alot simpler if it weren't for the Nones?
ret_list = []
# try to only retrieve what we need...
# Use .first() instead of [0] here because when the query returns nothing,
# .first() returns None while [0] generates an IndexError
# Nah... [0] is faster and catch Exception...
DewarItems = []
try:
DewarItems = getPrimaryDewar(beamlineName)['content']
except IndexError as AttributeError:
raise ValueError('could not find container: "{0}"!'.format(primaryDewarName))
items = []
for item in DewarItems:
if (item != ""):
items.append(item)
sample_list = []
contents = [getContainerByID(uid)['content'] for uid in items]
for samp in contents:
if (samp != ""):
sample_list += samp
for s in sample_list:
reqs = getRequestsBySampleID(s, active_only=True)
for request in reqs:
yield request
def getQueueUnorderedObsolete(beamlineName):
"""
returns a list of request dicts for all the samples in the container
named by the global variable 'primaryDewarName'
"""
# seems like this would be alot simpler if it weren't for the Nones?
ret_list = []
# try to only retrieve what we need...
# Use .first() instead of [0] here because when the query returns nothing,
# .first() returns None while [0] generates an IndexError
# Nah... [0] is faster and catch Exception...
try:
items = getPrimaryDewar(beamlineName)['content']
except IndexError as AttributeError:
raise ValueError('could not find container: "{0}"!'.format(primaryDewarName))
items = set(items)
items.discard("") # skip empty positions
sample_list = []
contents = [getContainerByID(uid)['content'] for uid in items]
for samp in contents:
sil = set(samp)
sil.discard("")
sample_list += sil
for s in sample_list:
reqs = getRequestsBySampleID(s, active_only=True)
for request in reqs:
yield request
def queueDone(beamlineName):
ql = list(getQueue(beamlineName))
for i in range (0,len(ql)):
if (ql[i]['priority'] > 0):
return 0
return 1
def getCoordsfromSampleID(beamline,sample_id):
"""
returns the container position within the dewar and position in
that container for a sample with the given id in one of the
containers in the container named by the global variable
'primaryDewarName'
"""
try:
primary_dewar_item_list = getPrimaryDewar(beamline)['content']
except IndexError as AttributeError:
raise ValueError('could not find container: "{0}"!'.format(primaryDewarName))
#john try:
# eliminate empty item_list slots
pdil_set = set(primary_dewar_item_list)
pdil_ssample_id = pdil_set.discard("")
# find container in the primary_dewar_item_list (pdil) which has the sample
filters = {'$and': [{'uid': {'$in':list(pdil_set)}}, {'content': {'$in':[sample_id]}}]}
c = getContainers(filters=filters)
# get the index of the found container in the primary dewar
i = primary_dewar_item_list.index(c[0]['uid'])
# get the index of the sample in the found container item_list
j = c[0]['content'].index(sample_id)
# get the container_id of the found container
puck_id = c[0]['uid']
return (i, j, puck_id)
def popNextRequest(beamlineName):
"""
this just gives you the next one, it doesn't
actually pop it off the stack
"""
orderedRequests = getOrderedRequestList(beamlineName)
try:
if (orderedRequests[0]["priority"] != 99999):
if orderedRequests[0]["priority"] > 0:
return orderedRequests[0]
else: #99999 priority means it's running, try next
if orderedRequests[1]["priority"] > 0:
return orderedRequests[1]
except IndexError:
pass
return {}
def getRequestObsolete(reqID): # need to get this from searching the dewar I guess
#skinner - no idea reqID = int(reqID)
"""
request_id: required, integer id
"""
r = getRequestByID(reqID)
return r
def updateRequest(request_dict):
"""
This is not recommended once results are recorded for a request!
Using a new request instead would keep the apparent history
complete and intuitive. Although it won't hurt anything if you've
also recorded the request params used inside the results and query
against that, making requests basically ephemerally useful objects.
"""
if 'uid' in request_dict:
r_uid = request_dict.pop('uid', '')
s_time = request_dict.pop('time', '')
r = request_ref.update({'uid':r_uid},request_dict)
request_dict["uid"] = r_uid
request_dict["time"] = s_time
def deleteRequest(r_id):
"""
reqObj should be a dictionary with a 'uid' field
and optionally a 'sample_uid' field.
"""
r = getRequestByID(r_id)
r['state'] = "inactive"
updateRequest(r)
def updateSample(sampleObj):
if 'uid' in sampleObj:
s_uid = sampleObj.pop('uid','')
s_time = sampleObj.pop('time','')
s = sample_ref.update({'uid': s_uid}, sampleObj)
def deleteSample(sample_uid):
s = getSampleByID(sample_uid)
s['state'] = "active"
updateSample(s)
def removePuckFromDewar(beamline,dewarPos):
dewar = getPrimaryDewar(beamline)
dewar['content'][dewarPos] = ''
updateContainer(dewar)
def updatePriority(request_id, priority):
r = getRequestByID(request_id)
r['priority'] = priority
updateRequest(r)
def getPriorityMap(beamlineName):
"""
returns a dictionary with priorities as keys and lists of requests
having those priorities as values
"""
priority_map = {}
for request in getQueue(beamlineName):
try:
priority_map[request['priority']].append(request)
except KeyError:
priority_map[request['priority']] = [request]
return priority_map
def getOrderedRequestList(beamlineName):
"""
returns a list of requests sorted by priority
"""
orderedRequestsList = []
priority_map = getPriorityMap(beamlineName)
for priority in sorted(six.iterkeys(priority_map), reverse=True):
orderedRequestsList += priority_map[priority]
#for request in priority_map[priority]:
# yield request
# or if we want this to be a generator could it be more efficient
# with itertools.chain?
# foo=['abc','def','ghi']
# [a for a in itertools.chain(*foo)]
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
# or [a for a in itertools.chain.from_iterable(foo)]
return orderedRequestsList
def createBeamline(bl_name, bl_num): #createBeamline("fmx", "17id1")
data = {"key": "beamline", "name": bl_name, "number": bl_num}
uid = configuration_ref.create(beamline_id=bl_num, **data)
return uid
def beamlineInfo(beamline_id, info_name, info_dict=None):
"""
to write info: beamlineInfo('x25', 'det', info_dict={'vendor':'adsc','model':'q315r'})
to fetch info: info = beamlineInfo('x25', 'det')
"""
# if it exists it's a query or update
try:
bli = list(configuration_ref.find(key='beamline_info', beamline_id=beamline_id, info_name=info_name))[0] #hugo put the [0]
if info_dict is None: # this is a query
return bli['info']
# else it's an update
bli_uid = bli.pop('uid', '')
configuration_ref.update({'uid': bli_uid},{'info':info_dict})
# else it's a create
except conftrak.exceptions.ConfTrakNotFoundException:
# edge case for 1st create in fresh database
# in which case this as actually a query
if info_dict is None:
return {}
# normal create
data = {'key': 'beamline_info', 'info_name':info_name, 'info': info_dict}
uid = configuration_ref.create(beamline_id,**data)
def setBeamlineConfigParams(paramDict, searchParams):
# get current config
beamlineConfig = beamlineInfo(**searchParams)
# update with given param dict and last_modified
paramDict['last_modified'] = time.time()
beamlineConfig.update(paramDict)
# save
beamlineInfo(info_dict=beamlineConfig, **searchParams)
def setBeamlineConfigParam(beamline_id, paramName, paramVal):
beamlineInfo(beamline_id,paramName,{"val":paramVal})
def getBeamlineConfigParam(beamline_id, paramName):
return beamlineInfo(beamline_id,paramName)["val"]
def getAllBeamlineConfigParams(beamline_id):
g = configuration_ref.find(key='beamline_info', beamline_id=beamline_id)
configList = list(g)
return configList
def logAllBeamlineConfigParams(beamline_id):
printAllBeamlineConfigParams(beamline_id, log=True)
def printAllBeamlineConfigParams(beamline_id, log=False):
configList = getAllBeamlineConfigParams(beamline_id)
for i in range (0,len(configList)):
try:
if log:
logger.info(configList[i]['info_name'] + " " + str(configList[i]['info']['val']))
else:
print(configList[i]['info_name'] + " " + str(configList[i]['info']['val']))
except KeyError:
pass
def deleteCompletedRequestsforSample(sid):
return #short circuit, not what they wanted
logger.info("delete request " + sid)
requestList=getRequestsBySampleID(sid)
for i in range (0,len(requestList)):
if (requestList[i]["priority"] == -1): #good to clean up completed requests after unmount
if (requestList[i]["protocol"] == "raster" or requestList[i]["protocol"] == "vector"):
deleteRequest(requestList[i]['uid'])