<commit_before># -*- coding: utf-8 -*-
# Copyright 2018 New Vector
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from twisted.internet import defer

from synapse.util.caches.descriptors import cached

from ._base import SQLBaseStore

logger = logging.getLogger(__name__)

# Number of msec of granularity to store the monthly_active_user timestamp
# This means it is not necessary to update the table on every request
LAST_SEEN_GRANULARITY = 60 * 60 * 1000


class MonthlyActiveUsersStore(SQLBaseStore):
    def __init__(self, dbconn, hs):
        super(MonthlyActiveUsersStore, self).__init__(None, hs)
        self._clock = hs.get_clock()
        self.hs = hs
        self.reserved_users = ()

    @defer.inlineCallbacks
    def initialise_reserved_users(self, threepids):
        # TODO Why can't I do this in init?
        store = self.hs.get_datastore()
        reserved_user_list = []

        # Do not add more reserved users than the total allowable number
        for tp in threepids[:self.hs.config.max_mau_value]:
            user_id = yield store.get_user_id_by_threepid(
                tp["medium"], tp["address"]
            )
            if user_id:
                self.upsert_monthly_active_user(user_id)
                reserved_user_list.append(user_id)
            else:
                logger.warning(
                    "mau limit reserved threepid %s not found in db" % tp
                )
        self.reserved_users = tuple(reserved_user_list)

    @defer.inlineCallbacks
    def reap_monthly_active_users(self):
        """
        Cleans out monthly active user table to ensure that no stale
        entries exist.<commit_msg>adding missing yield<commit_after># -*- coding: utf-8 -*-
# Copyright 2018 New Vector
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from twisted.internet import defer

from synapse.util.caches.descriptors import cached

from ._base import SQLBaseStore

logger = logging.getLogger(__name__)

# Number of msec of granularity to store the monthly_active_user timestamp
# This means it is not necessary to update the table on every request
LAST_SEEN_GRANULARITY = 60 * 60 * 1000


class MonthlyActiveUsersStore(SQLBaseStore):
    def __init__(self, dbconn, hs):
        super(MonthlyActiveUsersStore, self).__init__(None, hs)
        self._clock = hs.get_clock()
        self.hs = hs
        self.reserved_users = ()

    @defer.inlineCallbacks
    def initialise_reserved_users(self, threepids):
        # TODO Why can't I do this in init?
        store = self.hs.get_datastore()
        reserved_user_list = []

        # Do not add more reserved users than the total allowable number
        for tp in threepids[:self.hs.config.max_mau_value]:
            user_id = yield store.get_user_id_by_threepid(
                tp["medium"], tp["address"]
            )
            if user_id:
                yield self.upsert_monthly_active_user(user_id)
                reserved_user_list.append(user_id)
            else:
                logger.warning(
                    "mau limit reserved threepid %s not found in db" % tp
                )
        self.reserved_users = tuple(reserved_user_list)

    @defer.inlineCallbacks
    def reap_monthly_active_users(self):
        """
        Cleans out monthly active user table to ensure that no stale
        entries exist.
{'fraction_non_alphanumeric': 0.0711825487944891, 'fraction_numerical': 0.007807118254879449, 'mean_word_length': 2.787826086956522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3092140', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1186, 'n_words': 424}