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/ledger/CachedSLEs.h>
27#include <xrpld/nodestore/Database.h>
28#include <xrpld/rpc/Context.h>
29#include <xrpl/basics/UptimeClock.h>
30#include <xrpl/json/json_value.h>
31#include <xrpl/protocol/ErrorCodes.h>
32#include <xrpl/protocol/RPCErr.h>
33#include <xrpl/protocol/jss.h>
34
35namespace ripple {
36
37static void
39 std::string& text,
41 const char* unitName,
43{
44 auto i = seconds.time_since_epoch() / unitVal;
45
46 if (i == 0)
47 return;
48
49 seconds -= unitVal * i;
50
51 if (!text.empty())
52 text += ", ";
53
54 text += std::to_string(i);
55 text += " ";
56 text += unitName;
57
58 if (i > 1)
59 text += "s";
60}
61
63getCountsJson(Application& app, int minObjectCount)
64{
65 auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
66
68
69 for (auto const& [k, v] : objectCounts)
70 {
71 ret[k] = v;
72 }
73
74 if (app.config().useTxTables())
75 {
76 auto const db =
77 dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
78
79 if (!db)
80 Throw<std::runtime_error>("Failed to get relational database");
81
82 auto dbKB = db->getKBUsedAll();
83
84 if (dbKB > 0)
85 ret[jss::dbKBTotal] = dbKB;
86
87 dbKB = db->getKBUsedLedger();
88
89 if (dbKB > 0)
90 ret[jss::dbKBLedger] = dbKB;
91
92 dbKB = db->getKBUsedTransaction();
93
94 if (dbKB > 0)
95 ret[jss::dbKBTransaction] = dbKB;
96
97 {
99 if (c > 0)
100 ret[jss::local_txs] = static_cast<Json::UInt>(c);
101 }
102 }
103
104 ret[jss::write_load] = app.getNodeStore().getWriteLoad();
105
106 ret[jss::historical_perminute] =
107 static_cast<int>(app.getInboundLedgers().fetchRate());
108 ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
109 ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
110 ret[jss::AL_size] = Json::UInt(app.getAcceptedLedgerCache().size());
111 ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
112
113 ret[jss::fullbelow_size] =
114 static_cast<int>(app.getNodeFamily().getFullBelowCache()->size());
115 ret[jss::treenode_cache_size] =
116 app.getNodeFamily().getTreeNodeCache()->getCacheSize();
117 ret[jss::treenode_track_size] =
118 app.getNodeFamily().getTreeNodeCache()->getTrackSize();
119
120 std::string uptime;
121 auto s = UptimeClock::now();
122 using namespace std::chrono_literals;
123 textTime(uptime, s, "year", 365 * 24h);
124 textTime(uptime, s, "day", 24h);
125 textTime(uptime, s, "hour", 1h);
126 textTime(uptime, s, "minute", 1min);
127 textTime(uptime, s, "second", 1s);
128 ret[jss::uptime] = uptime;
129
130 app.getNodeStore().getCountsJson(ret);
131
132 return ret;
133}
134
135// {
136// min_count: <number> // optional, defaults to 10
137// }
140{
141 int minCount = 10;
142
143 if (context.params.isMember(jss::min_count))
144 minCount = context.params[jss::min_count].asUInt();
145
146 return getCountsJson(context.app, minCount);
147}
148
149} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
UInt asUInt() const
Definition: json_value.cpp:545
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
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:350
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:266
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
double rate() const
Returns the fraction of cache hits.
Definition: TaggedCache.h:475
static time_point now()
Definition: UptimeClock.cpp:63
T empty(T... args)
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
unsigned int UInt
Definition: json_forwards.h:27
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
static void textTime(std::string &text, UptimeClock::time_point &seconds, const char *unitName, std::chrono::seconds unitVal)
Definition: GetCounts.cpp:38
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:139
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition: GetCounts.cpp:63
Application & app
Definition: Context.h:42
Json::Value params
Definition: Context.h:64
T time_since_epoch(T... args)
T to_string(T... args)