-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdiag_down_graph.cpp
133 lines (109 loc) · 3.42 KB
/
diag_down_graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "diag_down_graph.h"
#include <cloud/blockstore/libs/diagnostics/critical_events.h>
#include <library/cpp/resource/resource.h>
#include <util/generic/vector.h>
#include <util/string/printf.h>
namespace {
//////////////////////////////////8//////////////////////////////////////////////
constexpr float WIDTH = 1024.0;
constexpr float HEIGHT = 50.0;
constexpr auto MAX_DURATION = TDuration::Hours(1);
constexpr std::string_view UNKNOWN_STATE_COLOR = "#777777";
constexpr std::string_view OK_STATE_COLOR = "#5cb85c";
constexpr std::string_view FAIL_STATE_COLOR = "#d9534f";
} // namespace
////////////////////////////////////////////////////////////////////////////////
namespace NCloud::NBlockStore {
////////////////////////////////////////////////////////////////////////////////
TSvgWithDownGraph::TSvgWithDownGraph(IOutputStream& str)
: MaxWidth(WIDTH)
, MaxDuration(MAX_DURATION)
, Str(str)
{
Init();
}
TSvgWithDownGraph::TSvgWithDownGraph(
IOutputStream& str,
const TDuration& maxDuration,
float width)
: MaxWidth(width)
, MaxDuration(maxDuration)
, Str(str)
{
Init();
}
TSvgWithDownGraph::~TSvgWithDownGraph()
{
if (SvgHeader.empty() || SvgFooter.empty() || SvgElement.empty()) {
Str << "Svg templates not found. " << ProcessedEvents
<< " events were processed";
return;
}
auto message = (PrevDate ? PrevDate.ToString() : "Start") + " - Now";
Str << Sprintf(
SvgElement.data(),
message.c_str(),
PrevX,
0.0,
MaxWidth - PrevX,
HEIGHT,
ColorForState(PrevIsDown).data());
Str << SvgFooter.data();
}
void TSvgWithDownGraph::Init()
{
try {
SvgHeader = NResource::Find("diag_graph_svg_header.xml");
SvgFooter = NResource::Find("diag_graph_svg_footer.xml");
SvgElement = NResource::Find("diag_graph_svg_graph_element.xml");
Str << Sprintf(SvgHeader.data(), MaxWidth, HEIGHT, MaxWidth, HEIGHT);
} catch (const yexception& ex) {
ReportMonitoringSvgTemplatesNotFound(
Sprintf("Svg templates not found: %s", ex.what()));
SvgHeader = "";
SvgFooter = "";
SvgElement = "";
}
}
void TSvgWithDownGraph::AddEvent(const TInstant& time, bool isDown)
{
auto now = TInstant::Now();
auto start = now - MaxDuration;
if (time < start) {
PrevIsDown = isDown ? EState::Fail : EState::Ok;
PrevDate = TInstant::Zero();
return;
}
ProcessedEvents++;
if (SvgHeader.empty() || SvgFooter.empty() || SvgElement.empty()) {
return;
}
auto x = MaxWidth * (time - start).Seconds() / (now - start).Seconds();
auto message =
(PrevDate ? PrevDate.ToString() : "Start") + " - " + time.ToString();
Str << Sprintf(
SvgElement.data(),
message.c_str(),
PrevX,
0.0f,
x - PrevX,
HEIGHT,
ColorForState(PrevIsDown).data());
PrevIsDown = isDown ? EState::Fail : EState::Ok;
PrevDate = time;
PrevX = x;
}
std::string_view TSvgWithDownGraph::ColorForState(EState state)
{
switch (state) {
case EState::Unknown:
return UNKNOWN_STATE_COLOR;
case EState::Ok:
return OK_STATE_COLOR;
case EState::Fail:
return FAIL_STATE_COLOR;
}
Y_DEBUG_ABORT_UNLESS(false, "Unknown graph state");
return UNKNOWN_STATE_COLOR;
}
} // namespace NCloud::NBlockStore