# Path: amon/apps/api/throttle.py
# class CheckPeriodThrottle(BaseThrottle):
#     
#     def allow_request(self, request, view):
#         request.server = None
#         allow = True
# 
#         view_name = view.get_view_name()
# 
#         allowed_views = [u'System Data', u'Collectd Data', u'Legacy System Data']
#         
#         if view_name in allowed_views:
#             server_key = view.kwargs.get('server_key')
#             server = server_model.get_server_by_key(server_key)
# 
#             if server:
# 
#                 request.server = server # Needed in the Models
#                 server_status = throttle_status(server=server)
# 
#                 
#                 if server_status.allow == False:
#                     allow = False
#             
# 
#         return allow
# 
# Path: amon/apps/api/models.py



from django.conf import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from amon.apps.servers.models import server_model
from amon.apps.api.throttle import CheckPeriodThrottle
from amon.apps.api.permissions import ApiKeyPermission
from amon.apps.notifications.sender import send_notifications
from amon.apps.api.models import api_model

        # Cloud synced servers
        instance_id = host_data.get('instance_id', "")


        server = server_model.get_or_create_by_machine_id(machine_id=machine_id,
            hostname=hostname,
            instance_id=instance_id)

        api_model.save_data_to_backend(server=server, data=data)

        # Update host data
        server_meta = {
            'ip_address': host_data.get('ip_address'),
            'distro': host_data.get('distro', {}),
        }

        server_model.update(server_meta, server['_id'])

        if settings.DEBUG is True:
            send_notifications()

        status = settings.API_RESULTS['ok']


        return Response({'status': status})


class LegacySystemDataView(APIView):


# Path: amon/apps/api/throttle.py
# class CheckPeriodThrottle(BaseThrottle):
#     
#     def allow_request(self, request, view):
#         request.server = None
#         allow = True
# 
#         view_name = view.get_view_name()
# 
#         allowed_views = [u'System Data', u'Collectd Data', u'Legacy System Data']
#         
#         if view_name in allowed_views:
#             server_key = view.kwargs.get('server_key')
#             server = server_model.get_server_by_key(server_key)
# 
#             if server:
# 
#                 request.server = server # Needed in the Models
#                 server_status = throttle_status(server=server)
# 
#                 
#                 if server_status.allow == False:
#                     allow = False
#             
# 
#         return allow
# 
# Path: amon/apps/api/models.py



# Path: amon/apps/api/views/core.py
from django.conf import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from amon.apps.servers.models import server_model
from amon.apps.api.throttle import CheckPeriodThrottle
from amon.apps.api.permissions import ApiKeyPermission
from amon.apps.notifications.sender import send_notifications
from amon.apps.api.models import api_model


        # Cloud synced servers
        instance_id = host_data.get('instance_id', "")


        server = server_model.get_or_create_by_machine_id(machine_id=machine_id,
            hostname=hostname,
            instance_id=instance_id)

        api_model.save_data_to_backend(server=server, data=data)

        # Update host data
        server_meta = {
            'ip_address': host_data.get('ip_address'),
            'distro': host_data.get('distro', {}),
        }

        server_model.update(server_meta, server['_id'])

        if settings.DEBUG is True:
            send_notifications()

        status = settings.API_RESULTS['ok']


        return Response({'status': status})


class LegacySystemDataView(APIView):


    throttle_classes = (CheckPeriodThrottle,)