//------------------------------------------------------------------------------
/*
    This file is part of rippled: https://github.com/ripple/rippled
    Copyright (c) 2012, 2013 Ripple Labs Inc.

    Permission to use, copy, modify, and/or distribute this software for any
    purpose  with  or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL ,  DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <xrpld/core/LoadEvent.h>
#include <xrpld/core/LoadMonitor.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <iomanip>

namespace ripple {

LoadEvent::LoadEvent(
    LoadMonitor& monitor,
    std::string const& name,
    bool shouldStart)
    : monitor_(monitor)
    , running_(shouldStart)
    , name_(name)
    , mark_{std::chrono::steady_clock::now()}
    , timeWaiting_{}
    , timeRunning_{}
{
}

LoadEvent::~LoadEvent()
{
    if (running_)
        stop();
}

std::string const&
LoadEvent::name() const
{
    return name_;
}

std::chrono::steady_clock::duration
LoadEvent::waitTime() const
{
    return timeWaiting_;
}

std::chrono::steady_clock::duration
LoadEvent::runTime() const
{
    return timeRunning_;
}

void
LoadEvent::setName(std::string const& name)
{
    name_ = name;
}

void
LoadEvent::start()
{
    auto const now = std::chrono::steady_clock::now();

    // If we had already called start, this call will
    // replace the previous one. Any time accumulated will
    // be counted as "waiting".
    timeWaiting_ += now - mark_;
    mark_ = now;
    running_ = true;
}

void
LoadEvent::stop()
{
    XRPL_ASSERT(running_, "ripple::LoadEvent::stop : is running");

    auto const now = std::chrono::steady_clock::now();

    timeRunning_ += now - mark_;
    mark_ = now;
    running_ = false;

    monitor_.addLoadSample(*this);
}

}  // namespace ripple
