<commit_before>import json
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from api.models.auth import RequireLogin
from api.models import redis_wrapper


@csrf_exempt
def rest(request, *pargs):
    """
    Calls python function corresponding with HTTP METHOD name. 
    Calls with incomplete arguments will return HTTP 400
    """
    if request.method == 'GET':
        rest_function = get
    elif request.method == 'POST':
        rest_function = post
    elif request.method == 'PUT':
        rest_function = put
    elif request.method == 'DELETE':
        rest_function = delete
    else:
        return JsonResponse({"error": "HTTP METHOD UNKNOWN"})

    try:
        return rest_function(request, *pargs)
    except TypeError as e:
            print e
            return HttpResponseBadRequest("unknown method")


@RequireLogin
def get(request):
    r = redis_wrapper.init_redis()

    key = 'token:%s' % (request.token_string,)
    last_score = r.hget(key, 'last_score')
    last_score = int(last_score) if last_score else 0

    traffic = r.zrangebyscore('heliosburn.traffic', last_score, '+inf', withscores=True)

    requests = []
    for message in traffic:
        request = json.loads(message[0])<commit_msg>get last score from Redis when requesting traffic for the first time to avoid retrieving all traffic history.<commit_after>import json
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from api.models.auth import RequireLogin
from api.models import redis_wrapper


@csrf_exempt
def rest(request, *pargs):
    """
    Calls python function corresponding with HTTP METHOD name. 
    Calls with incomplete arguments will return HTTP 400
    """
    if request.method == 'GET':
        rest_function = get
    elif request.method == 'POST':
        rest_function = post
    elif request.method == 'PUT':
        rest_function = put
    elif request.method == 'DELETE':
        rest_function = delete
    else:
        return JsonResponse({"error": "HTTP METHOD UNKNOWN"})

    try:
        return rest_function(request, *pargs)
    except TypeError as e:
            print e
            return HttpResponseBadRequest("unknown method")


@RequireLogin
def get(request):
    r = redis_wrapper.init_redis()

    key = 'token:%s' % (request.token_string,)
    last_score = r.hget(key, 'last_score')

    if last_score:
        last_score = int(last_score)
    else:
        # get the last score from Redis
        traffic = r.zrevrange('heliosburn.traffic', 0, 0, withscores=True)
        last_score = int(traffic[0][1])+1 if len(traffic) > 0 else 0

    traffic = r.zrangebyscore('heliosburn.traffic', last_score, '+inf', withscores=True)

    requests = []
    for message in traffic:
        request = json.loads(message[0])
{'fraction_non_alphanumeric': 0.09513960703205791, 'fraction_numerical': 0.005515339538090314, 'mean_word_length': 3.2119013062409287, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3624422', 'n_tokens_mistral': 879, 'n_tokens_neox': 834, 'n_words': 249}