# Path: mrq/context.py
# _GLOBAL_CONTEXT = {
# 
#     # Contains all the running greenlets for this worker. greenletid => Job object
#     "greenlets": {},
# 
#     # pointer to the current worker
#     "worker": None,
# 
#     # pointer to the current config
#     "config": {}
# }
# def setup_context(**kwargs):
# def set_current_job(job):
# def get_current_job(greenlet_id=None):
# def set_current_worker(worker):
# def get_current_worker():
# def set_logger_config():
# def set_current_config(config):
# def get_current_config():
# def retry_current_job(delay=None, max_retries=None, queue=None):
# def abort_current_job():
# def _connections_factory(attr):
#     def versiontuple(v):
# def enable_greenlet_tracing():
#     def trace(*args):
# def run_task(path, params):
# def set_current_job_progress(ratio, save=False):
# def queue_raw_jobs(*args, **kwargs):
# def queue_job(*args, **kwargs):
# def queue_jobs(*args, **kwargs):
# def metric(*args, **kwargs):


from .context import connections, get_current_config
import time
""" Helpers are util functions which use the context """


def ratelimit(key, limit, per=1, redis=None):
    """ Returns an integer with the number of available actions for the
    current period in seconds. If zero, rate was already reached. """

    if redis is None:
        redis = connections.redis

    # http://redis.io/commands/INCR
    now = int(time.time())

    k = "ratelimit:%s:%s" % (key, now // per)

    with redis.pipeline(transaction=True) as pipeline:
        pipeline.incr(k, 1)
        pipeline.expire(k, per + 10)
        value = pipeline.execute()

    current = int(value[0]) - 1

    if current >= limit:
        return 0
    else:
        return limit - current


def metric(name, incr=1, **kwargs):

# Path: mrq/context.py
# _GLOBAL_CONTEXT = {
# 
#     # Contains all the running greenlets for this worker. greenletid => Job object
#     "greenlets": {},
# 
#     # pointer to the current worker
#     "worker": None,
# 
#     # pointer to the current config
#     "config": {}
# }
# def setup_context(**kwargs):
# def set_current_job(job):
# def get_current_job(greenlet_id=None):
# def set_current_worker(worker):
# def get_current_worker():
# def set_logger_config():
# def set_current_config(config):
# def get_current_config():
# def retry_current_job(delay=None, max_retries=None, queue=None):
# def abort_current_job():
# def _connections_factory(attr):
#     def versiontuple(v):
# def enable_greenlet_tracing():
#     def trace(*args):
# def run_task(path, params):
# def set_current_job_progress(ratio, save=False):
# def queue_raw_jobs(*args, **kwargs):
# def queue_job(*args, **kwargs):
# def queue_jobs(*args, **kwargs):
# def metric(*args, **kwargs):


# Path: mrq/helpers.py
from .context import connections, get_current_config
import time

""" Helpers are util functions which use the context """


def ratelimit(key, limit, per=1, redis=None):
    """ Returns an integer with the number of available actions for the
    current period in seconds. If zero, rate was already reached. """

    if redis is None:
        redis = connections.redis

    # http://redis.io/commands/INCR
    now = int(time.time())

    k = "ratelimit:%s:%s" % (key, now // per)

    with redis.pipeline(transaction=True) as pipeline:
        pipeline.incr(k, 1)
        pipeline.expire(k, per + 10)
        value = pipeline.execute()

    current = int(value[0]) - 1

    if current >= limit:
        return 0
    else:
        return limit - current


def metric(name, incr=1, **kwargs):

    cfg = get_current_config()