<commit_before>/**
 * @file heartbeats.cpp
 * @brief Classes for heartbeating Sync configuration and status
 *
 * (c) 2013 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#include "mega/heartbeats.h"
#include "mega/command.h"
#include "assert.h"

namespace mega {


HeartBeatBackupInfo::HeartBeatBackupInfo(int tag, handle aBackupId)
    : mBackupId(aBackupId), mSyncTag(tag)
{
}

handle HeartBeatBackupInfo::backupId() const
{
    return mBackupId;
}

Command *HeartBeatBackupInfo::runningCommand() const
{
    return mRunningCommand;
}

void HeartBeatBackupInfo::setRunningCommand(Command *runningCommand)
{
    mRunningCommand = runningCommand;
}

HeartBeatBackupInfo::Status HeartBeatBackupInfo::status() const
{
    return mStatus;
}

double HeartBeatBackupInfo::progress() const
{
    return std::max(0., std::min(1., static_cast<double>(mTransferredBytes) / static_cast<double>(mTotalBytes)));
}

uint32_t HeartBeatBackupInfo::pendingUps() const
{
    return mPendingUps;
}

uint32_t HeartBeatBackupInfo::pendingDowns() const
{
    return mPendingDowns;
}

m_time_t HeartBeatBackupInfo::lastAction() const
{
    return mLastAction;
}

mega::MegaHandle HeartBeatBackupInfo::lastItemUpdated() const
{
    return mLastItemUpdated;
}

void HeartBeatBackupInfo::updateTransferInfo(MegaTransfer *transfer)
{
    if (mPendingTransfers.find(transfer->getTag()) == mPendingTransfers.end())
    {
        mPendingTransfers.insert(std::make_pair(transfer->getTag(), ::mega::make_unique<PendingTransferInfo>()));
    }

    const auto &pending = mPendingTransfers[transfer->getTag()];

    auto total = mTotalBytes;
    auto transferred = mTransferredBytes;

    // reduce globals by the last known data
    total -= pending->mTotalBytes;
    transferred -= pending->mTransferredBytes;

    // update values with those of the transfer
    pending->mTotalBytes = transfer->getTotalBytes();
    pending->mTransferredBytes = transfer->getTransferredBytes();

    // reflect those in the globals
    total += pending->mTotalBytes;
    transferred += pending->mTransferredBytes;

    setTotalBytes(total);
    setTransferredBytes(transferred);
}

void HeartBeatBackupInfo::removePendingTransfer(MegaTransfer *transfer)
{
    if (mPendingTransfers.find(transfer->getTag()) == mPendingTransfers.end())
    {
        assert(false && "removing a non included transfer");
        return;
    }

    auto &pending = mPendingTransfers[transfer->getTag()];

    // add the transfer data to a list of finished. To reduce the totals with its values
    // when we consider progress is complete
    mFinishedTransfers.push_back(std::move(pending));

    mPendingTransfers.erase(transfer->getTag());

    if (mPendingTransfers.empty())
    {
        clearFinshedTransfers(); // asume the sync is up-to-date: clear totals
        assert(!mPendingUps && !mPendingDowns);
        mTotalBytes = 0;
        mTransferredBytes = 0;
    }
}

void HeartBeatBackupInfo::clearFinshedTransfers()
{
    for (const auto &pending : mFinishedTransfers)
    {<commit_msg>Avoid to search twice among pending transfers<commit_after>/**
 * @file heartbeats.cpp
 * @brief Classes for heartbeating Sync configuration and status
 *
 * (c) 2013 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#include "mega/heartbeats.h"
#include "mega/command.h"
#include "assert.h"

namespace mega {


HeartBeatBackupInfo::HeartBeatBackupInfo(int tag, handle aBackupId)
    : mBackupId(aBackupId), mSyncTag(tag)
{
}

handle HeartBeatBackupInfo::backupId() const
{
    return mBackupId;
}

Command *HeartBeatBackupInfo::runningCommand() const
{
    return mRunningCommand;
}

void HeartBeatBackupInfo::setRunningCommand(Command *runningCommand)
{
    mRunningCommand = runningCommand;
}

HeartBeatBackupInfo::Status HeartBeatBackupInfo::status() const
{
    return mStatus;
}

double HeartBeatBackupInfo::progress() const
{
    return std::max(0., std::min(1., static_cast<double>(mTransferredBytes) / static_cast<double>(mTotalBytes)));
}

uint32_t HeartBeatBackupInfo::pendingUps() const
{
    return mPendingUps;
}

uint32_t HeartBeatBackupInfo::pendingDowns() const
{
    return mPendingDowns;
}

m_time_t HeartBeatBackupInfo::lastAction() const
{
    return mLastAction;
}

mega::MegaHandle HeartBeatBackupInfo::lastItemUpdated() const
{
    return mLastItemUpdated;
}

void HeartBeatBackupInfo::updateTransferInfo(MegaTransfer *transfer)
{
    auto it = mPendingTransfers.find(transfer->getTag());
    if (it == mPendingTransfers.end())
    {
        it = mPendingTransfers.insert(std::make_pair(transfer->getTag(), ::mega::make_unique<PendingTransferInfo>())).first;
    }

    const unique_ptr<PendingTransferInfo> &pending = it->second;

    auto total = mTotalBytes;
    auto transferred = mTransferredBytes;

    // reduce globals by the last known data
    total -= pending->mTotalBytes;
    transferred -= pending->mTransferredBytes;

    // update values with those of the transfer
    pending->mTotalBytes = transfer->getTotalBytes();
    pending->mTransferredBytes = transfer->getTransferredBytes();

    // reflect those in the globals
    total += pending->mTotalBytes;
    transferred += pending->mTransferredBytes;

    setTotalBytes(total);
    setTransferredBytes(transferred);
}

void HeartBeatBackupInfo::removePendingTransfer(MegaTransfer *transfer)
{
    const auto it = mPendingTransfers.find(transfer->getTag());
    if (it == mPendingTransfers.end())
    {
        assert(false && "removing a non included transfer");
        return;
    }

    unique_ptr<PendingTransferInfo> &pending = it->second;

    // add the transfer data to a list of finished. To reduce the totals with its values
    // when we consider progress is complete
    mFinishedTransfers.push_back(std::move(pending));

    mPendingTransfers.erase(transfer->getTag());

    if (mPendingTransfers.empty())
    {
        clearFinshedTransfers(); // asume the sync is up-to-date: clear totals
        assert(!mPendingUps && !mPendingDowns);
        mTotalBytes = 0;
        mTransferredBytes = 0;
    }
}

void HeartBeatBackupInfo::clearFinshedTransfers()
{
    for (const auto &pending : mFinishedTransfers)
    {
{'fraction_non_alphanumeric': 0.1018736988202637, 'fraction_numerical': 0.0036086051353226927, 'mean_word_length': 4.2407272727272725, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, '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': '6230082', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2142, 'n_words': 570}