# Path: memsql_loader/api/base.py
# class Api(object):
#     name = None
# 
#     def __init__(self):
#         self.logger = log.get_logger(self.name or 'api')
#         self.storage = LoaderStorage()
# 
#     def query(self, params):
#         assert 'validate' in dir(self), '`validate` must be defined'
#         return self._execute(self.validate(params))
# 
#     def _execute(self, params):
#         raise NotImplemented()
# 
#     def __db_caller(self, callback):
#         with self.storage.transaction() as cursor:
#             return callback(cursor)
# 
#     def _db_query(self, *args, **kwargs):
#         return self.__db_caller(lambda c: apsw_helpers.query(c, *args, **kwargs))
# 
#     def _db_custom_query(self, callback):
#         return self.__db_caller(callback)
# 
#     def _db_get(self, *args, **kwargs):
#         return self.__db_caller(lambda c: apsw_helpers.get(c, *args, **kwargs))
# 
# Path: memsql_loader/api/shared.py
# TASKS_TTL = 120
#     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
#     PROJECTION = re.sub(r'\s+', ' ', """
#         (CASE
#             WHEN (%s) THEN UPPER(tasks.result)
#             WHEN (%s) THEN 'QUEUED'
#             ELSE 'RUNNING'
#         END)
#     """ % (FINISHED_CONDITION, QUEUED_CONDITION)).strip()
#     QUEUED = SuperEnum.E
#     RUNNING = SuperEnum.E
#     FINISHED = SuperEnum.E
#     CANCELLED = SuperEnum.E
#     PROJECTION = re.sub(r'\s+', ' ', '''
#         (CASE
#             WHEN (
#                 (job_tasks.tasks_total - job_tasks.tasks_finished) = 0
#                 AND job_tasks.tasks_cancelled > 0) THEN 'CANCELLED'
#             WHEN (
#                 job_tasks.tasks_total IS NULL
#                 OR job_tasks.tasks_finished = job_tasks.tasks_total) THEN 'FINISHED'
#             WHEN (job_tasks.tasks_queued = job_tasks.tasks_total) THEN 'QUEUED'
#             ELSE 'RUNNING'
#         END)
#     ''').strip()
#     DESC = SuperEnum.E
#     ASC = SuperEnum.E
# class TaskState(SuperEnum):
# class JobState(SuperEnum):
# class SortDirection(SuperEnum):
#     def projection_params():
# def task_load_row(row):
# def job_load_row(row):
# 
# Path: memsql_loader/api/validation.py
# def listor(sub_validator):
#     def _validate(value):
# def validate_enum(EnumType):
#     def _validate(value):


import dateutil.parser
from memsql_loader.api.base import Api
from memsql_loader.api import shared
from memsql_loader.api.validation import V, validate_enum, listor


class Jobs(Api):
    SORTABLE_COLUMNS = [ 'id', 'created', 'last_contact', 'state', 'tasks_queued', 'tasks_running', 'tasks_cancelled', 'tasks_errored', 'tasks_finished', 'tasks_total' ]

    name = 'Jobs'


# Path: memsql_loader/api/base.py
# class Api(object):
#     name = None
# 
#     def __init__(self):
#         self.logger = log.get_logger(self.name or 'api')
#         self.storage = LoaderStorage()
# 
#     def query(self, params):
#         assert 'validate' in dir(self), '`validate` must be defined'
#         return self._execute(self.validate(params))
# 
#     def _execute(self, params):
#         raise NotImplemented()
# 
#     def __db_caller(self, callback):
#         with self.storage.transaction() as cursor:
#             return callback(cursor)
# 
#     def _db_query(self, *args, **kwargs):
#         return self.__db_caller(lambda c: apsw_helpers.query(c, *args, **kwargs))
# 
#     def _db_custom_query(self, callback):
#         return self.__db_caller(callback)
# 
#     def _db_get(self, *args, **kwargs):
#         return self.__db_caller(lambda c: apsw_helpers.get(c, *args, **kwargs))
# 
# Path: memsql_loader/api/shared.py
# TASKS_TTL = 120
#     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
#     PROJECTION = re.sub(r'\s+', ' ', """
#         (CASE
#             WHEN (%s) THEN UPPER(tasks.result)
#             WHEN (%s) THEN 'QUEUED'
#             ELSE 'RUNNING'
#         END)
#     """ % (FINISHED_CONDITION, QUEUED_CONDITION)).strip()
#     QUEUED = SuperEnum.E
#     RUNNING = SuperEnum.E
#     FINISHED = SuperEnum.E
#     CANCELLED = SuperEnum.E
#     PROJECTION = re.sub(r'\s+', ' ', '''
#         (CASE
#             WHEN (
#                 (job_tasks.tasks_total - job_tasks.tasks_finished) = 0
#                 AND job_tasks.tasks_cancelled > 0) THEN 'CANCELLED'
#             WHEN (
#                 job_tasks.tasks_total IS NULL
#                 OR job_tasks.tasks_finished = job_tasks.tasks_total) THEN 'FINISHED'
#             WHEN (job_tasks.tasks_queued = job_tasks.tasks_total) THEN 'QUEUED'
#             ELSE 'RUNNING'
#         END)
#     ''').strip()
#     DESC = SuperEnum.E
#     ASC = SuperEnum.E
# class TaskState(SuperEnum):
# class JobState(SuperEnum):
# class SortDirection(SuperEnum):
#     def projection_params():
# def task_load_row(row):
# def job_load_row(row):
# 
# Path: memsql_loader/api/validation.py
# def listor(sub_validator):
#     def _validate(value):
# def validate_enum(EnumType):
#     def _validate(value):


# Path: memsql_loader/api/jobs.py
import dateutil.parser
from memsql_loader.api.base import Api
from memsql_loader.api import shared
from memsql_loader.api.validation import V, validate_enum, listor



class Jobs(Api):
    SORTABLE_COLUMNS = [ 'id', 'created', 'last_contact', 'state', 'tasks_queued', 'tasks_running', 'tasks_cancelled', 'tasks_errored', 'tasks_finished', 'tasks_total' ]

    name = 'Jobs'


    validate = V.Schema({