Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
OperationalStatistics.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _StroikaSample_OperationalStatistics_h_
5#define _StroikaSample_OperationalStatistics_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
11
12/**
13 *
14 */
15
16namespace Stroika::Samples::HTMLUI {
17
18 using namespace Stroika;
19 using namespace Stroika::Foundation;
20
21 using Time::Duration;
22
23 /**
24 * Fully internally synchronized.
25 *
26 * Simple API to track recent application statistics.
27 */
29 public:
30 static OperationalStatisticsMgr sThe;
31
32 public:
33 static inline const Time::Duration kLookbackInterval{5min};
34
35 public:
36 enum DBCommandType {
37 eRead,
38 eWrite
39 };
40
41 public:
42 /**
43 */
44 class ProcessAPICmd;
45
46 public:
47 /**
48 */
49 class ProcessDBCmd;
50
51 public:
52 nonvirtual void RecordActiveRunningTasksCount (size_t length);
53
54 public:
55 nonvirtual void RecordOpenConnectionCount (size_t length);
56
57 public:
58 nonvirtual void RecordProcessingConnectionCount (size_t length);
59
60 public:
61 struct Statistics;
62
63 public:
64 /**
65 */
66 nonvirtual Statistics GetStatistics () const;
67
68 private:
69 mutable mutex fMutex_; // protect all data with single quick access mutex
70 struct Rec_ {
71 enum class Kind {
72 eNull,
73 eAPI,
74 eAPIError,
75 eAPIActiveRunningTasks,
76 eAPIOpenConnectionCount,
77 eAPIProcessingConnectionCount,
78 eDBRead,
79 eDBWrite,
80 eDBError,
81 };
82 Kind fKind;
84 Time::DurationSeconds fDuration;
85 size_t fLength;
86 };
87 Rec_ fRollingHistory_[1024]; // @todo see https://stroika.atlassian.net/browse/STK-174 - redo as circular q when available
88 size_t fNextHistory_{0}; // circular - can be < first. - first==last implies zero length q
89
90 void Add_ (const Rec_& r);
91 };
92 inline OperationalStatisticsMgr OperationalStatisticsMgr::sThe;
93
94 /**
95 */
96 class OperationalStatisticsMgr::ProcessAPICmd {
97 public:
98 ProcessAPICmd ();
99 ~ProcessAPICmd ();
100
101 public:
102 static void NoteError ();
103
104 private:
106 };
107
108 /**
109 */
110 class OperationalStatisticsMgr::ProcessDBCmd {
111 public:
112 ProcessDBCmd (DBCommandType cmdType);
113 ~ProcessDBCmd ();
114
115 public:
116 static void NoteError ();
117
118 private:
119 Rec_::Kind fKind_;
121 };
122
123 /**
124 */
125 struct OperationalStatisticsMgr::Statistics {
126 struct WSAPI {
127 unsigned int fCallsCompleted{};
128 optional<Duration> fMeanDuration;
129 optional<Duration> fMedianDuration;
130 optional<Duration> fMaxDuration;
131 optional<float> fMedianRunningAPITasks;
132 unsigned int fErrors{};
133 };
134 WSAPI fRecentAPI;
135
136 // Sample doesn't have a database, but often apps like this will, so include some sample stats about it.
137 struct DB {
138 unsigned int fReads{};
139 unsigned int fWrites{};
140 unsigned int fErrors{};
141 Math::CommonStatistics<Duration> fReadDurationStats;
142 Math::CommonStatistics<Duration> fWriteDurationStats;
143 };
144 DB fRecentDB;
145 };
146
147}
148
149/*
150 ********************************************************************************
151 ***************************** Implementation Details ***************************
152 ********************************************************************************
153 */
154#include "OperationalStatistics.inl"
155
156#endif /*_StroikaSample_OperationalStatistics_h_*/
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
Definition Realtime.h:82
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Definition Realtime.h:57
Duration is a chrono::duration<double> (=.
Definition Duration.h:96