# Path: jarr/bootstrap.py
# def is_secure_served():
# def init_logging(log_path=None, log_level=logging.INFO, modules=(),
#                  log_format='%(asctime)s %(levelname)s %(message)s'):
# def init_db(echo=False):
# def init_models():
# def commit_pending_sql(*args, **kwargs):
# def rollback_pending_sql(*args, **kwargs):
# REDIS_CONN = Redis(host=conf.db.redis.host,
#                    db=conf.db.redis.db,
#                    port=conf.db.redis.port)
# 
# Path: jarr/metrics.py
# WORKER = prom(Histogram, 'worker_method', 'worker taken actions',
#               ['method', 'result'], buckets=BUCKETS_3H)


import logging
from datetime import datetime
from enum import Enum
from functools import wraps
from hashlib import sha256
from jarr.bootstrap import conf, REDIS_CONN
from jarr.metrics import WORKER


logger = logging.getLogger(__name__)
LOCK_EXPIRE = 60 * 60


def observe_worker_result_since(start, method, result):
    duration = (datetime.now() - start).total_seconds()
    WORKER.labels(method=method, result=result).observe(duration)


class Queues(Enum):
    DEFAULT = conf.celery.task_default_queue
    CRAWLING = 'jarr-crawling'
    CLUSTERING = 'jarr-clustering'


def lock(prefix, expire=LOCK_EXPIRE):
    def metawrapper(func):
        @wraps(func)
        def wrapper(args):
            start = datetime.now()
            key = str(args).encode('utf8')
            key = f"lock-{prefix}-{sha256(key).hexdigest()}"
            if REDIS_CONN.setnx(key, 'locked'):

# Path: jarr/bootstrap.py
# def is_secure_served():
# def init_logging(log_path=None, log_level=logging.INFO, modules=(),
#                  log_format='%(asctime)s %(levelname)s %(message)s'):
# def init_db(echo=False):
# def init_models():
# def commit_pending_sql(*args, **kwargs):
# def rollback_pending_sql(*args, **kwargs):
# REDIS_CONN = Redis(host=conf.db.redis.host,
#                    db=conf.db.redis.db,
#                    port=conf.db.redis.port)
# 
# Path: jarr/metrics.py
# WORKER = prom(Histogram, 'worker_method', 'worker taken actions',
#               ['method', 'result'], buckets=BUCKETS_3H)


# Path: jarr/crawler/utils.py
import logging
from datetime import datetime
from enum import Enum
from functools import wraps
from hashlib import sha256
from jarr.bootstrap import conf, REDIS_CONN
from jarr.metrics import WORKER



logger = logging.getLogger(__name__)
LOCK_EXPIRE = 60 * 60


def observe_worker_result_since(start, method, result):
    duration = (datetime.now() - start).total_seconds()
    WORKER.labels(method=method, result=result).observe(duration)


class Queues(Enum):
    DEFAULT = conf.celery.task_default_queue
    CRAWLING = 'jarr-crawling'
    CLUSTERING = 'jarr-clustering'


def lock(prefix, expire=LOCK_EXPIRE):
    def metawrapper(func):
        @wraps(func)
        def wrapper(args):
            start = datetime.now()
            key = str(args).encode('utf8')
            key = f"lock-{prefix}-{sha256(key).hexdigest()}"
            if REDIS_CONN.setnx(key, 'locked'):

                REDIS_CONN.expire(key, expire)