# Path: salmon/metrics/models.py
# class Source(models.Model):
# class Metric(models.Model):
#     class Meta:
# class MetricGroup(Metric):
#     class Meta:
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __init__(self, *args, **kwargs):
#     def _reset_changes(self):
#     def whisper_filename(self):
#     def add_latest_to_archive(self):
#     def get_or_create_archive(self):
#     def load_archive(self, from_date, to_date=None):
#     def in_alert_state(self):
#     def get_value_display(self):
#     def time_between_updates(self):
#     def do_transform(self):
#     def do_counter_conversion(self):
#     def check_alarm(self):
#     def save(self, *args, **kwargs):
#     OPERATOR_CHOICES = (
#         ('lt', 'value < alert'),
#         ('le', 'value <= alert'),
#         ('eq', 'value == alert'),
#         ('ne', 'value != alert'),
#         ('ge', 'value >= alert'),
#         ('gt', 'value > alert'),
#     )
#     DISPLAY_CHOICES = (
#         ('float', 'Number'),
#         ('boolean', 'True/False'),
#         ('byte', 'Bytes'),
#         ('percentage', 'Percentage'),
#         ('second', 'Seconds'),
#     )


import logging
from django.core.urlresolvers import reverse, NoReverseMatch
from django.utils.timezone import now
from rest_framework import serializers
from . import models


logger = logging.getLogger(__name__)


class MetricSerializer(serializers.ModelSerializer):
    source = serializers.CharField(source='source.name', required=False)
    value = serializers.FloatField(source='latest_value')
    timestamp = serializers.DateTimeField(source='last_updated',
                                          required=False)

    class Meta:
        model = models.Metric
        fields = ('source', 'name', 'value', 'timestamp')

    def validate_source(self, attrs, source):
        if source in attrs:
            try:
                reverse("history", args=[attrs[source]])
            except NoReverseMatch:
                raise serializers.ValidationError("Source is invalid.")
        return attrs

    def restore_object(self, attrs, instance=None):
        kwargs = {'name': attrs['name']}
        if 'source.name' in attrs:

# Path: salmon/metrics/models.py
# class Source(models.Model):
# class Metric(models.Model):
#     class Meta:
# class MetricGroup(Metric):
#     class Meta:
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __init__(self, *args, **kwargs):
#     def _reset_changes(self):
#     def whisper_filename(self):
#     def add_latest_to_archive(self):
#     def get_or_create_archive(self):
#     def load_archive(self, from_date, to_date=None):
#     def in_alert_state(self):
#     def get_value_display(self):
#     def time_between_updates(self):
#     def do_transform(self):
#     def do_counter_conversion(self):
#     def check_alarm(self):
#     def save(self, *args, **kwargs):
#     OPERATOR_CHOICES = (
#         ('lt', 'value < alert'),
#         ('le', 'value <= alert'),
#         ('eq', 'value == alert'),
#         ('ne', 'value != alert'),
#         ('ge', 'value >= alert'),
#         ('gt', 'value > alert'),
#     )
#     DISPLAY_CHOICES = (
#         ('float', 'Number'),
#         ('boolean', 'True/False'),
#         ('byte', 'Bytes'),
#         ('percentage', 'Percentage'),
#         ('second', 'Seconds'),
#     )


# Path: salmon/metrics/serializers.py
import logging
from django.core.urlresolvers import reverse, NoReverseMatch
from django.utils.timezone import now
from rest_framework import serializers
from . import models



logger = logging.getLogger(__name__)


class MetricSerializer(serializers.ModelSerializer):
    source = serializers.CharField(source='source.name', required=False)
    value = serializers.FloatField(source='latest_value')
    timestamp = serializers.DateTimeField(source='last_updated',
                                          required=False)

    class Meta:
        model = models.Metric
        fields = ('source', 'name', 'value', 'timestamp')

    def validate_source(self, attrs, source):
        if source in attrs:
            try:
                reverse("history", args=[attrs[source]])
            except NoReverseMatch:
                raise serializers.ValidationError("Source is invalid.")
        return attrs

    def restore_object(self, attrs, instance=None):
        kwargs = {'name': attrs['name']}
        if 'source.name' in attrs:

            source, created = models.Source.objects.get_or_create(