# Path: memsql_loader/util/super_json.py
# def simplejson_datetime_serializer(obj):
# def _set_defaults(kwargs):
# def dumps(data, **kwargs):
# def loads(data, **kwargs):
# def safe_loads(data, default, **kwargs):
# def pformat(d):
# 
# Path: memsql_loader/util/apsw_sql_step_queue/time_helpers.py
# def unix_timestamp(dt):
#     return int(time.mktime(dt.timetuple()))


import re
from datetime import datetime
from dateutil import parser
from clark.super_enum import SuperEnum
from memsql_loader.util import super_json as json
from memsql_loader.util.apsw_sql_step_queue.time_helpers import unix_timestamp
TASKS_TTL = 120


class TaskState(SuperEnum):
    QUEUED = SuperEnum.E
    RUNNING = SuperEnum.E
    SUCCESS = SuperEnum.E
    ERROR = SuperEnum.E
    CANCELLED = SuperEnum.E

    SUCCESS_CONDITION = 'tasks.result = \'success\''
    ERROR_CONDITION = 'tasks.result = \'error\''
    CANCELLED_CONDITION = 'tasks.result = \'cancelled\''
    FINISHED_CONDITION = 'tasks.finished IS NOT NULL'
    QUEUED_CONDITION = 'tasks.finished IS NULL AND (tasks.execution_id IS NULL OR tasks.last_contact <= datetime(:now, "unixepoch", "-%s second"))' % TASKS_TTL

    # The cancelled condition is not necessary here since a cancelled
    # task also counts as finished, and UPPER(tasks.result) will return
    # what we want in that case.
    PROJECTION = re.sub(r'\s+', ' ', """
        (CASE
            WHEN (%s) THEN UPPER(tasks.result)
            WHEN (%s) THEN 'QUEUED'
            ELSE 'RUNNING'
        END)
    """ % (FINISHED_CONDITION, QUEUED_CONDITION)).strip()

    @staticmethod
    def projection_params():
        return {

# Path: memsql_loader/util/super_json.py
# def simplejson_datetime_serializer(obj):
# def _set_defaults(kwargs):
# def dumps(data, **kwargs):
# def loads(data, **kwargs):
# def safe_loads(data, default, **kwargs):
# def pformat(d):
# 
# Path: memsql_loader/util/apsw_sql_step_queue/time_helpers.py
# def unix_timestamp(dt):
#     return int(time.mktime(dt.timetuple()))


# Path: memsql_loader/api/shared.py
import re
from datetime import datetime
from dateutil import parser
from clark.super_enum import SuperEnum
from memsql_loader.util import super_json as json
from memsql_loader.util.apsw_sql_step_queue.time_helpers import unix_timestamp

TASKS_TTL = 120


class TaskState(SuperEnum):
    QUEUED = SuperEnum.E
    RUNNING = SuperEnum.E
    SUCCESS = SuperEnum.E
    ERROR = SuperEnum.E
    CANCELLED = SuperEnum.E

    SUCCESS_CONDITION = 'tasks.result = \'success\''
    ERROR_CONDITION = 'tasks.result = \'error\''
    CANCELLED_CONDITION = 'tasks.result = \'cancelled\''
    FINISHED_CONDITION = 'tasks.finished IS NOT NULL'
    QUEUED_CONDITION = 'tasks.finished IS NULL AND (tasks.execution_id IS NULL OR tasks.last_contact <= datetime(:now, "unixepoch", "-%s second"))' % TASKS_TTL

    # The cancelled condition is not necessary here since a cancelled
    # task also counts as finished, and UPPER(tasks.result) will return
    # what we want in that case.
    PROJECTION = re.sub(r'\s+', ' ', """
        (CASE
            WHEN (%s) THEN UPPER(tasks.result)
            WHEN (%s) THEN 'QUEUED'
            ELSE 'RUNNING'
        END)
    """ % (FINISHED_CONDITION, QUEUED_CONDITION)).strip()

    @staticmethod
    def projection_params():
        return {

            'now': unix_timestamp(datetime.utcnow())