This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #528: Add metrics table for counting
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.3 on 2017-07-19 19:56 | ||
from __future__ import unicode_literals | ||
|
||
import django.contrib.postgres.fields.jsonb | ||
import django.core.serializers.json | ||
from django.db import migrations, models | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Metric', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(blank=True, default=django.utils.timezone.now, editable=False)), | ||
('key', models.CharField(help_text='Name of the metric being recorded', max_length=100)), | ||
('value', models.PositiveIntegerField(help_text='Integer value of the metric')), | ||
('data', django.contrib.postgres.fields.jsonb.JSONField(blank=True, encoder=django.core.serializers.json.DjangoJSONEncoder, help_text='Extra data about this metric', null=True)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, you can obtain one at http://mozilla.org/MPL/2.0/. | ||
from django.contrib.postgres.fields import JSONField | ||
from django.core.serializers.json import DjangoJSONEncoder | ||
from django.db import models | ||
from django.utils import timezone | ||
|
||
|
||
class Metric(models.Model): | ||
created_at = models.DateTimeField( | ||
editable=False, | ||
blank=True, | ||
default=timezone.now, | ||
) | ||
key = models.CharField( | ||
max_length=100, | ||
help_text='Name of the metric being recorded', | ||
) | ||
value = models.PositiveIntegerField( | ||
help_text='Integer value of the metric', | ||
) | ||
data = JSONField( | ||
encoder=DjangoJSONEncoder, | ||
blank=True, | ||
null=True, | ||
help_text='Extra data about this metric', | ||
) | ||
|
||
@classmethod | ||
def record(cls, key, value=1, **kwargs): | ||
""" | ||
Create a new entry in the ``Metric`` table. | ||
:param key: | ||
The metric key name. | ||
:param value: | ||
The metric value as an integer. | ||
:param data: | ||
Any extra data to be stored with this record as a dictionary. | ||
""" | ||
created_at = kwargs.pop('created_at', None) or timezone.now() | ||
data = kwargs.pop('data', None) | ||
|
||
cls.objects.create( | ||
created_at=created_at, | ||
key=key, | ||
value=value, | ||
data=data | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, you can obtain one at http://mozilla.org/MPL/2.0/. | ||
from atmo.stats.models import Metric | ||
|
||
|
||
def test_metrics_record(now, one_hour_ago): | ||
Metric.record('metric-key-1') | ||
Metric.record('metric-key-2', 500) | ||
Metric.record('metric-key-3', data={'other-value': 'test'}) | ||
Metric.record('metric-key-4', created_at=one_hour_ago, | ||
data={'other-value-2': 100}) | ||
|
||
m = Metric.objects.get(key='metric-key-1') | ||
assert m.value == 1 | ||
assert m.created_at.replace(microsecond=0) == now | ||
assert m.data is None | ||
|
||
m = Metric.objects.get(key='metric-key-2') | ||
assert m.value == 500 | ||
assert m.created_at.replace(microsecond=0) == now | ||
assert m.data is None | ||
|
||
m = Metric.objects.get(key='metric-key-3') | ||
assert m.value == 1 | ||
assert m.created_at.replace(microsecond=0) == now | ||
assert m.data == {'other-value': 'test'} | ||
|
||
m = Metric.objects.get(key='metric-key-4') | ||
assert m.value == 1 | ||
assert m.created_at.replace(microsecond=0) == one_hour_ago | ||
assert m.data == {'other-value-2': 100} |