def last_metrics(cls): last_24_hours = arrow.utcnow().replace(hours=-24).datetime objects = cls.select().where(cls.timestamp >= last_24_hours) count = objects.count() if count == 0: return last_1_hour = arrow.utcnow().replace(hours=-1).datetime accepted = cls.select(fn.Sum( cls.accepts)).where(cls.timestamp >= last_24_hours) rejected = cls.select(fn.Sum( cls.rejects)).where(cls.timestamp >= last_24_hours) delay = cls.select(fn.Avg(cls.delay)).where( cls.timestamp >= last_24_hours, cls.accepts >= 0, cls.delay >= 0) metrics = { 'count': count, 'accepted': accepted or 0, 'rejected': rejected or 0, 'delay': delay or 0.0, 'abandoned': objects.filter(cls.accepts == 0, cls.timestamp <= last_1_hour).count(), #'count_accepts': objects.filter(accepts__gte=1).count(), } metrics['requests'] = metrics['accepted'] + metrics['rejected'] return metrics
def report(): donations = Donation.select( Donation.donor, fn.Count(Donation.value).alias('count'), fn.Sum(Donation.value).alias('total'), fn.Avg(Donation.value).alias('average')).group_by(Donation.donor) return render_template('report.jinja2', donations=donations)
def get_high_and_average_scores(): high_score, average_score = (ScoreEntry .select(fn.Max(ScoreEntry.final_score), fn.Avg(ScoreEntry.final_score)) .scalar(as_tuple=True)) if (high_score, average_score) == (None, None): high_score, average_score = 0, 0 return high_score, average_score
def run(self): self.event.set() avg_length_type1 = DocumentModel.select( fn.Avg(fn.Length(DocumentModel.content))) avg_length_type2 = DocumentModel.select( fn.Avg(fn.Length(DocumentModel.title))) lst_args.setdefault("avg_length_type1", avg_length_type1) lst_args.setdefault("avg_length_type2", avg_length_type2) pool = Pool(processes=cpu_count(), initializer=start_process) pool.map_async(calculate, StemDocumentRelationModel.select()) pool.close() pool.join() self.event.clear()
def run(self): self.summary_msg = summary_template self.best_set_desc = best_set_desc avg_fg_bind, avg_bg_bind, nprimers = (Primer.select( fn.Avg(Primer.fg_freq), fn.Avg(Primer.bg_freq), fn.Count(Primer.seq)).scalar(as_tuple=True)) if (avg_fg_bind is None) or (avg_bg_bind is None): (avg_fg_bind, avg_bg_bind) = (0, 0) fg_bind_ratio = avg_fg_bind / float(self.fg_length) bg_bind_ratio = avg_bg_bind / float(self.bg_length) nactive = Primer.select().where(Primer.active == True).count() min_tm, max_tm, avg_tm = (Primer.select(fn.Min( Primer.tm), fn.Max(Primer.tm), fn.Avg( Primer.tm)).where(Primer.active == True).scalar(as_tuple=True)) nsets = Set.select(fn.Count(Set._id)).scalar() if nsets > 0: bs = Set.select().order_by(Set.score).limit(1).get() bs_primers = ", ".join(bs.primer_seqs()).strip() best_set = bs._id bs_size = bs.set_size bs_score = bs.score bs_stats = "- " + "\n - ".join( fmtkv(k, bs.__dict__['_data'][k]) for k in bs.exported_fields() if k not in ["_id", "pids", "score", "primers"]) self.best_set_desc = self.best_set_desc.format(**locals()) if_no_primers_msg = click.style( "Run `swga count` to find possible primers." if nprimers == 0 else "", fg='green') if_no_active_primers_msg = click.style( "Run `swga filter` to identify primers to use." if nactive == 0 else "", fg='green') melting_tmp_msg = ( "The melting temp of the primers ranges between {min_tm:.2f}C and " "{max_tm:.2f}C with an average of {avg_tm:.2f}C." if nactive > 0 and min_tm and max_tm else "No melting temps have been calculated yet.").format(**locals()) ifzero_sets_msg = click.style( "Run `swga find_sets` after identifying valid primers to begin " "collecting sets.\n", fg='green') set_msg = (self.best_set_desc if nsets > 0 else ifzero_sets_msg) primer_db = os.path.abspath(self.primer_db) nprimers = click.style(str(nprimers), bold=True, fg='blue') nactive = click.style(str(nactive), bold=True, fg='blue') nsets = click.style(str(nsets), bold=True, fg='blue') self.header = click.style("swga v{}".format(__version__), fg='green') # Copy all the relevant values into one dict values = self.__dict__.copy() values.update(locals()) # Format the summary message with all the calculated values self.summary_msg = self.summary_msg.format(**values) click.echo(quote(self.summary_msg, quote=" ", width=200))
def get_x_values(x, x_low, x_high, algorithm, version_constraint, with_ignored, m_type, y_values): """Build a database (sub-)query to fetch x-values in a generic manner.""" logging.info('Building up x-values.') x_values = None if x == 'size': x_values = (Run.select(Run.id, fn.Avg(Polygon.size).alias('x'), y_values.c.y, y_values.c.algorithm_id).join(Polygon).where( Polygon.type == m_type, version_constraint).order_by().alias('t1')) if x_low is not None: x_values = x_values.where(Polygon.size >= x_low) if x_high is not None: x_values = x_values.where(Polygon.size <= x_high) elif x == 'path': x_values = (Run.select( Run.id, fn.Avg(Instance.path_length).alias('x'), y_values.c.y, y_values.c.algorithm_id).join(Polygon).switch(Run).join( Instance).where(Polygon.type == m_type, version_constraint).order_by().alias('t1')) if x_low is not None: x_values = x_values.where(Instance.path_length >= x_low) if x_high is not None: x_values = x_values.where(Instance.path_length <= x_high) elif '.' in x: parts = x.split('.') if len(parts) != 2: logging.critical('I do not know how to interpret x-values "%s".', x) return 1 property_class = None if parts[0] == 'i': property_class = IntegerProperty if property_class is None: logging.critical('Prefix "%s" is unknown.', parts[0]) return 1 try: property = PropertyName.get(PropertyName.name == parts[1]) except DoesNotExist: logging.critical('Property "%s" is unknown.', parts[1]) return 1 x_values = (Run.select( Run.id, fn.Avg(property_class.value).alias('x'), y_values.c.y, y_values.c.algorithm_id).join(Instance).join( property_class).switch(Run).join(Polygon).where( Instance.algorithm == algorithm, property_class.name == property, Polygon.type == m_type, version_constraint).order_by().alias('t1')) if x_low is not None: x_values = x_values.where(property_class.value >= x_low) if x_high is not None: x_values = x_values.where(property_class.value <= x_high) return x_values
def route_index(): center_coords = Stop.select( fn.Avg(Stop.lat).alias('lat'), fn.Avg(Stop.lng).alias('lng')).get() return render_template('index.html', center_coords=center_coords)