19 #ifndef PAGESPEED_KERNEL_BASE_STATISTICS_TEMPLATE_H_
20 #define PAGESPEED_KERNEL_BASE_STATISTICS_TEMPLATE_H_
34 namespace net_instaweb {
40 template<
class Var,
class UpDown,
class Hist,
46 STLDeleteContainerPointers(variables_.begin(), variables_.end());
47 STLDeleteContainerPointers(up_downs_.begin(), up_downs_.end());
48 STLDeleteContainerPointers(histograms_.begin(), histograms_.end());
49 STLDeleteContainerPointers(timed_vars_.begin(), timed_vars_.end());
58 variables_.push_back(var);
59 variable_names_.push_back(name.as_string());
60 variable_map_[name.as_string()] = var;
69 up_downs_.push_back(var);
70 up_down_names_.push_back(name.as_string());
71 up_down_map_[name.as_string()] = var;
80 up_downs_.push_back(var);
81 up_down_names_.push_back(name.as_string());
82 up_down_map_[name.as_string()] = var;
88 typename VarMap::const_iterator p = variable_map_.find(name.as_string());
90 if (p != variable_map_.end()) {
97 typename UpDownMap::const_iterator p = up_down_map_.find(name.as_string());
99 if (p != up_down_map_.end()) {
108 hist = NewHistogram(name);
109 histograms_.push_back(hist);
110 histogram_names_.push_back(name.as_string());
111 histogram_map_[name.as_string()] = hist;
117 typename HistMap::const_iterator p = histogram_map_.find(name.as_string());
119 if (p != histogram_map_.end()) {
126 const StringPiece& group) {
128 if (timedvar == NULL) {
129 timedvar = NewTimedVariable(name);
130 timed_vars_.push_back(timedvar);
131 timed_var_map_[name.as_string()] = timedvar;
132 timed_var_group_map_[group.as_string()].push_back(name.as_string());
138 typename TimedVarMap::const_iterator p =
139 timed_var_map_.find(name.as_string());
140 TimedVar* timedvar = NULL;
141 if (p != timed_var_map_.end()) {
142 timedvar = p->second;
148 return histogram_names_;
152 return timed_var_group_map_;
156 int longest_string = 0;
157 for (
int i = 0, n = variables_.size(); i < n; ++i) {
159 int length_number = Integer64ToString(variables_[i]->Get()).size();
160 int length_name = var_name.size();
161 longest_string = std::max(longest_string, length_name + length_number);
163 for (
int i = 0, n = up_downs_.size(); i < n; ++i) {
165 int length_number = Integer64ToString(up_downs_[i]->Get()).size();
166 int length_name = up_down_name.size();
167 longest_string = std::max(longest_string, length_name + length_number);
171 StringPiece spaces(spaces_buffer);
172 for (
int i = 0, n = variables_.size(); i < n; ++i) {
174 GoogleString var_as_str = Integer64ToString(variables_[i]->Get());
175 writer->Write(var_name, message_handler);
176 writer->Write(
": ", message_handler);
177 int num_spaces = longest_string - var_name.size() - var_as_str.size();
178 writer->Write(spaces.substr(0, num_spaces), message_handler);
179 writer->Write(var_as_str, message_handler);
180 writer->Write(
"\n", message_handler);
182 for (
int i = 0, n = up_downs_.size(); i < n; ++i) {
184 GoogleString up_down_as_str = Integer64ToString(up_downs_[i]->Get());
185 writer->Write(up_down_name, message_handler);
186 writer->Write(
": ", message_handler);
187 int num_spaces = longest_string - up_down_name.size() -
188 up_down_as_str.size();
189 writer->Write(spaces.substr(0, num_spaces), message_handler);
190 writer->Write(up_down_as_str, message_handler);
191 writer->Write(
"\n", message_handler);
198 int longest_string = 0;
199 writer->Write(
"{\"variables\": {", message_handler);
200 for (
int i = 0, n = variables_.size(); i < n; ++i) {
202 GoogleString var_as_str = Integer64ToString(variables_[i]->Get());
203 int length_name = var_name.size();
204 int length_number = var_as_str.size();
205 longest_string = std::max(longest_string, length_name + length_number);
206 writer->Write(StrCat(
"\"", var_name,
"\": ", var_as_str),
209 writer->Write(
",", message_handler);
212 for (
int i = 0, n = up_downs_.size(); i < n; ++i) {
214 GoogleString up_down_as_str = Integer64ToString(up_downs_[i]->Get());
215 int length_name = up_down_name.size();
216 int length_number = up_down_as_str.size();
217 longest_string = std::max(longest_string, length_name + length_number);
218 writer->Write(StrCat(
",\"", up_down_name,
"\": ", up_down_as_str),
221 writer->Write(
"}, \"maxlength\": ", message_handler);
222 writer->Write(Integer64ToString(longest_string), message_handler);
223 writer->Write(
"}", message_handler);
227 for (
int i = 0, n = variables_.size(); i < n; ++i) {
231 for (
int i = 0, n = up_downs_.size(); i < n; ++i) {
235 for (
int i = 0, n = histograms_.size(); i < n; ++i) {
239 for (
int i = 0, n = timed_vars_.size(); i < n; ++i) {
257 virtual Hist* NewHistogram(StringPiece name) = 0;
258 virtual TimedVar* NewTimedVariable(StringPiece name) = 0;
260 size_t variables_size()
const {
return variables_.size(); }
261 Var* variables(
size_t pos) {
return variables_.at(pos); }
263 size_t up_down_size()
const {
return up_downs_.size(); }
264 UpDown* up_downs(
size_t pos) {
return up_downs_.at(pos); }
266 size_t histograms_size()
const {
return histograms_.size(); }
267 Hist* histograms(
size_t pos) {
return histograms_.at(pos); }
270 return histogram_names_.at(pos);
274 typedef std::vector<Var*> VarVector;
275 typedef std::map<GoogleString, Var*> VarMap;
276 typedef std::vector<UpDown*> UpDownVector;
277 typedef std::map<GoogleString, UpDown*> UpDownMap;
278 typedef std::vector<Hist*> HistVector;
279 typedef std::map<GoogleString, Hist*> HistMap;
281 typedef std::vector<TimedVar*> TimedVarVector;
282 typedef std::map<GoogleString, TimedVar*> TimedVarMap;
283 VarVector variables_;
284 VarMap variable_map_;
285 UpDownVector up_downs_;
286 UpDownMap up_down_map_;
287 HistVector histograms_;
288 HistMap histogram_map_;
289 TimedVarVector timed_vars_;
290 TimedVarMap timed_var_map_;
292 std::map<GoogleString, StringVector> timed_var_group_map_;
293 StringVector variable_names_;
294 StringVector up_down_names_;
295 StringVector histogram_names_;
317 virtual int64 Get()
const {
return impl_.Get(); }
318 virtual StringPiece
GetName()
const {
return impl_.GetName(); }
319 virtual int64
AddHelper(int64 delta) {
return impl_.AddHelper(delta); }
320 virtual void Clear() { impl_.Set(0); }
322 Impl* impl() {
return &impl_; }
337 : impl_(name, stats) {}
339 virtual int64 Get()
const {
return impl_.Get(); }
340 virtual StringPiece
GetName()
const {
return impl_.GetName(); }
341 virtual void Set(int64 value) { impl_.Set(value); }
342 virtual int64
AddHelper(int64 delta) {
return impl_.AddHelper(delta); }
343 virtual void Clear() { impl_.Set(0); }
345 Impl* impl() {
return &impl_; }
356 class HistC = CountHistogram,
357 class TVarC = FakeTimedVariable>
373 return new Var(name,
this);
377 return new UpDown(name,
this);
380 virtual TVar* NewTimedVariable(StringPiece name) {
381 return new TVar(name,
this);
virtual const StringVector & HistogramNames()
Return the names of all the histograms for render.
Definition: statistics_template.h:147
virtual int64 AddHelper(int64 delta)
This is virtual so that subclasses can add platform-specific atomicity.
Definition: statistics_template.h:319
TimeDVariable.
Definition: statistics_template.h:358
virtual void Clear()=0
Throw away all data.
Definition: statistics.h:43
virtual void Dump(Writer *writer, MessageHandler *message_handler)
Dump the variable-values to a writer.
Definition: statistics_template.h:155
virtual StringPiece GetName() const
Definition: statistics_template.h:318
virtual UpDown * FindUpDownCounter(const StringPiece &name) const
Find a variable from a name, returning NULL if not found.
Definition: statistics_template.h:96
virtual Var * NewVariable(StringPiece name)=0
Interface to subclass.
virtual Hist * FindHistogram(const StringPiece &name) const
Find a histogram from a name, returning NULL if not found.
Definition: statistics_template.h:116
virtual Hist * AddHistogram(const StringPiece &name)
Definition: statistics_template.h:105
virtual Var * NewVariable(StringPiece name)
Interface to subclass.
Definition: statistics_template.h:372
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
virtual StringPiece GetName() const
Definition: statistics_template.h:340
virtual TimedVar * FindTimedVariable(const StringPiece &name) const
Find a TimedVariable from a name, returning NULL if not found.
Definition: statistics_template.h:137
virtual UpDown * NewGlobalUpDownCounter(StringPiece name)
Default implementation just calls NewUpDownCounter.
Definition: statistics_template.h:253
virtual UpDown * NewUpDownCounter(StringPiece name)
Interface to subclass.
Definition: statistics_template.h:376
TimedVariable implementation that only updates a basic UpDownCounter.
Definition: statistics.h:313
virtual void Clear()
Definition: statistics_template.h:226
virtual void Clear()=0
Throw away all data.
virtual UpDown * AddUpDownCounter(const StringPiece &name)
Definition: statistics_template.h:65
virtual TimedVar * AddTimedVariable(const StringPiece &name, const StringPiece &group)
Definition: statistics_template.h:125
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: statistics_template.h:313
virtual Var * AddVariable(const StringPiece &name)
Definition: statistics_template.h:54
virtual int64 AddHelper(int64 delta)
This is virtual so that subclasses can add platform-specific atomicity.
Definition: statistics_template.h:342
virtual UpDown * AddGlobalUpDownCounter(const StringPiece &name)
Definition: statistics_template.h:76
Trivial implementation. But Count() returns a meaningful value.
Definition: statistics.h:255
Interface for writing bytes to an output stream.
Definition: writer.h:29
Definition: statistics_template.h:41
VarTemplate< Impl > Var
Add typedefs for template class args to make them visible to subclasses.
Definition: statistics_template.h:363
virtual const std::map< GoogleString, StringVector > & TimedVariableMap()
Return the map of groupnames and names of all timedvariables for render.
Definition: statistics_template.h:151
Definition: statistics.h:138
virtual UpDown * NewUpDownCounter(StringPiece name)=0
Interface to subclass.
virtual void DumpJson(Writer *writer, MessageHandler *message_handler)
Definition: statistics_template.h:197
virtual Var * FindVariable(const StringPiece &name) const
Find a variable from a name, returning NULL if not found.
Definition: statistics_template.h:87
Definition: statistics.h:73
Definition: message_handler.h:39
Definition: statistics.h:298
Definition: statistics_template.h:334