-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetchMongoRecords.py
50 lines (42 loc) · 1.88 KB
/
fetchMongoRecords.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Utilitarian script to delete documents in MongoDB
"""
from __future__ import print_function, division
import sys
import json
import pymongo
def main():
myClient = pymongo.MongoClient("mongodb://localhost:8230/")
myDB = myClient["msOutDB"]
collections = ["msOutRelValColl", "msOutNonRelValColl"]
summaryByContainer = {}
for coll in collections:
if coll == "msOutRelValColl":
print("RelVals don't run ACDC workflows, so move to the next collection")
continue
myCol = myDB[coll]
resp = myCol.find()
docs = list(resp)
print("Found {} documents in the DB collection: {}".format(len(docs), coll))
for doc in docs:
for outItem in doc['OutputMap']:
if outItem['TapeRuleID']:
summaryByContainer.setdefault(outItem['Dataset'], [])
summaryByContainer[outItem['Dataset']].append(dict(rse=outItem['TapeDestination'],
ruleId=outItem['TapeRuleID']))
print(" there are a total of {} containers with Tape rules".format(len(summaryByContainer)))
# now filter containers with more than one Tape rule
for contName in list(summaryByContainer):
if len(summaryByContainer[contName]) == 1:
print("Dropping single rule container: {}".format(contName))
summaryByContainer.pop(contName, None)
print(" where {} have multiple Tape rules (perhaps dup rules too).".format(len(summaryByContainer)))
fileName = "/data/user/{}_tapeRules.json".format(coll)
print("saving summary in file: {}\n".format(fileName))
with open(fileName, "w") as fObj:
json.dump(summaryByContainer, fObj, indent=2)
print("Done!")
if __name__ == '__main__':
sys.exit(main())