<commit_before>from datetime import datetime, timedelta
import logging
import math

from adhocracy import model
from adhocracy.lib.cache import memoize
from adhocracy.lib.util import timedelta2seconds

log = logging.getLogger(__name__)


def activity(query_filter, from_time=None, to_time=None):
    if not to_time:
        to_time = datetime.utcnow()
    if not from_time:
        from_time = to_time - timedelta(days=30)
    base_age = timedelta2seconds(to_time - from_time)

    query = model.meta.Session.query(model.Event.time)
    query = query.filter(model.Event.time >= from_time)
    query = query.filter(model.Event.time <= to_time)
    query = query.order_by(model.Event.time.asc())
    query = query_filter(query)

    def evt_value(event_time):
        age = base_age - timedelta2seconds(to_time - event_time)
        return math.log(max(1, age))

    act = sum([evt_value(row[0]) for row in query])
    log.debug("Activity %s - %s: %s" % (from_time, to_time, act))
    return act


@memoize('instance_activity', 84600)
def instance_activity(instance, from_time=None, to_time=None):
    def query_filter(q):
        return q.filter(model.Event.instance == instance)
    return activity(query_filter, from_time, to_time)


@memoize('user_activity', 84600)
def user_activity(instance, user, from_time=None, to_time=None):
    '''
    compute the user activity, either for a given
    :class:`adhocracy.model.Instance` *instance*, or across
    all instances if *instance* is `None`
    '''
    def query_filter(q):
        q = q.filter(model.Event.user == user)
        if instance is not None:
            q = q.filter(model.Event.instance == instance)
        return q
    return activity(query_filter, from_time, to_time)<commit_msg>remove debug logging for calculated Activity<commit_after>from datetime import datetime, timedelta
import logging
import math

from adhocracy import model
from adhocracy.lib.cache import memoize
from adhocracy.lib.util import timedelta2seconds

log = logging.getLogger(__name__)


def activity(query_filter, from_time=None, to_time=None):
    if not to_time:
        to_time = datetime.utcnow()
    if not from_time:
        from_time = to_time - timedelta(days=30)
    base_age = timedelta2seconds(to_time - from_time)

    query = model.meta.Session.query(model.Event.time)
    query = query.filter(model.Event.time >= from_time)
    query = query.filter(model.Event.time <= to_time)
    query = query.order_by(model.Event.time.asc())
    query = query_filter(query)

    def evt_value(event_time):
        age = base_age - timedelta2seconds(to_time - event_time)
        return math.log(max(1, age))

    act = sum([evt_value(row[0]) for row in query])
    return act


@memoize('instance_activity', 84600)
def instance_activity(instance, from_time=None, to_time=None):
    def query_filter(q):
        return q.filter(model.Event.instance == instance)
    return activity(query_filter, from_time, to_time)


@memoize('user_activity', 84600)
def user_activity(instance, user, from_time=None, to_time=None):
    '''
    compute the user activity, either for a given
    :class:`adhocracy.model.Instance` *instance*, or across
    all instances if *instance* is `None`
    '''
    def query_filter(q):
        q = q.filter(model.Event.user == user)
        if instance is not None:
            q = q.filter(model.Event.instance == instance)
        return q
    return activity(query_filter, from_time, to_time)
{'fraction_non_alphanumeric': 0.10725643249494074, 'fraction_numerical': 0.009829430471234461, 'mean_word_length': 3.720327421555252, 'pattern_counts': {'":': 0, '<': 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': '2106114', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1096, 'n_words': 269}