comparison OrthancFramework/Sources/MetricsRegistry.cpp @ 5335:c103b0e70d75

refactoring MetricsRegistry::Item
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Jun 2023 15:42:07 +0200
parents fbe857e942cd
children dd9795dc380d
comparison
equal deleted inserted replaced
5334:5b2a5cc64cb1 5335:c103b0e70d75
41 MetricsType type_; 41 MetricsType type_;
42 boost::posix_time::ptime time_; 42 boost::posix_time::ptime time_;
43 bool hasValue_; 43 bool hasValue_;
44 int64_t value_; 44 int64_t value_;
45 45
46 void Touch(int64_t value, 46 void SetValue(int64_t value,
47 const boost::posix_time::ptime& now) 47 const boost::posix_time::ptime& now)
48 { 48 {
49 hasValue_ = true; 49 hasValue_ = true;
50 value_ = value; 50 value_ = value;
51 time_ = now; 51 time_ = now;
52 } 52 }
53 53
54 void Touch(int64_t value) 54 bool IsLargerOverPeriod(int64_t value,
55 { 55 int duration,
56 Touch(value, GetNow()); 56 const boost::posix_time::ptime& now) const
57 }
58
59 void UpdateMax(int64_t value,
60 int duration)
61 { 57 {
62 if (hasValue_) 58 if (hasValue_)
63 { 59 {
64 const boost::posix_time::ptime now = GetNow(); 60 return (value > value_ ||
65 61 (now - time_).total_seconds() > duration /* old value has expired */);
66 if (value > value_ ||
67 (now - time_).total_seconds() > duration)
68 {
69 Touch(value, now);
70 }
71 } 62 }
72 else 63 else
73 { 64 {
74 Touch(value); 65 return true; // No value yet
75 } 66 }
76 } 67 }
77 68
78 void UpdateMin(int64_t value, 69 bool IsSmallerOverPeriod(int64_t value,
79 int duration) 70 int duration,
71 const boost::posix_time::ptime& now) const
80 { 72 {
81 if (hasValue_) 73 if (hasValue_)
82 { 74 {
83 const boost::posix_time::ptime now = GetNow(); 75 return (value < value_ ||
84 76 (now - time_).total_seconds() > duration /* old value has expired */);
85 if (value < value_ ||
86 (now - time_).total_seconds() > duration)
87 {
88 Touch(value, now);
89 }
90 } 77 }
91 else 78 else
92 { 79 {
93 Touch(value); 80 return true; // No value yet
94 } 81 }
95 } 82 }
96 83
97 public: 84 public:
98 explicit Item(MetricsType type) : 85 explicit Item(MetricsType type) :
107 return type_; 94 return type_;
108 } 95 }
109 96
110 void Update(int64_t value) 97 void Update(int64_t value)
111 { 98 {
99 const boost::posix_time::ptime now = GetNow();
100
112 switch (type_) 101 switch (type_)
113 { 102 {
114 case MetricsType_Default: 103 case MetricsType_Default:
115 Touch(value); 104 SetValue(value, now);
116 break; 105 break;
117 106
118 case MetricsType_MaxOver10Seconds: 107 case MetricsType_MaxOver10Seconds:
119 UpdateMax(value, 10); 108 if (IsLargerOverPeriod(value, 10, now))
109 {
110 SetValue(value, now);
111 }
120 break; 112 break;
121 113
122 case MetricsType_MaxOver1Minute: 114 case MetricsType_MaxOver1Minute:
123 UpdateMax(value, 60); 115 if (IsLargerOverPeriod(value, 60, now))
116 {
117 SetValue(value, now);
118 }
124 break; 119 break;
125 120
126 case MetricsType_MinOver10Seconds: 121 case MetricsType_MinOver10Seconds:
127 UpdateMin(value, 10); 122 if (IsSmallerOverPeriod(value, 10, now))
123 {
124 SetValue(value, now);
125 }
128 break; 126 break;
129 127
130 case MetricsType_MinOver1Minute: 128 case MetricsType_MinOver1Minute:
131 UpdateMin(value, 60); 129 if (IsSmallerOverPeriod(value, 60, now))
130 {
131 SetValue(value, now);
132 }
132 break; 133 break;
133 134
134 default: 135 default:
135 throw OrthancException(ErrorCode_NotImplemented); 136 throw OrthancException(ErrorCode_NotImplemented);
136 } 137 }