13 #ifndef __DERIVED_STATS_H__
14 #define __DERIVED_STATS_H__
17 #include <boost/shared_ptr.hpp>
18 #include <boost/make_shared.hpp>
19 #include <boost/function.hpp>
34 template<
typename ElemT>
35 std::map<std::string, ElemT>
DerivedStatsAgg(
const std::map<std::string, ElemT> & raw,
36 std::map<std::string, ElemT> & agg,
37 const std::map<std::string, bool> &del) {
39 std::map<std::string, ElemT> diff;
41 typename std::map<std::string, ElemT>::const_iterator rit;
42 typename std::map<std::string, ElemT>::iterator ait;
45 for (rit=raw.begin(); rit!= raw.end(); rit++) {
46 ait=agg.find(rit->first);
49 diff[rit->first] = rit->second - ait->second;
52 if (!del.at(rit->first)) ait->second = ait->second + diff[rit->first];
55 diff[rit->first] = rit->second;
56 if (!del.at(rit->first)) agg[rit->first] = diff[rit->first];
63 template<
template<
class,
class>
class DSTT,
typename ElemT,
typename ResultT>
65 std::map<std::string, boost::shared_ptr<DSTT<ElemT,ResultT> > > & dsm,
66 std::string annotation,
const std::map<std::string, bool> &del,
69 std::map<std::string, bool> srcmap;
70 typename std::map<std::string, ElemT>::const_iterator rit;
71 std::map<std::string, bool>::const_iterator dlt;
79 for(rit=raw.begin(); rit!= raw.end(); rit++) {
80 srcmap[rit->first] =
false;
84 typename std::map<std::string,
85 boost::shared_ptr<DSTT<
86 ElemT,ResultT> > >::iterator dt = dsm.begin();
87 while (dt != dsm.end()) {
88 rit = raw.find(dt->first);
89 dlt = del.find(dt->first);
90 if (rit != raw.end()) {
91 assert(dlt!=del.end());
93 srcmap[rit->first] =
true;
96 typename std::map<std::string,
97 boost::shared_ptr<DSTT<
98 ElemT,ResultT> > >::iterator et = dt;
102 dt->second->Update(rit->second, mono_usec);
112 std::map<std::string, bool>::iterator mt;
113 for (mt = srcmap.begin(); mt != srcmap.end(); mt++) {
114 rit = raw.find(mt->first);
115 dlt = del.find(mt->first);
116 if ((mt->second ==
false) && (!dlt->second)) {
117 assert(dsm.find(mt->first) == dsm.end());
118 dsm[mt->first] = boost::make_shared<DSTT<ElemT,ResultT> >(annotation);
119 dsm[mt->first]->Update((raw.find(mt->first))->second, mono_usec);
124 template <
template<
class,
class>
class DSTT,
typename ElemT,
typename ResultT>
127 typedef std::map<std::string, boost::shared_ptr<DSTT<ElemT,ResultT> > >
result_map;
129 boost::shared_ptr<result_map>
dsm_;
131 boost::shared_ptr<DSTT<ElemT,ResultT> >
ds_;
146 template<
template<
class,
class>
class,
typename,
150 if ((!
ds_) && (!
dsm_))
return false;
156 void FillResult(ResultT &res,
bool& isset,
bool force=
false)
const {
160 if ((force && rt) || (!force && rt ==
DSR_OK)) {
166 void FillResult(std::map<std::string, ResultT> &mres,
bool& isset,
bool force=
false)
const {
169 for (
typename result_map::const_iterator dit =
dsm_->begin();
170 dit !=
dsm_->end(); dit++) {
173 if ((force && rt) || (!force && rt ==
DSR_OK)) {
174 mres.insert(std::make_pair(dit->first, res));
178 isset = !mres.empty();
184 void Update(ElemT raw, uint64_t mono_usec) {
198 else ds_->Update(raw, mono_usec);
201 void Update(
const std::map<std::string, ElemT> & raw,
202 const std::map<std::string, bool> &del, uint64_t mono_usec) {
204 dsm_ = boost::make_shared<result_map>();
210 DerivedStatsMerge<DSTT,ElemT,ResultT>(raw, *
dsm_,
annotation_, del, mono_usec);
215 template <
template<
class,
class>
class DSTT,
typename ElemT,
216 typename SubResultT,
typename ResultT>
219 typedef std::map<std::string, boost::shared_ptr<DSTT<ElemT,SubResultT> > >
result_map;
220 boost::shared_ptr<result_map>
dsm_;
221 boost::shared_ptr< std::map<std::string,ResultT> >
dsm_cache_;
223 boost::shared_ptr<DSTT<ElemT,SubResultT> >
ds_;
247 void Update(ElemT raw, uint64_t mono_usec) {
260 }
else ds_->Update(raw, mono_usec);
264 void Update(
const std::map<std::string, ElemT> & raw,
265 const std::map<std::string, bool> &del, uint64_t mono_usec) {
267 dsm_ = boost::make_shared<result_map>();
271 DerivedStatsMerge<DSTT,ElemT,SubResultT>(
273 }
else DerivedStatsMerge<DSTT,ElemT,SubResultT>(
286 ds_cache_ = boost::make_shared<ResultT>();
299 void FillResult(ResultT &res,
bool& isset,
bool force=
false)
const {
304 res.__isset.value =
true;
310 if ((force && rt) || (!force && rt ==
DSR_OK)) {
311 res.__isset.staging =
true;
317 bool Flush(
const std::map<std::string, ResultT> &mres) {
325 dsm_cache_ = boost::make_shared<std::map<std::string,ResultT> >();
326 for (
typename result_map::const_iterator dit =
dsm_->begin();
327 dit !=
dsm_->end(); dit++) {
329 if (dit->second->FillResult(res.value)) {
330 res.__isset.value =
true;
331 dsm_cache_->insert(std::make_pair(dit->first, res));
343 void FillResult(std::map<std::string, ResultT> &mres,
bool& isset,
bool force=
false)
const {
347 for (
typename std::map<std::string,ResultT>::const_iterator dit =
dsm_cache_->begin();
350 res.value = dit->second.value;
351 res.__isset.value =
true;
352 mres.insert(std::make_pair(dit->first, res));
356 for (
typename result_map::const_iterator dit = (*dsm_).begin();
357 dit != (*dsm_).end(); dit++) {
360 typename std::map<std::string, ResultT>::iterator wit =
361 mres.find(dit->first);
362 if (wit != mres.end()) {
364 dit->second->FillResult(wit->second.staging);
365 if ((force && rt) || (!force && rt ==
DSR_OK)) {
366 wit->second.__isset.staging =
true;
371 dit->second->FillResult(res.staging);
372 if ((force && rt) || (!force && rt ==
DSR_OK)) {
373 res.__isset.staging =
true;
374 mres.insert(std::make_pair(dit->first, res));
379 isset = !mres.empty();
391 template <
typename ElemT>
404 template <
template<
class,
class>
class DSTT,
typename ElemT,
405 template<
class,
class>
class PreT,
typename ResultT>
409 typedef std::map<std::string, boost::shared_ptr<DSTT<ElemT,ResultT> > >
result_map;
425 void Update(ElemT raw, uint64_t mono_usec) {
430 void Update(
const std::map<std::string, ElemT> & raw,
431 const std::map<std::string, bool> &del, uint64_t mono_usec) {
438 if (!
init_)
return false;
443 bool result_available;
444 periodic_.FillResult(sres, result_available);
453 bool Flush(
const std::map<std::string, ResultT> &mres) {
455 if (!
init_)
return false;
456 typename std::map<std::string, DSPeriodic<ElemT> > smres;
460 bool result_available;
461 periodic_.FillResult(smres, result_available);
464 typename std::map<std::string, ElemT> val;
465 std::map<std::string, bool> dmap;
469 dit = smres.begin(); dit != smres.end(); dit++) {
470 val.insert(std::make_pair(dit->first, dit->second.value));
471 dmap.insert(std::make_pair(dit->first,
false));
476 for (
typename result_map::const_iterator dit =
478 dit !=
anomaly_.dsm_->end(); dit++) {
479 if (val.find(dit->first) == val.end()) {
480 val.insert(std::make_pair(dit->first, zelem));
481 dmap.insert(std::make_pair(dit->first,
false));
492 void FillResult(ResultT &res,
bool& isset,
bool force=
false)
const {
493 anomaly_.FillResult(res, isset, force);
496 void FillResult(std::map<std::string, ResultT> &mres,
bool& isset,
bool force=
false)
const {
497 anomaly_.FillResult(mres, isset, force);
void FillResult(ResultT &res, bool &isset, bool force=false) const
bool Flush(const ResultT &res)
boost::shared_ptr< std::map< std::string, ResultT > > dsm_cache_
DerivedStatsIf(std::string annotation, bool is_agg=false)
void Update(const std::map< std::string, ElemT > &raw, const std::map< std::string, bool > &del, uint64_t mono_usec)
DerivedStatsPeriodicIf< PreT, ElemT, ElemT, DSPeriodic< ElemT > > periodic_
boost::shared_ptr< ResultT > ds_cache_
bool Flush(const ResultT &res)
boost::shared_ptr< DSTT< ElemT, SubResultT > > ds_
bool Flush(const std::map< std::string, ResultT > &mres)
std::map< std::string, ElemT > diffm_
DerivedStatsPeriodicIf(std::string annotation, bool is_agg=false)
bool Flush(const std::map< std::string, ResultT > &mres)
void Update(ElemT raw, uint64_t mono_usec)
DerivedStatsIf< DSTT, ElemT, ResultT > anomaly_
std::map< std::string, ElemT > aggm_
void Update(ElemT raw, uint64_t mono_usec)
std::map< std::string, boost::shared_ptr< DSTT< ElemT, ResultT > > > result_map
boost::shared_ptr< DSTT< ElemT, ResultT > > ds_
DerivedStatsPeriodicAnomalyIf(std::string annotation, bool is_agg=false)
bool IsResult(void) const
std::map< std::string, ElemT > aggm_
void Update(ElemT raw, uint64_t mono_usec)
bool IsResult(void) const
void Update(const std::map< std::string, ElemT > &raw, const std::map< std::string, bool > &del, uint64_t mono_usec)
void DerivedStatsMerge(const std::map< std::string, ElemT > &raw, std::map< std::string, boost::shared_ptr< DSTT< ElemT, ResultT > > > &dsm, std::string annotation, const std::map< std::string, bool > &del, uint64_t mono_usec)
std::map< std::string, ElemT > diffm_
void FillResult(std::map< std::string, ResultT > &mres, bool &isset, bool force=false) const
std::map< std::string, boost::shared_ptr< DSTT< ElemT, SubResultT > > > result_map
void Update(const std::map< std::string, ElemT > &raw, const std::map< std::string, bool > &del, uint64_t mono_usec)
std::map< std::string, ElemT > DerivedStatsAgg(const std::map< std::string, ElemT > &raw, std::map< std::string, ElemT > &agg, const std::map< std::string, bool > &del)
bool IsResult(void) const
void FillResult(ResultT &res, bool &isset, bool force=false) const
std::map< std::string, boost::shared_ptr< DSTT< ElemT, ResultT > > > result_map
void FillResult(std::map< std::string, ResultT > &mres, bool &isset, bool force=false) const
void FillResult(std::map< std::string, ResultT > &mres, bool &isset, bool force=false) const
boost::shared_ptr< result_map > dsm_
boost::shared_ptr< result_map > dsm_
void FillResult(ResultT &res, bool &isset, bool force=false) const