def read_distribution(dbs, confs): description = [('Replicate', 'string'), ('Lane', 'string'), ('Start', 'number'), ('Transcript Length', 'number'), ('Read Coverage', 'number'), ] chart = {} chart['table_description'] = description stats = [] for conf in confs['configurations']: data = run_method_using_mysqldb(_read_distribution, dbs, conf, http.not_found) if data == http.not_found: print "Can't collect because of missing data." else: lines = [] for line in data: lines.append((conf['replicateid'], line[0], line[1], line[2], line[3])) stats.extend(lines) if stats: chart['table_data'] = stats else: chart['table_data'] = [['', '', 0, 0, 0]] return chart
def read_distribution(dbs, confs): description = [ ('Replicate', 'string'), ('Lane', 'string'), ('Start', 'number'), ('Transcript Length', 'number'), ('Read Coverage', 'number'), ] chart = {} chart['table_description'] = description stats = [] for conf in confs['configurations']: data = run_method_using_mysqldb(_read_distribution, dbs, conf, http.not_found) if data == http.not_found: print "Can't collect because of missing data." else: lines = [] for line in data: lines.append( (conf['replicateid'], line[0], line[1], line[2], line[3])) stats.extend(lines) if stats: chart['table_data'] = stats else: chart['table_data'] = [['', '', 0, 0, 0]] return chart
def test_running_method_using_mysqldb_with_operationalerror(self): def dummy_method(dbs, confs): raise OperationalError method = dummy_method dbs = {} confs = {} marker = None result = run_method_using_mysqldb(method, dbs, confs, marker) self.failUnless(result == marker)
def test_running_method_using_mysqldb_with_programmingerror(self): def dummy_method(dbs, confs): raise ProgrammingError method = dummy_method dbs = {} confs = {} marker = None result = run_method_using_mysqldb(method, dbs, confs, marker) self.failUnless(result == marker)
def test_running_method_using_mysqldb(self): def dummy_method(dbs, confs): return "Some result data from the MySQL database" method = dummy_method dbs = {} confs = {} marker = None result = run_method_using_mysqldb(method, dbs, confs, marker) self.failUnless(result == "Some result data from the MySQL database")
def test_running_method_using_mysqldb_with_attributerror(self): def dummy_method(dbs, confs): raise AttributeError method = dummy_method dbs = {} confs = {} marker = None cla, exc, trbk = None, None, None result = run_method_using_mysqldb(method, dbs, confs, marker) self.failUnless(result == marker)
def collect(dbs, confs, method, strategy, **kwargs): """Collect results from multiple queries to the database using a strategy.""" results = [] for conf in confs: conf.update(kwargs) data = run_method_using_mysqldb(method, dbs, conf, http.not_found) if data == http.not_found: print "Can't collect because of missing data." else: for line in data: results.append(strategy(conf, line)) return results
def run(dbs, method, conf): """Run a method running sql code. Returns a tuple of (data, success). If the success value is True, the method could not be run properly. If the success value is False, the method has been executed correctly. """ success = True data = run_method_using_mysqldb(method, dbs, conf, http.not_found) if data == http.not_found: print "Error running sql method." success = False return data, success
def aggregate(dbs, confs, method, strategy, **kwargs): """Aggregate results from multiple queries to the database using a strategy.""" stats = None failed = 0 for conf in confs: conf.update(kwargs) data = run_method_using_mysqldb(method, dbs, conf, http.not_found) if data == http.not_found: print "Can't aggregate because of missing data." failed = failed + 1 else: if stats is None: stats = data else: stats = merge(stats, data, strategy=strategy) return stats, failed
def show(self, request): """Return the resource representation in the requested content type""" if not self.method: # The method needs to be set at least return http.not_found([('Content-type', 'text/javascript')], '') # Inject the project specific project databases self.dbs = request.environ['dbs'] # Get the configurations for the given level of detail confs = get_configurations(request, self.level, self.resolution, self.partition, self.dbs, **self.kwargs) # Run the method containing the code to access the database data = run_method_using_mysqldb(self.method, self.dbs, confs, http.not_found) if data == http.not_found: # If the returned value is the marker http.not_found, we know # that something related to MySQL went wrong when the method # was called. The marker is used so that no internals of the # MySQL adapter need to be considered here. return http.not_found() if data is None: log.warning("Method appears to be unimplemented: %s" % self.method) return http.not_found([('Content-type', 'text/javascript')], '') accept_header = request.headers.get('Accept', 'text/javascript') body = None # Different results are returned depending on whether this is a table if 'table_description' in data and 'table_data' in data: #print "Extract table info and return info" # This chart is using the google visualization library table = gviz_api.DataTable(data['table_description']) try: table.AppendData(data['table_data']) except DataTableException: print self.method raise if accept_header == 'text/plain': body = table.ToJSonResponse() elif accept_header == 'text/html': body = table.ToHtml() elif accept_header == 'text/x-cfg': body = to_cfg(data) elif accept_header == 'text/csv': body = table.ToCsv() elif accept_header == 'text/tab-separated-values': body = table.ToCsv(separator="\t") elif accept_header == 'text/x-python-pickled-dict': body = pickle.dumps(data) else: try: body = table.ToJSon() except DataTableException: print self.method print data['table_description'] print data['table_data'] raise except: raise else: accept_header = request.headers.get('Accept', None) if accept_header == 'text/x-python-pickled-dict': body = pickle.dumps(data) else: body = json.dumps(data) headers = [('Content-type', accept_header), ('Content-Length', len(body))] return http.ok(headers, body)