rippled
Loading...
Searching...
No Matches
GetCounts.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2014 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/ledger/AcceptedLedger.h>
21#include <xrpld/app/ledger/InboundLedgers.h>
22#include <xrpld/app/ledger/LedgerMaster.h>
23#include <xrpld/app/main/Application.h>
24#include <xrpld/app/misc/NetworkOPs.h>
25#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
26#include <xrpld/rpc/Context.h>
27
28#include <xrpl/basics/UptimeClock.h>
29#include <xrpl/json/json_value.h>
30#include <xrpl/nodestore/Database.h>
31#include <xrpl/protocol/ErrorCodes.h>
32#include <xrpl/protocol/jss.h>
33
34namespace ripple {
35
36static void
38 std::string& text,
40 char const* unitName,
42{
43 auto i = seconds.time_since_epoch() / unitVal;
44
45 if (i == 0)
46 return;
47
48 seconds -= unitVal * i;
49
50 if (!text.empty())
51 text += ", ";
52
53 text += std::to_string(i);
54 text += " ";
55 text += unitName;
56
57 if (i > 1)
58 text += "s";
59}
60
62getCountsJson(Application& app, int minObjectCount)
63{
64 auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
65
67
68 for (auto const& [k, v] : objectCounts)
69 {
70 ret[k] = v;
71 }
72
73 if (app.config().useTxTables())
74 {
75 auto const db =
76 dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
77
78 if (!db)
79 Throw<std::runtime_error>("Failed to get relational database");
80
81 auto dbKB = db->getKBUsedAll();
82
83 if (dbKB > 0)
84 ret[jss::dbKBTotal] = dbKB;
85
86 dbKB = db->getKBUsedLedger();
87
88 if (dbKB > 0)
89 ret[jss::dbKBLedger] = dbKB;
90
91 dbKB = db->getKBUsedTransaction();
92
93 if (dbKB > 0)
94 ret[jss::dbKBTransaction] = dbKB;
95
96 {
98 if (c > 0)
99 ret[jss::local_txs] = static_cast<Json::UInt>(c);
100 }
101 }
102
103 ret[jss::write_load] = app.getNodeStore().getWriteLoad();
104
105 ret[jss::historical_perminute] =
106 static_cast<int>(app.getInboundLedgers().fetchRate());
107 ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
108 ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
109 ret[jss::AL_size] = Json::UInt(app.getAcceptedLedgerCache().size());
110 ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
111
112 ret[jss::fullbelow_size] =
113 static_cast<int>(app.getNodeFamily().getFullBelowCache()->size());
114 ret[jss::treenode_cache_size] =
115 app.getNodeFamily().getTreeNodeCache()->getCacheSize();
116 ret[jss::treenode_track_size] =
117 app.getNodeFamily().getTreeNodeCache()->getTrackSize();
118
119 std::string uptime;
120 auto s = UptimeClock::now();
121 using namespace std::chrono_literals;
122 textTime(uptime, s, "year", 365 * 24h);
123 textTime(uptime, s, "day", 24h);
124 textTime(uptime, s, "hour", 1h);
125 textTime(uptime, s, "minute", 1min);
126 textTime(uptime, s, "second", 1s);
127 ret[jss::uptime] = uptime;
128
129 app.getNodeStore().getCountsJson(ret);
130
131 return ret;
132}
133
134// {
135// min_count: <number> // optional, defaults to 10
136// }
139{
140 int minCount = 10;
141
142 if (context.params.isMember(jss::min_count))
143 minCount = context.params[jss::min_count].asUInt();
144
145 return getCountsJson(context.app, minCount);
146}
147
148} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
UInt asUInt() const
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual CachedSLEs & cachedSLEs()=0
virtual Config & config()=0
virtual NodeStore::Database & getNodeStore()=0
virtual TaggedCache< uint256, AcceptedLedger > & getAcceptedLedgerCache()=0
virtual NetworkOPs & getOPs()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual Family & getNodeFamily()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual RelationalDatabase & getRelationalDatabase()=0
bool useTxTables() const
Definition Config.h:342
static CountedObjects & getInstance() noexcept
List getCounts(int minimumThreshold) const
virtual std::shared_ptr< FullBelowCache > getFullBelowCache()=0
Return a pointer to the Family Full Below Cache.
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache()=0
Return a pointer to the Family Tree Node Cache.
virtual std::size_t fetchRate()=0
Returns the rate of historical ledger fetches per minute.
virtual std::size_t getLocalTxCount()=0
void getCountsJson(Json::Value &obj)
Definition Database.cpp:267
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
double rate() const
Returns the fraction of cache hits.
static time_point now()
T empty(T... args)
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
unsigned int UInt
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Json::Value doGetCounts(RPC::JsonContext &context)
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition GetCounts.cpp:62
static void textTime(std::string &text, UptimeClock::time_point &seconds, char const *unitName, std::chrono::seconds unitVal)
Definition GetCounts.cpp:37
Application & app
Definition Context.h:41
T time_since_epoch(T... args)
T to_string(T... args)