db.py (2343:a2b4a6ccee56) | db.py (2665:a124942bacb8) |
---|---|
1# Copyright (c) 2003-2004 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 9 unchanged lines hidden (view full) --- 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 1# Copyright (c) 2003-2004 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 9 unchanged lines hidden (view full) --- 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26# 27# Authors: Nathan Binkert |
|
26 27import MySQLdb, re, string 28 29def statcmp(a, b): 30 v1 = a.split('.') 31 v2 = b.split('.') 32 33 last = min(len(v1), len(v2)) - 1 --- 96 unchanged lines hidden (view full) --- 130 self.allStatNames = {} 131 132 self.allSubData = {} 133 134 self.allRuns = [] 135 self.allRunIds = {} 136 self.allRunNames = {} 137 | 28 29import MySQLdb, re, string 30 31def statcmp(a, b): 32 v1 = a.split('.') 33 v2 = b.split('.') 34 35 last = min(len(v1), len(v2)) - 1 --- 96 unchanged lines hidden (view full) --- 132 self.allStatNames = {} 133 134 self.allSubData = {} 135 136 self.allRuns = [] 137 self.allRunIds = {} 138 self.allRunNames = {} 139 |
140 self.allBins = [] 141 self.allBinIds = {} 142 self.allBinNames = {} 143 |
|
138 self.allFormulas = {} 139 140 self.stattop = {} 141 self.statdict = {} 142 self.statlist = [] 143 144 self.mode = 'sum'; 145 self.runs = None | 144 self.allFormulas = {} 145 146 self.stattop = {} 147 self.statdict = {} 148 self.statlist = [] 149 150 self.mode = 'sum'; 151 self.runs = None |
152 self.bins = None |
|
146 self.ticks = None 147 self.method = 'sum' 148 self._method = type(self).sum 149 150 def get(self, job, stat, system=None): 151 run = self.allRunNames.get(str(job), None) 152 if run is None: 153 return None --- 54 unchanged lines hidden (view full) --- 208 self.query('''select rn_id,rn_name,rn_sample,rn_user,rn_project 209 from runs''') 210 for result in self.cursor.fetchall(): 211 run = RunData(result); 212 self.allRuns.append(run) 213 self.allRunIds[run.run] = run 214 self.allRunNames[run.name] = run 215 | 153 self.ticks = None 154 self.method = 'sum' 155 self._method = type(self).sum 156 157 def get(self, job, stat, system=None): 158 run = self.allRunNames.get(str(job), None) 159 if run is None: 160 return None --- 54 unchanged lines hidden (view full) --- 215 self.query('''select rn_id,rn_name,rn_sample,rn_user,rn_project 216 from runs''') 217 for result in self.cursor.fetchall(): 218 run = RunData(result); 219 self.allRuns.append(run) 220 self.allRunIds[run.run] = run 221 self.allRunNames[run.name] = run 222 |
223 self.query('select * from bins') 224 for id,name in self.cursor.fetchall(): 225 self.allBinIds[int(id)] = name 226 self.allBinNames[name] = int(id) 227 |
|
216 self.query('select sd_stat,sd_x,sd_y,sd_name,sd_descr from subdata') 217 for result in self.cursor.fetchall(): 218 subdata = SubData(result) 219 if self.allSubData.has_key(subdata.stat): 220 self.allSubData[subdata.stat].append(subdata) 221 else: 222 self.allSubData[subdata.stat] = [ subdata ] 223 --- 6 unchanged lines hidden (view full) --- 230 import info 231 for result in self.cursor.fetchall(): 232 stat = info.NewStat(self, StatData(result)) 233 self.append(stat) 234 self.allStats.append(stat) 235 self.allStatIds[stat.stat] = stat 236 self.allStatNames[stat.name] = stat 237 | 228 self.query('select sd_stat,sd_x,sd_y,sd_name,sd_descr from subdata') 229 for result in self.cursor.fetchall(): 230 subdata = SubData(result) 231 if self.allSubData.has_key(subdata.stat): 232 self.allSubData[subdata.stat].append(subdata) 233 else: 234 self.allSubData[subdata.stat] = [ subdata ] 235 --- 6 unchanged lines hidden (view full) --- 242 import info 243 for result in self.cursor.fetchall(): 244 stat = info.NewStat(self, StatData(result)) 245 self.append(stat) 246 self.allStats.append(stat) 247 self.allStatIds[stat.stat] = stat 248 self.allStatNames[stat.name] = stat 249 |
250 # Name: listbins 251 # Desc: Prints all bins matching regex argument, if no argument 252 # is given all bins are returned 253 def listBins(self, regex='.*'): 254 print '%-50s %-10s' % ('bin name', 'id') 255 print '-' * 61 256 names = self.allBinNames.keys() 257 names.sort() 258 for name in names: 259 id = self.allBinNames[name] 260 print '%-50s %-10d' % (name, id) 261 |
|
238 # Name: listruns 239 # Desc: Prints all runs matching a given user, if no argument 240 # is given all runs are returned 241 def listRuns(self, user=None): 242 print '%-40s %-10s %-5s' % ('run name', 'user', 'id') 243 print '-' * 62 244 for run in self.allRuns: 245 if user == None or user == run.user: --- 87 unchanged lines hidden (view full) --- 333 334 if type(stat) is str: 335 rx = re.compile(stat) 336 for stat in self.allStats: 337 if rx.match(stat.name): 338 ret.append(stat) 339 return ret 340 | 262 # Name: listruns 263 # Desc: Prints all runs matching a given user, if no argument 264 # is given all runs are returned 265 def listRuns(self, user=None): 266 print '%-40s %-10s %-5s' % ('run name', 'user', 'id') 267 print '-' * 62 268 for run in self.allRuns: 269 if user == None or user == run.user: --- 87 unchanged lines hidden (view full) --- 357 358 if type(stat) is str: 359 rx = re.compile(stat) 360 for stat in self.allStats: 361 if rx.match(stat.name): 362 ret.append(stat) 363 return ret 364 |
365 def getBin(self, bins): 366 if type(bins) is not list: 367 bins = [ bins ] 368 369 ret = [] 370 for bin in bins: 371 if type(bin) is int: 372 ret.append(bin) 373 elif type(bin) is str: 374 ret.append(self.allBinNames[bin]) 375 else: 376 for name,id in self.allBinNames.items(): 377 if bin.match(name): 378 ret.append(id) 379 380 return ret 381 382 def getNotBin(self, bin): 383 map = {} 384 for bin in getBin(bin): 385 map[bin] = 1 386 387 ret = [] 388 for bin in self.allBinIds.keys(): 389 if not map.has_key(bin): 390 ret.append(bin) 391 392 return ret 393 |
|
341 ######################################### 342 # get the data 343 # | 394 ######################################### 395 # get the data 396 # |
344 def query(self, op, stat, ticks, group=False): | 397 def inner(self, op, stat, bins, ticks, group=False): |
345 sql = 'select ' 346 sql += 'dt_stat as stat, ' 347 sql += 'dt_run as run, ' 348 sql += 'dt_x as x, ' 349 sql += 'dt_y as y, ' 350 if group: 351 sql += 'dt_tick as tick, ' 352 sql += '%s(dt_data) as data ' % op --- 5 unchanged lines hidden (view full) --- 358 sql += ' (%s)' % val 359 else: 360 sql += ' dt_stat=%d' % stat.stat 361 362 if self.runs != None and len(self.runs): 363 val = ' or '.join([ 'dt_run=%d' % r for r in self.runs ]) 364 sql += ' and (%s)' % val 365 | 398 sql = 'select ' 399 sql += 'dt_stat as stat, ' 400 sql += 'dt_run as run, ' 401 sql += 'dt_x as x, ' 402 sql += 'dt_y as y, ' 403 if group: 404 sql += 'dt_tick as tick, ' 405 sql += '%s(dt_data) as data ' % op --- 5 unchanged lines hidden (view full) --- 411 sql += ' (%s)' % val 412 else: 413 sql += ' dt_stat=%d' % stat.stat 414 415 if self.runs != None and len(self.runs): 416 val = ' or '.join([ 'dt_run=%d' % r for r in self.runs ]) 417 sql += ' and (%s)' % val 418 |
419 if bins != None and len(bins): 420 val = ' or '.join([ 'dt_bin=%d' % b for b in bins ]) 421 sql += ' and (%s)' % val 422 |
|
366 if ticks != None and len(ticks): 367 val = ' or '.join([ 'dt_tick=%d' % s for s in ticks ]) 368 sql += ' and (%s)' % val 369 370 sql += ' group by dt_stat,dt_run,dt_x,dt_y' 371 if group: 372 sql += ',dt_tick' 373 return sql 374 | 423 if ticks != None and len(ticks): 424 val = ' or '.join([ 'dt_tick=%d' % s for s in ticks ]) 425 sql += ' and (%s)' % val 426 427 sql += ' group by dt_stat,dt_run,dt_x,dt_y' 428 if group: 429 sql += ',dt_tick' 430 return sql 431 |
432 def outer(self, op_out, op_in, stat, bins, ticks): 433 sql = self.inner(op_in, stat, bins, ticks, True) 434 sql = 'select stat,run,x,y,%s(data) from (%s) as tb ' % (op_out, sql) 435 sql += 'group by stat,run,x,y' 436 return sql 437 |
|
375 # Name: sum | 438 # Name: sum |
376 # Desc: given a run, a stat and an array of samples, total the samples 377 def sum(self, *args, **kwargs): 378 return self.query('sum', *args, **kwargs) | 439 # Desc: given a run, a stat and an array of samples and bins, 440 # sum all the bins and then get the standard deviation of the 441 # samples for non-binned runs. This will just return the average 442 # of samples, however a bin array still must be passed 443 def sum(self, stat, bins, ticks): 444 return self.inner('sum', stat, bins, ticks) |
379 380 # Name: avg | 445 446 # Name: avg |
381 # Desc: given a run, a stat and an array of samples, average the samples 382 def avg(self, stat, ticks): 383 return self.query('avg', *args, **kwargs) | 447 # Desc: given a run, a stat and an array of samples and bins, 448 # sum all the bins and then average the samples for non-binned 449 # runs this will just return the average of samples, however 450 # a bin array still must be passed 451 def avg(self, stat, bins, ticks): 452 return self.outer('avg', 'sum', stat, bins, ticks) |
384 385 # Name: stdev | 453 454 # Name: stdev |
386 # Desc: given a run, a stat and an array of samples, get the standard 387 # deviation 388 def stdev(self, stat, ticks): 389 return self.query('stddev', *args, **kwargs) | 455 # Desc: given a run, a stat and an array of samples and bins, 456 # sum all the bins and then get the standard deviation of the 457 # samples for non-binned runs. This will just return the average 458 # of samples, however a bin array still must be passed 459 def stdev(self, stat, bins, ticks): 460 return self.outer('stddev', 'sum', stat, bins, ticks) |
390 391 def __setattr__(self, attr, value): 392 super(Database, self).__setattr__(attr, value) 393 if attr != 'method': 394 return 395 396 if value == 'sum': 397 self._method = self.sum 398 elif value == 'avg': 399 self._method = self.avg 400 elif value == 'stdev': 401 self._method = self.stdev 402 else: 403 raise AttributeError, "can only set get to: sum | avg | stdev" 404 | 461 462 def __setattr__(self, attr, value): 463 super(Database, self).__setattr__(attr, value) 464 if attr != 'method': 465 return 466 467 if value == 'sum': 468 self._method = self.sum 469 elif value == 'avg': 470 self._method = self.avg 471 elif value == 'stdev': 472 self._method = self.stdev 473 else: 474 raise AttributeError, "can only set get to: sum | avg | stdev" 475 |
405 def data(self, stat, ticks=None): | 476 def data(self, stat, bins=None, ticks=None): 477 if bins is None: 478 bins = self.bins |
406 if ticks is None: 407 ticks = self.ticks | 479 if ticks is None: 480 ticks = self.ticks |
408 sql = self._method(self, stat, ticks) | 481 sql = self._method(self, stat, bins, ticks) |
409 self.query(sql) 410 411 runs = {} 412 xmax = 0 413 ymax = 0 414 for x in self.cursor.fetchall(): 415 data = Data(x) 416 if not runs.has_key(data.run): --- 18 unchanged lines hidden --- | 482 self.query(sql) 483 484 runs = {} 485 xmax = 0 486 ymax = 0 487 for x in self.cursor.fetchall(): 488 data = Data(x) 489 if not runs.has_key(data.run): --- 18 unchanged lines hidden --- |