# Path: kardboard/app.py
#     DEBUG_TOOLBAR = True
#     DEBUG_TOOLBAR = False
#     SENTRY_SUPPORT = True
#     SENTRY_SUPPORT = False
# def get_app():
# 
# Path: kardboard/app.py
#     DEBUG_TOOLBAR = True
#     DEBUG_TOOLBAR = False
#     SENTRY_SUPPORT = True
#     SENTRY_SUPPORT = False
# def get_app():
# 
# Path: kardboard/util.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# def log_exception(exc, msg=""):
#     tb = traceback.format_exc()
#     log_msg = [msg, str(exc), tb]
#     log_msg = '\n'.join(log_msg)
#     app = get_current_app()
#     app.logger.critical(log_msg)
# 
# Path: kardboard/tasks.py
# @celery.task(name="tasks.update_ticket", ignore_result=True)
# def update_ticket(card_id):
#     from kardboard.app import app
# 
#     statsd_conn = app.statsd.get_client('tasks.update_ticket')
#     consider_counter = statsd_conn.get_client('consider', class_=statsd.Counter)
#     update_counter = statsd_conn.get_client('update', class_=statsd.Counter)
#     error_counter = statsd_conn.get_client('error', class_=statsd.Counter)
# 
#     timer = statsd_conn.get_client(class_=statsd.Timer)
#     timer.start()
# 
#     consider_counter += 1
# 
#     logger = update_ticket.get_logger()
#     try:
#         # We want to update cards if their local update time
#         # is less than their origin update time
#         k = Kard.objects.with_id(card_id)
#         i = k.ticket_system.get_issue(k.key)
#         origin_updated = getattr(i, 'updated')
#         local_updated = k.ticket_system_data.get('updated', None)
# 
#         should_update = False
#         if not local_updated:
#             # We've never sync'd before, time to do it right now
#             should_update = True
#         elif origin_updated:
#             if local_updated < origin_updated:
#                 logger.info(
#                     "%s UPDATED on origin: Local: %s < Origin: %s" % (k.key, local_updated, origin_updated)
#                 )
#                 should_update = True
#             else:
#                 k._ticket_system_updated_at = datetime.datetime.now()
#                 k.save()
#         else:
#             # Ok well something changed with the ticket system
#             # so we need fall back to the have we updated
#             # from origin in THRESHOLD seconds, regardless
#             # of how long ago the origin was updated
#             # less efficient, but it guarantees updates
#             threshold = app.config.get('TICKET_UPDATE_THRESHOLD', 60 * 60)
#             now = datetime.datetime.now()
#             diff = now - local_updated
#             if diff.seconds >= threshold:
#                 should_update = True
#                 logger.info(
#                     "%s FORCED UPDATE because no origin update date available")
# 
#         if should_update:
#             logger.info("update_ticket running for %s" % (k.key, ))
#             try:
#                 update_counter += 1
#                 k.ticket_system.actually_update()
#             except AttributeError:
#                 error_counter += 1
#                 logger.warning('Updating kard: %s and we got an AttributeError' % k.key)
#                 raise
# 
#     except Kard.DoesNotExist:
#         error_counter += 1
#         logger.error(
#             "update_ticket: Kard with id %s does not exist" % (card_id, ))
#     except Exception, e:
#         error_counter += 1
#         message = "update_ticket: Couldn't update ticket %s from ticket system" % (card_id, )
#         log_exception(e, message)
# 
#     timer.stop()


import urlparse
import datetime
import cPickle as pickle
import statsd
import dateutil
from kardboard.app import cache
from kardboard.app import app
from kardboard.util import ImproperlyConfigured, log_exception
from kardboard.tasks import update_ticket
            from suds.client import Client
                        resolution_msg = "and resolution was %s" % current_resolution
                    self.logger.info(
                        "AUTOMOVE: %s state moved %s => %s because status was %s %s" % (self.card.key,
                            oldstate, card.state, current_ticket_status, resolution_msg))

        if datefield and state_changed is True:
            current_value = getattr(card, datefield)
            if not current_value:
                setattr(card, datefield, datetime.datetime.now())
                self.logger.info(
                    "AUTOMOVE DATE: %s %s set to %s because status was %s" % (self.card.key,
                    datefield, getattr(card, datefield), current_ticket_status))

        return card

    def actually_update(self, issue=None):
        statsd_conn = self.statsd.get_client('actually_update')
        counter = statsd_conn.get_client(class_=statsd.Counter)
        timer = statsd_conn.get_client(class_=statsd.Timer)
        timer.start()
        counter += 1

        super(JIRAHelper, self).update()

        if not issue:
            self.logger.info("Fetching JIRA data for %s" % self.card.key)
            try:
                issue = self.get_issue(self.card.key)
            except Exception:
                issue = None

# Path: kardboard/app.py
#     DEBUG_TOOLBAR = True
#     DEBUG_TOOLBAR = False
#     SENTRY_SUPPORT = True
#     SENTRY_SUPPORT = False
# def get_app():
# 
# Path: kardboard/app.py
#     DEBUG_TOOLBAR = True
#     DEBUG_TOOLBAR = False
#     SENTRY_SUPPORT = True
#     SENTRY_SUPPORT = False
# def get_app():
# 
# Path: kardboard/util.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# def log_exception(exc, msg=""):
#     tb = traceback.format_exc()
#     log_msg = [msg, str(exc), tb]
#     log_msg = '\n'.join(log_msg)
#     app = get_current_app()
#     app.logger.critical(log_msg)
# 
# Path: kardboard/tasks.py
# @celery.task(name="tasks.update_ticket", ignore_result=True)
# def update_ticket(card_id):
#     from kardboard.app import app
# 
#     statsd_conn = app.statsd.get_client('tasks.update_ticket')
#     consider_counter = statsd_conn.get_client('consider', class_=statsd.Counter)
#     update_counter = statsd_conn.get_client('update', class_=statsd.Counter)
#     error_counter = statsd_conn.get_client('error', class_=statsd.Counter)
# 
#     timer = statsd_conn.get_client(class_=statsd.Timer)
#     timer.start()
# 
#     consider_counter += 1
# 
#     logger = update_ticket.get_logger()
#     try:
#         # We want to update cards if their local update time
#         # is less than their origin update time
#         k = Kard.objects.with_id(card_id)
#         i = k.ticket_system.get_issue(k.key)
#         origin_updated = getattr(i, 'updated')
#         local_updated = k.ticket_system_data.get('updated', None)
# 
#         should_update = False
#         if not local_updated:
#             # We've never sync'd before, time to do it right now
#             should_update = True
#         elif origin_updated:
#             if local_updated < origin_updated:
#                 logger.info(
#                     "%s UPDATED on origin: Local: %s < Origin: %s" % (k.key, local_updated, origin_updated)
#                 )
#                 should_update = True
#             else:
#                 k._ticket_system_updated_at = datetime.datetime.now()
#                 k.save()
#         else:
#             # Ok well something changed with the ticket system
#             # so we need fall back to the have we updated
#             # from origin in THRESHOLD seconds, regardless
#             # of how long ago the origin was updated
#             # less efficient, but it guarantees updates
#             threshold = app.config.get('TICKET_UPDATE_THRESHOLD', 60 * 60)
#             now = datetime.datetime.now()
#             diff = now - local_updated
#             if diff.seconds >= threshold:
#                 should_update = True
#                 logger.info(
#                     "%s FORCED UPDATE because no origin update date available")
# 
#         if should_update:
#             logger.info("update_ticket running for %s" % (k.key, ))
#             try:
#                 update_counter += 1
#                 k.ticket_system.actually_update()
#             except AttributeError:
#                 error_counter += 1
#                 logger.warning('Updating kard: %s and we got an AttributeError' % k.key)
#                 raise
# 
#     except Kard.DoesNotExist:
#         error_counter += 1
#         logger.error(
#             "update_ticket: Kard with id %s does not exist" % (card_id, ))
#     except Exception, e:
#         error_counter += 1
#         message = "update_ticket: Couldn't update ticket %s from ticket system" % (card_id, )
#         log_exception(e, message)
# 
#     timer.stop()


# Path: kardboard/tickethelpers.py
import urlparse
import datetime
import cPickle as pickle
import statsd
import dateutil
from kardboard.app import cache
from kardboard.app import app
from kardboard.util import ImproperlyConfigured, log_exception
from kardboard.tasks import update_ticket
            from suds.client import Client

                        resolution_msg = "and resolution was %s" % current_resolution
                    self.logger.info(
                        "AUTOMOVE: %s state moved %s => %s because status was %s %s" % (self.card.key,
                            oldstate, card.state, current_ticket_status, resolution_msg))

        if datefield and state_changed is True:
            current_value = getattr(card, datefield)
            if not current_value:
                setattr(card, datefield, datetime.datetime.now())
                self.logger.info(
                    "AUTOMOVE DATE: %s %s set to %s because status was %s" % (self.card.key,
                    datefield, getattr(card, datefield), current_ticket_status))

        return card

    def actually_update(self, issue=None):
        statsd_conn = self.statsd.get_client('actually_update')
        counter = statsd_conn.get_client(class_=statsd.Counter)
        timer = statsd_conn.get_client(class_=statsd.Timer)
        timer.start()
        counter += 1

        super(JIRAHelper, self).update()

        if not issue:
            self.logger.info("Fetching JIRA data for %s" % self.card.key)
            try:
                issue = self.get_issue(self.card.key)
            except Exception:
                issue = None

                log_exception("Couldn't fetch JIRA issue %s" % self.card.key)