|
| 1 | +import pandas |
| 2 | +import json |
| 3 | +import copy |
| 4 | +import threading |
| 5 | +import time |
| 6 | +from collections import defaultdict |
| 7 | + |
| 8 | +def hostlist(dblist): |
| 9 | + #This could do something fancier, but for now we look by default only on localhost. |
| 10 | + return ["localhost"]*len(dblist) |
| 11 | + |
| 12 | +class childQuery(threading.Thread): |
| 13 | + def __init__(self,dictJSON,host): |
| 14 | + super(SummingThread, self).__init__() |
| 15 | + self.dict = json.dumps(dict) |
| 16 | + self.host = host |
| 17 | + |
| 18 | + def runQuery(self): |
| 19 | + #make a webquery, assign it to self.data |
| 20 | + url = self.host + "/cgi-bin/bookwormAPI?query=" + self.dict |
| 21 | + |
| 22 | + def parseResults(self): |
| 23 | + pass |
| 24 | + #return json.loads(self.data) |
| 25 | + |
| 26 | + def run(self): |
| 27 | + self.runQuery() |
| 28 | + |
| 29 | +def flatten(dictOfdicts): |
| 30 | + """ |
| 31 | + Recursive function: transforms a dict with nested entries like |
| 32 | + foo["a"]["b"]["c"] = 3 |
| 33 | + to one with tuple entries like |
| 34 | + fooPrime[("a","b","c")] = 3 |
| 35 | + """ |
| 36 | + output = [] |
| 37 | + for (key,value) in dictOfdicts.iteritems(): |
| 38 | + if isinstance(value,dict): |
| 39 | + output.append([(key),value]) |
| 40 | + else: |
| 41 | + children = flatten(value) |
| 42 | + for child in children: |
| 43 | + output.append([(key,) + child[0],child[1]]) |
| 44 | + return output |
| 45 | + |
| 46 | +def animate(dictOfTuples): |
| 47 | + """ |
| 48 | + opposite of flatten |
| 49 | + """ |
| 50 | + |
| 51 | + def tree(): |
| 52 | + return defaultdict(tree) |
| 53 | + |
| 54 | + output = defaultdict(tree) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +def combineDicts(master,new): |
| 59 | + """ |
| 60 | + instead of a dict of dicts of arbitrary depth, use a dict of tuples to store. |
| 61 | + """ |
| 62 | + |
| 63 | + for (keysequence, valuesequence) in flatten(new): |
| 64 | + try: |
| 65 | + master[keysequence] = map(sum,zip(master[keysequence],valuesequence)) |
| 66 | + except KeyError: |
| 67 | + master[keysequence] = valuesequence |
| 68 | + return dict1 |
| 69 | + |
| 70 | +class MetaQuery(object): |
| 71 | + def __init__(self,dictJSON): |
| 72 | + self.outside_outdictionary = json.dumps(dictJSON) |
| 73 | + |
| 74 | + def setDefaults(self): |
| 75 | + for specialKey in ["database","host"]: |
| 76 | + try: |
| 77 | + if isinstance(self.outside_dictionary[specialKey],basestring): |
| 78 | + #coerce strings to list: |
| 79 | + self.outside_dictionary[specialKey] = [self.outside_dictionary[specialKey]] |
| 80 | + except KeyError: |
| 81 | + #It's OK not to define host. |
| 82 | + if specialKey=="host": |
| 83 | + pass |
| 84 | + |
| 85 | + if 'host' not in self.outside_dictionary: |
| 86 | + #Build a hostlist: usually just localhost a bunch of times. |
| 87 | + self.outside_dictionary['host'] = hostlist(self.outside_dictionary['database']) |
| 88 | + |
| 89 | + for (target, dest) in [("database","host"),("host","database")]: |
| 90 | + #Expand out so you can search for the same database on multiple databases, or multiple databases on the same host. |
| 91 | + if len(self.outside_dictionary[target])==1 and len(self.outside_dictionary[dest]) != 1: |
| 92 | + self.outside_dictionary[target] = self.outside_dictionary[target] * len(self.outside_dictionary[dest]) |
| 93 | + |
| 94 | + |
| 95 | + def buildChildren(self): |
| 96 | + desiredCounts = [] |
| 97 | + for (host,dbname) in zip(self.outside_dictionary["host"],self.outside_dictionary["database"]): |
| 98 | + query = copy.deepcopy(self.outside_dictionary) |
| 99 | + del(query['host']) |
| 100 | + query['database'] = dbname |
| 101 | + |
| 102 | + desiredCounts.append(childQuery(query,host)) |
| 103 | + self.children = desiredCounts |
| 104 | + |
| 105 | + def runChildren(self): |
| 106 | + for child in self.children: |
| 107 | + child.start() |
| 108 | + |
| 109 | + def combineChildren(self): |
| 110 | + complete = dict() |
| 111 | + while (threading.enumerate()): |
| 112 | + for child in self.children: |
| 113 | + if not child.is_alive(): |
| 114 | + complete=combineDicts(complete,child.parseResult()) |
| 115 | + time.sleep(.05) |
| 116 | + |
| 117 | + def return_json(self): |
| 118 | + pass |
| 119 | + |
| 120 | + |
| 121 | + |
0 commit comments