2016-03-31 02:25:35 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "AdminModel.hpp"
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
2016-04-19 06:07:12 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-03-31 02:25:35 -05:00
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Protocol.hpp>
|
|
|
|
#include <net/WebSocketHandler.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <Util.hpp>
|
2016-03-31 02:25:35 -05:00
|
|
|
|
2017-06-23 07:32:44 -05:00
|
|
|
void Document::addView(const std::string& sessionId, const std::string& userName, const std::string& userId)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-06-23 07:32:44 -05:00
|
|
|
const auto ret = _views.emplace(sessionId, View(sessionId, userName, userId));
|
2016-03-31 02:25:35 -05:00
|
|
|
if (!ret.second)
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_WRN("View with SessionID [" << sessionId << "] already exists.");
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-17 21:11:10 -05:00
|
|
|
++_activeViews;
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
int Document::expireView(const std::string& sessionId)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-04-14 15:29:31 -05:00
|
|
|
auto it = _views.find(sessionId);
|
2016-03-31 02:25:35 -05:00
|
|
|
if (it != _views.end())
|
|
|
|
{
|
|
|
|
it->second.expire();
|
2016-04-14 15:29:31 -05:00
|
|
|
|
|
|
|
// If last view, expire the Document also
|
2016-04-15 04:00:22 -05:00
|
|
|
if (--_activeViews == 0)
|
2016-04-14 15:29:31 -05:00
|
|
|
_end = std::time(nullptr);
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
2017-07-07 01:47:11 -05:00
|
|
|
takeSnapshot();
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
return _activeViews;
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
std::pair<std::time_t, std::string> Document::getSnapshot() const
|
|
|
|
{
|
|
|
|
std::time_t ct = std::time(nullptr);
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "{";
|
|
|
|
oss << "\"creationTime\"" << ":" << ct << ",";
|
2017-07-07 01:47:11 -05:00
|
|
|
oss << "\"memoryDirty\"" << ":" << getMemoryDirty() << ",";
|
|
|
|
oss << "\"activeViews\"" << ":" << getActiveViews() << ",";
|
2017-04-17 13:36:04 -05:00
|
|
|
|
|
|
|
oss << "\"views\"" << ":[";
|
2017-04-21 06:26:53 -05:00
|
|
|
std::string separator;
|
2017-07-07 01:47:11 -05:00
|
|
|
for (auto view : getViews())
|
2017-04-17 13:36:04 -05:00
|
|
|
{
|
|
|
|
oss << separator << "\"";
|
|
|
|
if(view.second.isExpired())
|
|
|
|
{
|
|
|
|
oss << "-";
|
|
|
|
}
|
|
|
|
oss << view.first << "\"";
|
|
|
|
separator = ",";
|
|
|
|
}
|
|
|
|
oss << "],";
|
|
|
|
|
2017-07-07 01:47:11 -05:00
|
|
|
oss << "\"lastActivity\"" << ":" << _lastActivity;
|
2017-04-17 13:36:04 -05:00
|
|
|
oss << "}";
|
|
|
|
return std::make_pair(ct, oss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Document::getHistory() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "{";
|
2017-07-07 01:47:11 -05:00
|
|
|
oss << "\"docKey\"" << ":\"" << _docKey << "\",";
|
|
|
|
oss << "\"filename\"" << ":\"" << getFilename() << "\",";
|
|
|
|
oss << "\"start\"" << ":" << _start << ",";
|
|
|
|
oss << "\"end\"" << ":" << _end << ",";
|
|
|
|
oss << "\"pid\"" << ":" << getPid() << ",";
|
2017-04-17 13:36:04 -05:00
|
|
|
oss << "\"snapshots\"" << ":[";
|
2017-04-21 06:26:53 -05:00
|
|
|
std::string separator;
|
2017-04-17 13:36:04 -05:00
|
|
|
for (auto s : _snapshots)
|
|
|
|
{
|
|
|
|
oss << separator << s.second;
|
|
|
|
separator = ",";
|
|
|
|
}
|
|
|
|
oss << "]}";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::takeSnapshot()
|
|
|
|
{
|
2017-07-07 01:47:11 -05:00
|
|
|
auto p = getSnapshot();
|
2017-04-17 13:36:04 -05:00
|
|
|
auto insPoint = _snapshots.upper_bound(p.first);
|
|
|
|
_snapshots.insert(insPoint, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Document::to_string() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::string encodedFilename;
|
2017-07-07 01:47:11 -05:00
|
|
|
Poco::URI::encode(getFilename(), " ", encodedFilename);
|
|
|
|
oss << getPid() << ' '
|
2017-04-17 13:36:04 -05:00
|
|
|
<< encodedFilename << ' '
|
2017-07-07 01:47:11 -05:00
|
|
|
<< getActiveViews() << ' '
|
|
|
|
<< getMemoryDirty() << ' '
|
|
|
|
<< getElapsedTime() << ' '
|
|
|
|
<< getIdleTime() << ' ';
|
2017-04-17 13:36:04 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
bool Subscriber::notify(const std::string& message)
|
|
|
|
{
|
2016-10-24 22:40:33 -05:00
|
|
|
// If there is no socket, then return false to
|
|
|
|
// signify we're disconnected.
|
2016-03-31 02:25:35 -05:00
|
|
|
auto webSocket = _ws.lock();
|
|
|
|
if (webSocket)
|
|
|
|
{
|
2016-10-24 22:40:33 -05:00
|
|
|
if (_subscriptions.find(LOOLProtocol::getFirstToken(message)) == _subscriptions.end())
|
|
|
|
{
|
|
|
|
// No subscribers for the given message.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
UnitWSD::get().onAdminNotifyMessage(message);
|
2017-03-29 19:03:01 -05:00
|
|
|
webSocket->sendMessage(message);
|
2016-10-22 09:24:27 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_ERR("Failed to notify Admin subscriber with message [" <<
|
|
|
|
message << "] due to [" << ex.what() << "].");
|
2016-10-22 09:24:27 -05:00
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
|
|
|
|
return false;
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
bool Subscriber::subscribe(const std::string& command)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
auto ret = _subscriptions.insert(command);
|
|
|
|
return ret.second;
|
|
|
|
}
|
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
void Subscriber::unsubscribe(const std::string& command)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
_subscriptions.erase(command);
|
|
|
|
}
|
|
|
|
|
2017-04-05 07:48:49 -05:00
|
|
|
void AdminModel::assertCorrectThread() const
|
2017-04-03 12:21:20 -05:00
|
|
|
{
|
|
|
|
// FIXME: share this code [!]
|
|
|
|
const bool sameThread = std::this_thread::get_id() == _owner;
|
|
|
|
if (!sameThread)
|
2017-04-05 07:48:49 -05:00
|
|
|
LOG_ERR("Admin command invoked from foreign thread. Expected: 0x" << std::hex <<
|
2017-04-03 12:21:20 -05:00
|
|
|
_owner << " but called from 0x" << std::this_thread::get_id() << " (" <<
|
|
|
|
std::dec << Util::getThreadId() << ").");
|
|
|
|
|
2017-04-05 07:48:49 -05:00
|
|
|
assert(sameThread);
|
2017-04-03 12:21:20 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
AdminModel::~AdminModel()
|
|
|
|
{
|
2017-06-25 20:37:57 -05:00
|
|
|
LOG_DBG("History:\n\n" << getAllHistory() << '\n');
|
|
|
|
LOG_INF("AdminModel dtor.");
|
2017-04-17 13:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string AdminModel::getAllHistory() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "{ \"documents\" : [";
|
2017-04-21 06:26:53 -05:00
|
|
|
std::string separator1;
|
2017-06-25 20:37:57 -05:00
|
|
|
for (const auto& d : _documents)
|
2017-04-17 13:36:04 -05:00
|
|
|
{
|
|
|
|
oss << separator1;
|
|
|
|
oss << d.second.getHistory();
|
|
|
|
separator1 = ",";
|
|
|
|
}
|
|
|
|
oss << "], \"expiredDocuments\" : [";
|
|
|
|
separator1 = "";
|
2017-06-25 20:37:57 -05:00
|
|
|
for (const auto& ed : _expiredDocuments)
|
2017-04-17 13:36:04 -05:00
|
|
|
{
|
|
|
|
oss << separator1;
|
|
|
|
oss << ed.second.getHistory();
|
|
|
|
separator1 = ",";
|
|
|
|
}
|
|
|
|
oss << "]}";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-04-12 01:09:39 -05:00
|
|
|
std::string AdminModel::query(const std::string& command)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
const auto token = LOOLProtocol::getFirstToken(command);
|
|
|
|
if (token == "documents")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return getDocuments();
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "active_users_count")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return std::to_string(getTotalActiveViews());
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "active_docs_count")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return std::to_string(_documents.size());
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "mem_stats")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return getMemStats();
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "mem_stats_size")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return std::to_string(_memStatsSize);
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "cpu_stats")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return getCpuStats();
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
else if (token == "cpu_stats_size")
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
|
|
|
return std::to_string(_cpuStatsSize);
|
|
|
|
}
|
2017-06-09 20:28:16 -05:00
|
|
|
else if (token == "sent_activity")
|
|
|
|
{
|
|
|
|
return getSentActivity();
|
|
|
|
}
|
|
|
|
else if (token == "recv_activity")
|
|
|
|
{
|
|
|
|
return getRecvActivity();
|
|
|
|
}
|
|
|
|
else if (token == "net_stats_size")
|
|
|
|
{
|
|
|
|
return std::to_string(std::max(_sentStatsSize, _recvStatsSize));
|
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
|
|
|
|
return std::string("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns memory consumed by all active loolkit processes
|
2017-01-29 23:01:49 -06:00
|
|
|
unsigned AdminModel::getKitsMemoryUsage()
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
unsigned totalMem = 0;
|
2017-01-29 23:01:49 -06:00
|
|
|
unsigned docs = 0;
|
2017-01-15 12:32:07 -06:00
|
|
|
for (const auto& it : _documents)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-10-22 09:24:27 -05:00
|
|
|
if (!it.second.isExpired())
|
|
|
|
{
|
2017-02-07 08:39:56 -06:00
|
|
|
const auto bytes = it.second.getMemoryDirty();
|
2017-01-29 23:01:49 -06:00
|
|
|
if (bytes > 0)
|
|
|
|
{
|
|
|
|
totalMem += bytes;
|
|
|
|
++docs;
|
|
|
|
}
|
2016-10-22 09:24:27 -05:00
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 07:10:10 -06:00
|
|
|
if (docs > 0)
|
|
|
|
{
|
2017-02-03 00:29:53 -06:00
|
|
|
LOG_TRC("Got total Kits memory of " << totalMem << " bytes for " << docs <<
|
|
|
|
" docs, avg: " << static_cast<double>(totalMem) / docs << " bytes / doc.");
|
2017-01-30 07:10:10 -06:00
|
|
|
}
|
2017-01-29 23:01:49 -06:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
return totalMem;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t AdminModel::getKitsJiffies()
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
size_t totalJ = 0;
|
|
|
|
for (auto& it : _documents)
|
|
|
|
{
|
|
|
|
if (!it.second.isExpired())
|
|
|
|
{
|
|
|
|
const auto pid = it.second.getPid();
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
unsigned newJ = Util::getCpuUsage(pid);
|
|
|
|
unsigned prevJ = it.second.getLastJiffies();
|
|
|
|
if(newJ >= prevJ)
|
|
|
|
{
|
|
|
|
totalJ += (newJ - prevJ);
|
|
|
|
it.second.setLastJiffies(newJ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalJ;
|
|
|
|
}
|
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
void AdminModel::subscribe(int sessionId, const std::weak_ptr<WebSocketHandler>& ws)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-11-10 02:41:33 -06:00
|
|
|
const auto ret = _subscribers.emplace(sessionId, Subscriber(ws));
|
2016-03-31 02:25:35 -05:00
|
|
|
if (!ret.second)
|
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_WRN("Subscriber already exists");
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void AdminModel::subscribe(int sessionId, const std::string& command)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
auto subscriber = _subscribers.find(sessionId);
|
2016-10-22 09:24:27 -05:00
|
|
|
if (subscriber != _subscribers.end())
|
|
|
|
{
|
|
|
|
subscriber->second.subscribe(command);
|
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void AdminModel::unsubscribe(int sessionId, const std::string& command)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
auto subscriber = _subscribers.find(sessionId);
|
2016-10-22 09:24:27 -05:00
|
|
|
if (subscriber != _subscribers.end())
|
|
|
|
subscriber->second.unsubscribe(command);
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminModel::addMemStats(unsigned memUsage)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
_memStats.push_back(memUsage);
|
|
|
|
if (_memStats.size() > _memStatsSize)
|
|
|
|
_memStats.pop_front();
|
|
|
|
|
2016-10-24 22:40:33 -05:00
|
|
|
notify("mem_stats " + std::to_string(memUsage));
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminModel::addCpuStats(unsigned cpuUsage)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
_cpuStats.push_back(cpuUsage);
|
|
|
|
if (_cpuStats.size() > _cpuStatsSize)
|
|
|
|
_cpuStats.pop_front();
|
|
|
|
|
2016-10-24 22:40:33 -05:00
|
|
|
notify("cpu_stats " + std::to_string(cpuUsage));
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 20:28:16 -05:00
|
|
|
void AdminModel::addSentStats(uint64_t sent)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
_sentStats.push_back(sent);
|
|
|
|
if (_sentStats.size() > _sentStatsSize)
|
|
|
|
_sentStats.pop_front();
|
|
|
|
|
|
|
|
notify("sent_activity " + std::to_string(sent));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdminModel::addRecvStats(uint64_t recv)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
_recvStats.push_back(recv);
|
|
|
|
if (_recvStats.size() > _recvStatsSize)
|
|
|
|
_recvStats.pop_front();
|
|
|
|
|
|
|
|
notify("recv_activity " + std::to_string(recv));
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
void AdminModel::setCpuStatsSize(unsigned size)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
int wasteValuesLen = _cpuStats.size() - size;
|
|
|
|
while (wasteValuesLen-- > 0)
|
|
|
|
{
|
|
|
|
_cpuStats.pop_front();
|
|
|
|
}
|
|
|
|
_cpuStatsSize = size;
|
|
|
|
|
2016-10-24 22:40:33 -05:00
|
|
|
notify("settings cpu_stats_size=" + std::to_string(_cpuStatsSize));
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminModel::setMemStatsSize(unsigned size)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
int wasteValuesLen = _memStats.size() - size;
|
|
|
|
while (wasteValuesLen-- > 0)
|
|
|
|
{
|
|
|
|
_memStats.pop_front();
|
|
|
|
}
|
|
|
|
_memStatsSize = size;
|
|
|
|
|
2016-10-24 22:40:33 -05:00
|
|
|
notify("settings mem_stats_size=" + std::to_string(_memStatsSize));
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminModel::notify(const std::string& message)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-10-24 22:40:33 -05:00
|
|
|
if (!_subscribers.empty())
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_TRC("Message to admin console: " << message);
|
2016-10-24 22:40:33 -05:00
|
|
|
for (auto it = std::begin(_subscribers); it != std::end(_subscribers); )
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-10-24 22:40:33 -05:00
|
|
|
if (!it->second.notify(message))
|
|
|
|
{
|
|
|
|
it = _subscribers.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
void AdminModel::addBytes(const std::string& docKey, uint64_t sent, uint64_t recv)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
auto doc = _documents.find(docKey);
|
|
|
|
if(doc != _documents.end())
|
|
|
|
doc->second.addBytes(sent, recv);
|
|
|
|
|
|
|
|
_sentBytesTotal += sent;
|
|
|
|
_recvBytesTotal += recv;
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:59:01 -05:00
|
|
|
void AdminModel::modificationAlert(const std::string& docKey, Poco::Process::PID pid, bool value)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
auto doc = _documents.find(docKey);
|
2018-01-02 20:33:52 -06:00
|
|
|
if (doc != _documents.end())
|
2017-05-12 08:59:01 -05:00
|
|
|
doc->second.setModified(value);
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "modifications "
|
|
|
|
<< pid << ' '
|
|
|
|
<< (value?"Yes":"No");
|
|
|
|
|
|
|
|
notify(oss.str());
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
void AdminModel::addDocument(const std::string& docKey, Poco::Process::PID pid,
|
2017-04-07 09:19:04 -05:00
|
|
|
const std::string& filename, const std::string& sessionId,
|
2017-06-23 07:32:44 -05:00
|
|
|
const std::string& userName, const std::string& userId)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
const auto ret = _documents.emplace(docKey, Document(docKey, pid, filename));
|
2017-04-17 13:36:04 -05:00
|
|
|
ret.first->second.takeSnapshot();
|
2017-06-23 07:32:44 -05:00
|
|
|
ret.first->second.addView(sessionId, userName, userId);
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_DBG("Added admin document [" << docKey << "].");
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2017-04-07 09:19:04 -05:00
|
|
|
std::string encodedUsername;
|
2016-04-19 06:07:12 -05:00
|
|
|
std::string encodedFilename;
|
2017-06-23 07:32:44 -05:00
|
|
|
std::string encodedUserId;
|
|
|
|
Poco::URI::encode(userId, " ", encodedUserId);
|
2016-04-19 06:07:12 -05:00
|
|
|
Poco::URI::encode(filename, " ", encodedFilename);
|
2017-04-07 09:19:04 -05:00
|
|
|
Poco::URI::encode(userName, " ", encodedUsername);
|
2017-02-03 00:29:53 -06:00
|
|
|
|
|
|
|
// Notify the subscribers
|
|
|
|
std::ostringstream oss;
|
2016-04-17 21:11:10 -05:00
|
|
|
oss << "adddoc "
|
2016-10-24 22:40:33 -05:00
|
|
|
<< pid << ' '
|
|
|
|
<< encodedFilename << ' '
|
2017-04-07 09:19:04 -05:00
|
|
|
<< sessionId << ' '
|
2017-06-23 07:32:44 -05:00
|
|
|
<< encodedUsername << ' '
|
|
|
|
<< encodedUserId << ' ';
|
2017-02-03 00:29:53 -06:00
|
|
|
|
|
|
|
// We have to wait until the kit sends us its PSS.
|
|
|
|
// Here we guestimate until we get an update.
|
|
|
|
if (_documents.size() < 2) // If we aren't the only one.
|
|
|
|
{
|
|
|
|
if (_memStats.empty())
|
|
|
|
{
|
|
|
|
oss << 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Estimate half as much as wsd+forkit.
|
|
|
|
oss << _memStats.front() / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-07 08:39:56 -06:00
|
|
|
oss << _documents.begin()->second.getMemoryDirty();
|
2017-02-03 00:29:53 -06:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
notify(oss.str());
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2016-04-17 21:11:10 -05:00
|
|
|
void AdminModel::removeDocument(const std::string& docKey, const std::string& sessionId)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-04-15 04:00:22 -05:00
|
|
|
auto docIt = _documents.find(docKey);
|
2016-04-14 15:29:31 -05:00
|
|
|
if (docIt != _documents.end() && !docIt->second.isExpired())
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-04-14 15:29:31 -05:00
|
|
|
// Notify the subscribers
|
|
|
|
std::ostringstream oss;
|
2016-04-17 21:11:10 -05:00
|
|
|
oss << "rmdoc "
|
2016-10-24 22:40:33 -05:00
|
|
|
<< docIt->second.getPid() << ' '
|
2016-04-14 15:29:31 -05:00
|
|
|
<< sessionId;
|
|
|
|
notify(oss.str());
|
|
|
|
|
2017-04-17 13:36:04 -05:00
|
|
|
// The idea is to only expire the document and keep the history
|
2016-03-31 02:25:35 -05:00
|
|
|
// of documents open and close, to be able to give a detailed summary
|
2017-04-17 13:36:04 -05:00
|
|
|
// to the admin console with views.
|
2016-04-14 15:29:31 -05:00
|
|
|
if (docIt->second.expireView(sessionId) == 0)
|
|
|
|
{
|
2017-04-17 13:36:04 -05:00
|
|
|
_expiredDocuments.emplace(*docIt);
|
2016-04-14 15:29:31 -05:00
|
|
|
_documents.erase(docIt);
|
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
void AdminModel::removeDocument(const std::string& docKey)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
auto docIt = _documents.find(docKey);
|
|
|
|
if (docIt != _documents.end())
|
|
|
|
{
|
2016-11-23 21:42:27 -06:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "rmdoc "
|
|
|
|
<< docIt->second.getPid() << ' ';
|
|
|
|
const std::string msg = oss.str();
|
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
for (const auto& pair : docIt->second.getViews())
|
|
|
|
{
|
|
|
|
// Notify the subscribers
|
2016-11-23 21:42:27 -06:00
|
|
|
notify(msg + pair.first);
|
2017-04-17 13:36:04 -05:00
|
|
|
docIt->second.expireView(pair.first);
|
2016-04-17 21:32:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 07:10:10 -06:00
|
|
|
LOG_DBG("Removed admin document [" << docKey << "].");
|
2017-04-17 13:36:04 -05:00
|
|
|
_expiredDocuments.emplace(*docIt);
|
2016-04-17 21:32:58 -05:00
|
|
|
_documents.erase(docIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
std::string AdminModel::getMemStats()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
std::ostringstream oss;
|
2016-11-23 21:42:27 -06:00
|
|
|
for (const auto& i: _memStats)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-10-22 09:24:27 -05:00
|
|
|
oss << i << ',';
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
return oss.str();
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string AdminModel::getCpuStats()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
std::ostringstream oss;
|
2016-11-23 21:42:27 -06:00
|
|
|
for (const auto& i: _cpuStats)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-10-22 09:24:27 -05:00
|
|
|
oss << i << ',';
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 20:28:16 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AdminModel::getSentActivity()
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
for (const auto& i: _sentStats)
|
|
|
|
{
|
|
|
|
oss << i << ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AdminModel::getRecvActivity()
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
for (const auto& i: _recvStats)
|
|
|
|
{
|
|
|
|
oss << i << ',';
|
|
|
|
}
|
|
|
|
|
2016-10-22 09:24:27 -05:00
|
|
|
return oss.str();
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned AdminModel::getTotalActiveViews()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
unsigned numTotalViews = 0;
|
2016-11-23 21:42:27 -06:00
|
|
|
for (const auto& it: _documents)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-08-21 07:23:08 -05:00
|
|
|
if (!it.second.isExpired())
|
|
|
|
{
|
2017-01-13 06:52:08 -06:00
|
|
|
numTotalViews += it.second.getActiveViews();
|
2016-08-21 07:23:08 -05:00
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
return numTotalViews;
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-07-07 06:42:19 -05:00
|
|
|
std::list<DocBasicInfo> AdminModel::getDocumentsSortedByIdle() const
|
|
|
|
{
|
|
|
|
std::list<DocBasicInfo> docList;
|
|
|
|
for (const auto& it: _documents)
|
|
|
|
{
|
|
|
|
docList.emplace_back(it.second.getDocKey(),
|
|
|
|
it.second.getIdleTime(),
|
|
|
|
!it.second.getModifiedStatus(),
|
|
|
|
it.second.getMemoryDirty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the list by idle times;
|
|
|
|
docList.sort([](const DocBasicInfo& a, const DocBasicInfo& b) {
|
|
|
|
return a._idleTime > b._idleTime;
|
|
|
|
});
|
|
|
|
|
|
|
|
return docList;
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:12:09 -06:00
|
|
|
std::string AdminModel::getDocuments() const
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
std::ostringstream oss;
|
2017-04-07 09:19:04 -05:00
|
|
|
std::map<std::string, View> viewers;
|
|
|
|
oss << '{' << "\"documents\"" << ':' << '[';
|
2017-04-21 06:26:53 -05:00
|
|
|
std::string separator1;
|
2016-11-09 20:12:09 -06:00
|
|
|
for (const auto& it: _documents)
|
2016-03-31 02:25:35 -05:00
|
|
|
{
|
2016-11-09 20:12:09 -06:00
|
|
|
if (!it.second.isExpired())
|
|
|
|
{
|
2017-04-07 09:19:04 -05:00
|
|
|
std::string encodedFilename;
|
|
|
|
Poco::URI::encode(it.second.getFilename(), " ", encodedFilename);
|
|
|
|
oss << separator1 << '{' << ' '
|
|
|
|
<< "\"pid\"" << ':' << it.second.getPid() << ','
|
|
|
|
<< "\"fileName\"" << ':' << '"' << encodedFilename << '"' << ','
|
|
|
|
<< "\"activeViews\"" << ':' << it.second.getActiveViews() << ','
|
|
|
|
<< "\"memory\"" << ':' << it.second.getMemoryDirty() << ','
|
|
|
|
<< "\"elapsedTime\"" << ':' << it.second.getElapsedTime() << ','
|
|
|
|
<< "\"idleTime\"" << ':' << it.second.getIdleTime() << ','
|
2017-05-12 08:59:01 -05:00
|
|
|
<< "\"modified\"" << ':' << '"' << (it.second.getModifiedStatus() ? "Yes" : "No") << '"' << ','
|
2017-04-07 09:19:04 -05:00
|
|
|
<< "\"views\"" << ':' << '[';
|
|
|
|
viewers = it.second.getViews();
|
2017-04-21 06:26:53 -05:00
|
|
|
std::string separator;
|
2017-04-07 09:19:04 -05:00
|
|
|
for(const auto& viewIt: viewers)
|
|
|
|
{
|
|
|
|
if(!viewIt.second.isExpired()) {
|
|
|
|
oss << separator << '{'
|
|
|
|
<< "\"userName\"" << ':' << '"' << viewIt.second.getUserName() << '"' << ','
|
2017-06-23 07:32:44 -05:00
|
|
|
<< "\"userId\"" << ':' << '"' << viewIt.second.getUserId() << '"' << ','
|
2017-04-07 09:19:04 -05:00
|
|
|
<< "\"sessionid\"" << ':' << '"' << viewIt.second.getSessionId() << '"' << '}';
|
|
|
|
separator = ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oss << "]"
|
|
|
|
<< "}";
|
|
|
|
separator1 = ',';
|
2016-11-09 20:12:09 -06:00
|
|
|
}
|
2016-03-31 02:25:35 -05:00
|
|
|
}
|
2017-04-07 09:19:04 -05:00
|
|
|
oss << "]" << "}";
|
2016-03-31 02:25:35 -05:00
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void AdminModel::updateLastActivityTime(const std::string& docKey)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
auto docIt = _documents.find(docKey);
|
|
|
|
if (docIt != _documents.end())
|
|
|
|
{
|
|
|
|
if (docIt->second.getIdleTime() >= 10)
|
|
|
|
{
|
2017-04-17 13:36:04 -05:00
|
|
|
docIt->second.takeSnapshot(); // I would like to keep the idle time
|
2016-12-05 14:11:07 -06:00
|
|
|
docIt->second.updateLastActivityTime();
|
|
|
|
notify("resetidle " + std::to_string(docIt->second.getPid()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 12:52:01 -06:00
|
|
|
bool Document::updateMemoryDirty(int dirty)
|
|
|
|
{
|
|
|
|
if (_memoryDirty == dirty)
|
|
|
|
return false;
|
|
|
|
_memoryDirty = dirty;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-07 08:39:56 -06:00
|
|
|
void AdminModel::updateMemoryDirty(const std::string& docKey, int dirty)
|
2017-02-03 00:29:53 -06:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 12:21:20 -05:00
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
auto docIt = _documents.find(docKey);
|
2017-02-07 12:52:01 -06:00
|
|
|
if (docIt != _documents.end() &&
|
|
|
|
docIt->second.updateMemoryDirty(dirty))
|
|
|
|
{
|
|
|
|
notify("propchange " + std::to_string(docIt->second.getPid()) +
|
|
|
|
" mem " + std::to_string(dirty));
|
|
|
|
}
|
2017-02-03 00:29:53 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 02:25:35 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|