def get_series_metrics(user: InternalUserDTO, series_uuid: UUID, filter_params: dict) -> List[MetricDTO]: series = Series.find_or_fail(series_uuid) Item.find_readable_or_fail(user, series.item_uuid) metrics_dtos = Metric.find_by_series_uuid(series.uuid, filter_params) metrics = [Metric(**m.to_dict()) for m in metrics_dtos] return to_models(metrics, MetricDTO)
def create_series_metric(user: InternalUserDTO, series_uuid: UUID, metric_dto: MetricDTO) -> MetricDTO: series = Series.find_or_fail(series_uuid) Item.find_writeable_or_fail(user, series.item_uuid) metric_dto.series_uuid = series.uuid # type: ignore metric = Metric(**metric_dto.to_dict()) metric.save() metric.session.commit() return to_model(metric, MetricDTO)
def test_get_metrics(self): me1 = Metric(value=10, magnitude_id=self.magnitude.id) me2 = Metric(value=20, magnitude_id=self.magnitude.id) db.session.add(me1) db.session.add(me2) db.session.commit() response = self.client.get('/api/v1/metrics/', headers=self.get_writer_headers()) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual(json_response.get('count'), 2) self.assertEqual(len(json_response.get('metrics')), 2)
def delete_metric(user: InternalUserDTO, series_uuid: UUID, ts: datetime) -> None: series = Series.find_or_fail(series_uuid) Item.find_writeable_or_fail(user, series.item_uuid) metric = Metric.find_or_fail((series_uuid, ts)) metric.delete() metric.session.commit()
def add_metric(): add_metric = True form = MetricForm(request.form) if request.method == 'POST' and form.validate_on_submit(): metric = Metric( creator=current_user, service_name = form.service_name.data, service_element_name = form.service_element_name.data, service_level_detail = form.service_level_detail.data , target = form.target.data, service_provider_steward_1 = form.service_provider_steward_1.data, metric_name = form.metric_name.data, metric_description = form.metric_description.data, metric_rationale = form.metric_rationale.data, metric_value_display_format = form.metric_value_display_format.data, threshold_target = form.threshold_target.data, threshold_target_rationale = form.threshold_target_rationale.data, threshold_target_direction = form.threshold_target_direction.data, threshold_trigger = form.threshold_trigger.data, threshold_trigger_rationale = form.threshold_trigger_rationale.data, threshold_trigger_direction = form.threshold_trigger_direction.data, data_source = form.data_source.data, data_update_frequency = form.data_update_frequency.data, metric_owner_primary = form.metric_owner_primary.data, vantage_control_id = form.vantage_control_id.data) db.session.add(metric) db.session.commit() flash('New metric added') return redirect(url_for('list_metrics')) return render_template('/metric.html', action="Add", add_metric=add_metric, title='Add Metrics', form=form)
def update_metric(user: InternalUserDTO, series_uuid: UUID, ts: datetime, metric_update: MetricDTO) -> MetricDTO: series = Series.find_or_fail(series_uuid) Item.find_writeable_or_fail(user, series.item_uuid) metric = Metric.find_or_fail((series_uuid, ts)) metric.data = metric_update.data metric.save() metric.session.commit() return to_model(metric, MetricDTO)
def create_data(): for i in range(100): db.session.add(Artist()) for j in range(1000): date = datetime.date.today() - datetime.timedelta(j) value = (1000 - j) / (i + 1) db.session.add( Metric(artist_id=i + 1, date=date, value=value)) db.session.commit()
def test_magnitude_metrics(self): me = Metric(value=10, magnitude_id=self.magnitude.id) db.session.add(me) db.session.commit() response = self.client.get('/api/v1/magnitudes/%d/metrics/' % self.magnitude.id, headers=self.get_writer_headers()) self.assertEqual(response.status_code, 200) json_response = json.loads(response.get_data(as_text=True)) self.assertEqual(json_response['count'], 1) self.assertEqual(json_response['metrics'][0]['id'], me.id)
def insert_one(self, metric): new_metric = Metric() if "title" in metric: new_metric.title = metric["title"] if "description" in metric: new_metric.description = metric["description"] if "source" in metric: new_metric.source = metric["source"] if "data" in metric: new_metric.data = metric["data"] if "x_axis" in metric: new_metric.x_axis = metric["x_axis"] if "y_axis" in metric: new_metric.y_axis = metric["y_axis"] new_metric.save() return False if new_metric.pk is None else new_metric
def metric(db, series, request): from datetime import datetime from app.models import Metric metric = Metric.create( series_uuid=series["uuid"], ts=datetime.fromisoformat("2020-12-01T01:00:00.000"), data={ "name": "test-series1", "second_prop": "test-prop1", "third_prop": 1 }, ) Metric.session.commit() return metric.to_dict()
def metric_init_func(row): user_id=current_user.id m = Metric( row['service_name'], row['service_element_name'], row['service_level_detail'], row['target'], row['service_provider_steward_1'], row['metric_name'], row['metric_description'], row['metric_rationale'], row['metric_value_display_format'], row['threshold_target'], row['threshold_target_rationale'], row['threshold_target_direction'], row['threshold_trigger'], row['threshold_trigger_rationale'], row['threshold_trigger_direction'], row['data_source'], row['data_update_frequency'], row['metric_owner_primary'], row['vantage_control_id'], user_id) return m
def compute_metrics(): now = arrow.now() Metric.create(date=now, name=Metric.NB_USER, value=User.query.count(), commit=True) Metric.create( date=now, name=Metric.NB_ACTIVATED_USER, value=User.query.filter_by(activated=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_REFERRED_USER, value=User.query.filter(User.referral_id.isnot(None)).count(), commit=True, ) nb_referred_user_paid = 0 for user in User.query.filter(User.referral_id.isnot(None)): if user.is_paid(): nb_referred_user_paid += 1 Metric.create( date=now, name=Metric.NB_REFERRED_USER_PAID, value=nb_referred_user_paid, commit=True, ) Metric.create( date=now, name=Metric.NB_ALIAS, value=Alias.query.count(), commit=True ) Metric.create( date=now, name=Metric.NB_BOUNCED, value=EmailLog.query.filter_by(bounced=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_SPAM, value=EmailLog.query.filter_by(is_spam=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_REPLY, value=EmailLog.query.filter_by(is_reply=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_BLOCK, value=EmailLog.query.filter_by(blocked=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_FORWARD, value=EmailLog.query.filter_by( bounced=False, is_spam=False, is_reply=False, blocked=False ).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_PREMIUM, value=Subscription.query.filter(Subscription.cancelled.is_(False)).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_CANCELLED_PREMIUM, value=Subscription.query.filter(Subscription.cancelled.is_(True)).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_APPLE_PREMIUM, value=AppleSubscription.query.count(), commit=True, ) Metric.create( date=now, name=Metric.NB_MANUAL_PREMIUM, value=ManualSubscription.query.filter( ManualSubscription.end_at > now, ManualSubscription.is_giveaway.is_(False), ).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_COINBASE_PREMIUM, value=CoinbaseSubscription.query.filter( CoinbaseSubscription.end_at > now ).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_VERIFIED_CUSTOM_DOMAIN, value=CustomDomain.query.filter_by(verified=True).count(), commit=True, ) Metric.create( date=now, name=Metric.NB_APP, value=Client.query.count(), commit=True, )
def metric(user): if request.method == 'POST': if not user.admin: return {}, 403 category_id = request.data.get('categoryId', None) metric = Metric( name=str(request.data['name']), weight=request.data['weight'], unit_label=str(request.data['unitLabel']), total_range_min=request.data['totalRangeMin'], total_range_max=request.data['totalRangeMax'], healthy_range_min=request.data['healthyRangeMin'], healthy_range_max=request.data['healthyRangeMax'], gender=str(request.data['gender']), ) try: category = Category.query.get(category_id) metric.category = category except: db.session.rollback() metric.save() response = jsonify({ 'id': metric.id, 'name': metric.name, 'weight': metric.weight, 'unitLabel': metric.unit_label, 'totalRangeMin': metric.total_range_min, 'totalRangeMax': metric.total_range_max, 'healthyRangeMin': metric.healthy_range_min, 'healthyRangeMax': metric.healthy_range_max, 'gender': metric.gender, 'categoryId': None if metric.category is None else metric.category.id, 'categoryName': None if metric.category is None else metric.category.name }) response.status_code = 201 return response else: gender = request.values.get('gender', None) if gender is None: metrics = Metric.get_all() else: metrics = Metric.query.filter_by(gender=gender) results = [] for metric in metrics: obj = { 'id': metric.id, 'name': metric.name, 'weight': metric.weight, 'unitLabel': metric.unit_label, 'totalRangeMin': metric.total_range_min, 'totalRangeMax': metric.total_range_max, 'healthyRangeMin': metric.healthy_range_min, 'healthyRangeMax': metric.healthy_range_max, 'gender': metric.gender, 'categoryId': None if metric.category is None else metric.category.id, 'categoryName': None if metric.category is None else metric.category.name } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def get_metric(user: InternalUserDTO, series_uuid: UUID, ts: datetime) -> MetricDTO: series = Series.find_or_fail(series_uuid) Item.find_readable_or_fail(user, series.item_uuid) metrics_dto = Metric.find_or_fail((series_uuid, ts)) return to_model(Metric(**metrics_dto.to_dict()), MetricDTO)
def test_clean() -> None: actual = clean(metrics=[ Metric(label="Confirmed Cases in Ohio", value="4,043"), Metric(label="Number of ICU admissions", value="346"), Metric(label="Number of Hospitalizations in Ohio", value="1,104"), Metric(label="Number of Deaths", value="119"), Metric(label="Age Range", value="<1–101"), Metric(label="Median Age", value="53"), Metric(label="Sex - Males", value="48%*"), Metric(label="Sex - Females", value="51%*"), ]) expected = [ Metric(label="Confirmed Cases", value="4,043"), Metric(label="ICU Admissions", value="346"), Metric(label="Hospitalizations", value="1,104"), Metric(label="Deaths", value="119"), Metric(label="Age Range", value="<1–101"), Metric(label="Median Age", value="53"), Metric(label="Sex - Males", value="48%"), Metric(label="Sex - Females", value="51%"), ] assert expected == actual