<commit_before>"""Test liveness of hosts"""
import socket
import web

DATABASE_ACCESS=None

def myWebError(msg):
    return web.HTTPError(msg, {"Content-type": "text/plain"}, msg+'\n\n')

def lan(name=None, service='services/%s', ip=None, port=80, timeout=5):
    if not name:
        raise myWebError("401 Required argument name missing")
    if not ip:
        ip = name
    alive = True
    try:
        s = socket.create_connection((ip, int(port)), timeout)
    except socket.error:
        alive = False
    if service:
        if '%' in service:
            service = service % name
        if not DATABASE_ACCESS: 
            msg = "502 plugin did not have DATABASE_ACCESS set"
            raise web.HTTPError(msg, {"Content-type": "text/plain"}, msg+'\n\n')
        try:
            oldValue = DATABASE_ACCESS.get_key(service, 'text/plain', None)
        except web.HTTPError:
            web.ctx.status = "200 OK"
            oldValue = 'rabarber'
        if oldValue != repr(alive):
            xpAlive = 'true' if alive else ''
            try:
                rv = DATABASE_ACCESS.put_key(service + '/alive', 'text/plain', None, xpAlive, 'text/plain', replace=True)
            except web.HTTPError:
                raise myWebError("501 Failed to store into %s" % service)
            if alive:
                # If the service is alive we delete any error message and we also reset the "ignore errors" indicator
                try:
                    DATABASE_ACCESS.delete_key(service + '/errorMessage')
                except web.HTTPError:
                    pass
                try:
                    DATABASE_ACCESS.delete_key(service + '/ignoreErrorUntil')
                except web.HTTPError:
                    pass
            else:
                # If the service is not alive we set an error message
                DATABASE_ACCESS.put_key(service + '/errorMessage', 'text/plain', None, "%s is not available" % name, 'text/plain', replace=True)
    return repr(alive)<commit_msg>Oops, forgot the code:-)<commit_after>"""Test liveness of hosts"""
import socket
import web
import glob
import os

DATABASE_ACCESS=None

def myWebError(msg):
    return web.HTTPError(msg, {"Content-type": "text/plain"}, msg+'\n\n')

def niceDelta(delta):
    if delta < 60:
        return "%d seconds" % delta
    delta = (delta+1) / 60
    if delta < 60:
        return "%d minutes" % delta
    delta = (delta+1) / 60
    if delta < 48:
        return "%d hours" % delta
    delta = (delta+1) / 24
    if delta < 14:
        return "%d days" % delta
    delta = (delta+1) / 7
    return "%d weeks" % delta
    
    
def lastFileAccess(name=None, service='services/%s', path=None, stamp="mtime", max=0):
    if not name or not path:
        raise myWebError("401 Required arguments (name or path) missing")
    message = None
    latest = -1
    alive = None
    for p in glob.iglob(path):
        st = os.stat(p)
        if stamp == 'mtime':
            timestamp = st.st_mtime
        elif stamp == 'ctime':
            timestamp = st.st_ctime
        elif stamp == 'atime':
            timestamp = st.st_atime
        else:
            raise myWebError("401 unknown timestamp type stamp=%s" % stamp)
        if timestamp > latest:
            latest = timestamp
    if latest < 0:
        message = "%s timestamp file not found" % name
    else:
        if max > 0:
            if latest + max > time.time():
                alive = True
            else:
                alive = False
                delta = time.time() - latest
                message = "%s has not been active for %s" % (name, niceDelta(delta))

    # Now fill in fields. Note that we can also have missing fields (which we delete)
    if '%' in service:
        service = service % name
    if alive == None:
        try:
            DATABASE_ACCESS.delete_key(service + '/alive')
        except web.HTTPError:
            pass
    else:
        xpAlive = 'true' if alive else ''
        try:
            oldValue = DATABASE_ACCESS.get_key(service + '/alive', 'text/plain', None)
        except web.HTTPError:
            web.ctx.status = "200 OK"
            oldValue = 'rabarber'
            if oldValue != xpAlive:
                try:
                    rv = DATABASE_ACCESS.put_key(service + '/alive', 'text/plain', None, xpAlive, 'text/plain', replace=True)
                except web.HTTPError:
                    raise myWebError("501 Failed to store into %s" % (service + '/alive'))
                if alive:
                    # If the service is alive we delete any error message and we also reset the "ignore errors" indicator
                    try:
                        DATABASE_ACCESS.delete_key(service + '/ignoreErrorUntil')
                    except web.HTTPError:
                        pass

    if latest < 0:
        try:
            DATABASE_ACCESS.delete_key(service + '/lastActivity')
        except web.HTTPError:
            pass
    else:
        try:
            rv = DATABASE_ACCESS.put_key(service + '/lastActivity', 'text/plain', None, str(int(latest)), 'text/plain', replace=True)
        except web.HTTPError:
            raise myWebError("501 Failed to store into %s" % (service + '/lastActivity')

    if not message:
        try:
            DATABASE_ACCESS.delete_key(service + '/errorMessage')
        except web.HTTPError:
            pass
    else:
        try:
            rv = DATABASE_ACCESS.put_key(service + '/errorMessage', 'text/plain', None, message, 'text/plain', replace=True)
        except web.HTTPError:
            raise myWebError("501 Failed to store into %s" % (service + '/errorMessage')
{'fraction_non_alphanumeric': 0.10683912119064494, 'fraction_numerical': 0.010099220411055989, 'mean_word_length': 1.8481331987891019, 'pattern_counts': {'":': 3, '<': 9, '<?xml version=': 0, '>': 6, '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': '4323508', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1609, 'n_words': 489}