<commit_before>/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    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.
 */
/**
 * @file        src/service/monitor/EntriesManager.h
 * @author      <NAME> <<EMAIL>>
 * @version     1.0
 * @brief       This file implements class for managing monitor entries
 */

#include <algorithm>
#include <list>
#include <ctime>

#include <log/log.h>

#include "EntriesManager.h"

bool operator<(const struct timespec &lhs, const struct timespec &rhs) {
    return lhs.tv_sec < rhs.tv_sec || (lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec < rhs.tv_nsec);
}

namespace Cynara {

bool EntriesManager::isClientFilled(const ClientInfo &client) {
    return m_container.sizeFrom(client.from) >= client.bufferSize;
}

RequestContext::ClientId EntriesManager::getFilledClientId(void) {
    for (auto registeredClient : m_registeredClients) {
        auto &registeredClientInfo = registeredClient.second->second;
        if (m_container.sizeFrom(registeredClientInfo.from) >= registeredClientInfo.bufferSize) {
            return registeredClientInfo.clientId;
        }
    }
    return -1;
}

bool EntriesManager::isClientFilled(const RequestContext::ClientId &id) {
    auto clientIt = m_registeredClients.find(id);
    if (clientIt == m_registeredClients.end()) {
        LOGW("Client [" << id << "] is not registered");
        return false;
    }
    return isClientFilled(clientIt->second->second);
}

std::vector<MonitorEntry> EntriesManager::fetchEntriesForClient(RequestContext::ClientId clientId,
                                                                bool force) {
    auto regClientIt = m_registeredClients.find(clientId);
    if (regClientIt == m_registeredClients.end()) {
        LOGW("Client <" << clientId << "> not registered");
        return {};
    }
    auto clientIt = regClientIt->second;
    auto &clientInfo = clientIt->second;
    if (!force && !isClientFilled(clientInfo)) {
        LOGW("Not enough entries for client " << clientId);
        return {};
    }

    int entriesToFetch = std::min(m_container.sizeFrom(clientInfo.from), clientInfo.bufferSize);
    std::vector<MonitorEntry> entries;
    auto nextNotFetchedEntry = m_container.fetch(clientInfo.from, entriesToFetch, entries);

    updateClient(clientIt, nextNotFetchedEntry);
    //When entries are fetched for client, client is removed from waiting queue
    unregisterClient(clientId);
    cleanup();
    return entries;
}
bool EntriesManager::addClient(RequestContext::ClientId clientId, uint16_t bufferSize) {
    if (clientId == RequestContext::InvalidClientId) {
        LOGE("Invalid client id");
        return false;
    }
    if (m_clients.find(clientId) != m_clients.end()) {
        LOGE("Client [" << clientId << "] already exists, use modify instead");
        return false;
    }
    /*
     * For the first time client is registered for future entry, after this its state is kept,
     * so client can fetch entries continuously
     */
    EntriesQueue::EntryId startPoint = m_container.getBackEntryId();
    auto itInfo = m_clients.emplace(clientId, ClientInfo(clientId, startPoint, bufferSize));
    addEntryCount(startPoint, itInfo.first);
    registerClient(clientId, itInfo.first);
    return true;
}

bool EntriesManager::removeClient(RequestContext::ClientId clientId) {
    auto clientIt = m_clients.find(clientId);
    if (clientIt == m_clients.end()) {
        LOGE("Requested client [" << clientId << "] not found");
        return false;
    }

    unregisterClient(clientId);
    removeEntryCount(clientIt->second.from, clientIt->second.clientId);
    m_clients.erase(clientId);
    cleanup();
    return true;
}

bool EntriesManager::modifyClient(RequestContext::ClientId clientId, uint16_t bufferSize) {
    auto clientIt = m_clients.find(clientId);
    if (clientIt == m_clients.end()) {
        LOGE("Unknow client [" << clientId << "]");
        return false;
    }
    clientIt->second.bufferSize = bufferSize;
    registerClient(clientId, clientIt);
    return true;
}

bool EntriesManager::addEntry(const MonitorEntry &entry) {
    if (m_clients.empty()) {
        LOGD("Ignoring new entry, no client waiting");
        return false;
    }
    if (m_container.push(entry))
        squashRequests();
    return true;
}<commit_msg>Monitor: Change log level on client removal<commit_after>/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    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.
 */
/**
 * @file        src/service/monitor/EntriesManager.h
 * @author      <NAME> <<EMAIL>>
 * @version     1.0
 * @brief       This file implements class for managing monitor entries
 */

#include <algorithm>
#include <list>
#include <ctime>

#include <log/log.h>

#include "EntriesManager.h"

bool operator<(const struct timespec &lhs, const struct timespec &rhs) {
    return lhs.tv_sec < rhs.tv_sec || (lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec < rhs.tv_nsec);
}

namespace Cynara {

bool EntriesManager::isClientFilled(const ClientInfo &client) {
    return m_container.sizeFrom(client.from) >= client.bufferSize;
}

RequestContext::ClientId EntriesManager::getFilledClientId(void) {
    for (auto registeredClient : m_registeredClients) {
        auto &registeredClientInfo = registeredClient.second->second;
        if (m_container.sizeFrom(registeredClientInfo.from) >= registeredClientInfo.bufferSize) {
            return registeredClientInfo.clientId;
        }
    }
    return -1;
}

bool EntriesManager::isClientFilled(const RequestContext::ClientId &id) {
    auto clientIt = m_registeredClients.find(id);
    if (clientIt == m_registeredClients.end()) {
        LOGW("Client [" << id << "] is not registered");
        return false;
    }
    return isClientFilled(clientIt->second->second);
}

std::vector<MonitorEntry> EntriesManager::fetchEntriesForClient(RequestContext::ClientId clientId,
                                                                bool force) {
    auto regClientIt = m_registeredClients.find(clientId);
    if (regClientIt == m_registeredClients.end()) {
        LOGW("Client <" << clientId << "> not registered");
        return {};
    }
    auto clientIt = regClientIt->second;
    auto &clientInfo = clientIt->second;
    if (!force && !isClientFilled(clientInfo)) {
        LOGW("Not enough entries for client " << clientId);
        return {};
    }

    int entriesToFetch = std::min(m_container.sizeFrom(clientInfo.from), clientInfo.bufferSize);
    std::vector<MonitorEntry> entries;
    auto nextNotFetchedEntry = m_container.fetch(clientInfo.from, entriesToFetch, entries);

    updateClient(clientIt, nextNotFetchedEntry);
    //When entries are fetched for client, client is removed from waiting queue
    unregisterClient(clientId);
    cleanup();
    return entries;
}
bool EntriesManager::addClient(RequestContext::ClientId clientId, uint16_t bufferSize) {
    if (clientId == RequestContext::InvalidClientId) {
        LOGE("Invalid client id");
        return false;
    }
    if (m_clients.find(clientId) != m_clients.end()) {
        LOGE("Client [" << clientId << "] already exists, use modify instead");
        return false;
    }
    /*
     * For the first time client is registered for future entry, after this its state is kept,
     * so client can fetch entries continuously
     */
    EntriesQueue::EntryId startPoint = m_container.getBackEntryId();
    auto itInfo = m_clients.emplace(clientId, ClientInfo(clientId, startPoint, bufferSize));
    addEntryCount(startPoint, itInfo.first);
    registerClient(clientId, itInfo.first);
    return true;
}

bool EntriesManager::removeClient(RequestContext::ClientId clientId) {
    auto clientIt = m_clients.find(clientId);
    if (clientIt == m_clients.end()) {
        LOGD("Requested client [" << clientId << "] not found");
        return false;
    }

    unregisterClient(clientId);
    removeEntryCount(clientIt->second.from, clientIt->second.clientId);
    m_clients.erase(clientId);
    cleanup();
    return true;
}

bool EntriesManager::modifyClient(RequestContext::ClientId clientId, uint16_t bufferSize) {
    auto clientIt = m_clients.find(clientId);
    if (clientIt == m_clients.end()) {
        LOGE("Unknow client [" << clientId << "]");
        return false;
    }
    clientIt->second.bufferSize = bufferSize;
    registerClient(clientId, clientIt);
    return true;
}

bool EntriesManager::addEntry(const MonitorEntry &entry) {
    if (m_clients.empty()) {
        LOGD("Ignoring new entry, no client waiting");
        return false;
    }
    if (m_container.push(entry))
        squashRequests();
    return true;
}
{'fraction_non_alphanumeric': 0.1112368800164643, 'fraction_numerical': 0.0030870549495781024, 'mean_word_length': 3.4117113027689516, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '657671', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2784, 'n_words': 730}