Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Issue #528: Add metrics table for counting
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Jul 19, 2017
1 parent caa3b45 commit 909c2c9
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions atmo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class Core(AWS, Celery, Constance, CSP, Configuration):
'atmo.jobs',
'atmo.apps.KeysAppConfig',
'atmo.users',
'atmo.stats',

# Third party apps
'allauth',
Expand Down
Empty file added atmo/stats/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions atmo/stats/migrations/0001_initial.py
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.
53 changes: 53 additions & 0 deletions atmo/stats/models.py
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
)
32 changes: 32 additions & 0 deletions tests/test_stats.py
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}

0 comments on commit 909c2c9

Please sign in to comment.