<commit_before>from redis import Redis
from redis_semaphore import Semaphore
from threading import Thread
import urllib2
import time
import os
path=os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),'..'))
import sys
sys.path.insert(0, path)
from db_pool import *
import datetime
import logging

class KeyManager():
    def __init__(self):
        self.semaphore = Semaphore(Redis(host=env.get('redis').get('host')), count=1, namespace='example')

    def log_daily_key_use(self):
        logging.debug("KeyManager(): log_key_use")
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        doc = doc[0]
        date = datetime.datetime.now()
        doc["date"]=date
        doc.pop('_id',None)
        db.logs.insert_one(doc)

    def log_operation_result(self,key,operation,result):
        db.vtkeys.update_one({"doc": 1},{"$inc": { key+"."+operation+".daily."+result: 1,
            key+"."+operation+".total."+result: 1}})


    # When a private key is out of credits
    # we will block it until 00:00 UTC
    def block_key(self,apikey):
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        db.vtkeys.update_one({"doc": 1},{"$set": {apikey+".blocked": True }},upsert=False)

    def reset_daily_counter(self):
        logging.debug("KeyManager(): reset_daily_counter")
        logging.debug("KeyManager(): log_key_use")
        self.log_daily_key_use()
        logging.debug("KeyManager():</log_key_use>")
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        doc = doc[0]
        logging.debug("KeyManager(): doc.keys()="+str(doc.keys()))
        for key in doc.keys():<commit_msg>saved blocked datetime #113<commit_after>from redis import Redis
from redis_semaphore import Semaphore
from threading import Thread
import urllib2
import time
import os
path=os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),'..'))
import sys
sys.path.insert(0, path)
from db_pool import *
import datetime
import logging

class KeyManager():
    def __init__(self):
        self.semaphore = Semaphore(Redis(host=env.get('redis').get('host')), count=1, namespace='example')

    def log_daily_key_use(self):
        logging.debug("KeyManager(): log_key_use")
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        doc = doc[0]
        date = datetime.datetime.now()
        doc["date"]=date
        doc.pop('_id',None)
        db.logs.insert_one(doc)

    def log_operation_result(self,key,operation,result):
        db.vtkeys.update_one({"doc": 1},{"$inc": { key+"."+operation+".daily."+result: 1,
            key+"."+operation+".total."+result: 1}})


    # When a private key is out of credits
    # we will block it until 00:00 UTC
    def block_key(self,apikey):
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        date = datetime.datetime.now()
        db.vtkeys.update_one({"doc": 1},{"$set": {apikey+".blocked": True }},upsert=False)
        db.vtkeys.update_one({"doc": 1},{"$set": {apikey+".blocked_time": date }},upsert=False)

    def reset_daily_counter(self):
        logging.debug("KeyManager(): reset_daily_counter")
        logging.debug("KeyManager(): log_key_use")
        self.log_daily_key_use()
        logging.debug("KeyManager():</log_key_use>")
        doc = db.vtkeys.find()
        if doc.count()!=1:
            raise ValueError("doc.count() != 1! (check db.vtkeys in DB_Metadata)")
        doc = doc[0]
        logging.debug("KeyManager(): doc.keys()="+str(doc.keys()))
        for key in doc.keys():
{'fraction_non_alphanumeric': 0.16306283388450776, 'fraction_numerical': 0.010684304248282879, 'mean_word_length': 3.1696712619300107, 'pattern_counts': {'":': 13, '<': 5, '<?xml version=': 0, '>': 5, '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': '4200450', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1298, 'n_words': 265}