from celery.signals import task_success, task_failure
from django.conf import settings
import logging

logger = logging.getLogger(__name__)

try:
    from boto.ec2 import cloudwatch

    if not settings.AWS_CLOUDWATCH_SYNC:
        CONNECTION = None
    else:
        region = settings.AWS_CLOUDWATCH_REGION
        access_key = settings.AWS_CLOUDWATCH_ACCESS_KEY
        secret_key = settings.AWS_CLOUDWATCH_SECRET_KEY

        NAMESPACE = settings.AWS_CLOUDWATCH_NAMESPACE
        PREFIX = settings.AWS_CLOUDWATCH_PREFIX
        CONNECTION = cloudwatch.connect_to_region(
            region,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
        )

except ImportError:
    NAMESPACE = None
    PREFIX = None
    CONNECTION = None


if CONNECTION:
    try:
        import boto.utils
        _instance = boto.utils.get_instance_metadata(num_retries=2)
        DIMENSIONS = {
            'instance-id': _instance['instance-id'],
        }
    except:
        DIMENSIONS = {}


def _notify_cloudwatch(task_name, state):
    '''
    Update cloudwatch with a metric alert about a task
    '''
    if CONNECTION:
        if PREFIX:
            metric = '%s.%s.%s' % (PREFIX, task_name, state)
        else:
            metric = '%s.%s' % (task_name, state)

        try:
            CONNECTION.put_metric_data(NAMESPACE, metric, 1,
                                       dimensions=DIMENSIONS)
        except:
            logger.exception('Error sending cloudwatch metric')


@task_success.connect
def notify_success(sender=None, *args, **kwargs):
    '''
    Update cloudwatch about a task success
    '''
    _notify_cloudwatch(sender.name, 'success')


@task_failure.connect
def notify_failure(sender=None, *args, **kwargs):
    '''
    Update cloudwatch about a task failure
    '''
    _notify_cloudwatch(sender.name, 'failure')

@@ -773,250 +773,8 @@
 e%0A%0A%0A
-if CONNECTION:%0A    try:%0A        import boto.utils%0A        _instance = boto.utils.get_instance_metadata(num_retries=2)%0A        DIMENSIONS = %7B%0A            'instance-id': _instance%5B'instance-id'%5D,%0A        %7D%0A    except:%0A        DIMENSIONS = %7B%7D%0A%0A%0A
 def 
@@ -1122,70 +1122,8 @@
 c, 1
-,%0A                                       dimensions=DIMENSIONS
 )%0A  

remove the instance id dimension from cloudwatch metrics