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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <stats/observation.h>
#include <util/chrono_time_utils.h>
#include <cc/data.h>

#include <chrono><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace std::chrono;
using namespace isc::data;
using namespace isc::util;

namespace isc {
namespace stats {

std::pair<bool, uint32_t>
Observation::default_max_sample_count_ = std::make_pair(true, 20);

std::pair<bool, StatsDuration>
Observation::default_max_sample_age_ =
    std::make_pair(false, StatsDuration::zero());

Observation::Observation(const std::string& name, const int64_t value) :
    name_(name), type_(STAT_INTEGER),
    max_sample_count_(default_max_sample_count_),
    max_sample_age_(default_max_sample_age_) {
    setValue(value);
}

Observation::Observation(const std::string& name, const int128_t& value) :
    name_(name), type_(STAT_BIG_INTEGER),
    max_sample_count_(default_max_sample_count_),
    max_sample_age_(default_max_sample_age_) {
    setValue(value);
}

Observation::Observation(const std::string& name, const double value) :
    name_(name), type_(STAT_FLOAT),
    max_sample_count_(default_max_sample_count_),
    max_sample_age_(default_max_sample_age_) {
    setValue(value);
}

Observation::Observation(const std::string& name, const StatsDuration& value) :
    name_(name), type_(STAT_DURATION),
    max_sample_count_(default_max_sample_count_),
    max_sample_age_(default_max_sample_age_) {
    setValue(value);
}

Observation::Observation(const std::string& name, const std::string& value) :
    name_(name), type_(STAT_STRING),
    max_sample_count_(default_max_sample_count_),
    max_sample_age_(default_max_sample_age_) {
    setValue(value);
}

void Observation::setMaxSampleAge(const StatsDuration& duration) {
    switch(type_) {
    case STAT_INTEGER: {
        setMaxSampleAgeInternal(integer_samples_, duration, STAT_INTEGER);
        return;
    }
    case STAT_BIG_INTEGER: {
        setMaxSampleAgeInternal(big_integer_samples_, duration, STAT_BIG_INTEGER);
        return;
    }
    case STAT_FLOAT: {
        setMaxSampleAgeInternal(float_samples_, duration, STAT_FLOAT);
        return;
    }
    case STAT_DURATION: {
        setMaxSampleAgeInternal(duration_samples_, duration, STAT_DURATION);
        return;
    }
    case STAT_STRING: {
        setMaxSampleAgeInternal(string_samples_, duration, STAT_STRING);
        return;
    }
    default:
        isc_throw(InvalidStatType, "Unknown statistic type: "
                  << typeToText(type_));
    };
}

void Observation::setMaxSampleCount(uint32_t max_samples) {
    switch(type_) {
    case STAT_INTEGER: {
        setMaxSampleCountInternal(integer_samples_, max_samples, STAT_INTEGER);
        return;
    }
    case STAT_BIG_INTEGER: {
        setMaxSampleCountInternal(big_integer_samples_, max_samples, STAT_BIG_INTEGER);
        return;
    }
    case STAT_FLOAT: {
        setMaxSampleCountInternal(float_samples_, max_samples, STAT_FLOAT);
        return;
    }
    case STAT_DURATION: {
        setMaxSampleCountInternal(duration_samples_, max_samples, STAT_DURATION);
        return;
    }
    case STAT_STRING: {
        setMaxSampleCountInternal(string_samples_, max_samples, STAT_STRING);
        return;
    }
    default:
        isc_throw(InvalidStatType, "Unknown statistic type: "
                  << typeToText(type_));
    };
}

void Observation::addValue(const int64_t value) {
    IntegerSample current = getInteger();
    setValue(current.first + value);
}

void Observation::addValue(const int128_t& value) {
    BigIntegerSample current = getBigInteger();
    setValue(current.first + value);
}

void Observation::addValue(const double value) {
    FloatSample current = getFloat();
    setValue(current.first + value);
}

void Observation::addValue(const StatsDuration& value) {
    DurationSample current = getDuration();
    setValue(current.first + value);
}

void Observation::addValue(const std::string& value) {
    StringSample current = getString();
    setValue(current.first + value);
}

void Observation::setValue(const int64_t value) {
    setValueInternal(value, integer_samples_, STAT_INTEGER);
}

void Observation::setValue(const int128_t& value) {
    setValueInternal(value, big_integer_samples_, STAT_BIG_INTEGER);
}

void Observation::setValue(const double value) {
    setValueInternal(value, float_samples_, STAT_FLOAT);
}

void Observation::setValue(const StatsDuration& value) {
    setValueInternal(value, duration_samples_, STAT_DURATION);
}

void Observation::setValue(const std::string& value) {
    setValueInternal(value, string_samples_, STAT_STRING);
}

size_t Observation::getSize() const {
    size_t size = 0;
    switch(type_) {
    case STAT_INTEGER: {
        size = getSizeInternal(integer_samples_, STAT_INTEGER);
        return (size);
    }
    case STAT_BIG_INTEGER: {
        size = getSizeInternal(big_integer_samples_, STAT_BIG_INTEGER);
        return (size);
    }
    case STAT_FLOAT: {
        size = getSizeInternal(float_samples_, STAT_FLOAT);
        return (size);
    }
    case STAT_DURATION: {
        size = getSizeInternal(duration_samples_, STAT_DURATION);
        return (size);
    }
    case STAT_STRING: {
        size = getSizeInternal(string_samples_, STAT_STRING);
        return (size);
    }
    default:
        isc_throw(InvalidStatType, "Unknown statistic type: "
                  << typeToText(type_));
    };
    return (size);
}

std::pair<bool, StatsDuration> Observation::getMaxSampleAge() const {
    return (max_sample_age_);
}

std::pair<bool, uint32_t> Observation::getMaxSampleCount() const {
    return (max_sample_count_);
}

template<typename StorageType>
size_t Observation::getSizeInternal(StorageType& storage, Type exp_type) const {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    } else {
        return (storage.size());
    }
    return (0); // to avoid compilation error
}

template<typename SampleType, typename StorageType>
void Observation::setValueInternal(SampleType value, StorageType& storage,
                                   Type exp_type) {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    }

    if (storage.empty()) {
        storage.push_back(make_pair(value, SampleClock::now()));
    } else {
        // Storing of more than one sample
        storage.push_front(make_pair(value, SampleClock::now()));

        if (max_sample_count_.first) {
            // if max_sample_count_ is set to true
            // and size of storage is equal to max_sample_count_
            if (storage.size() > max_sample_count_.second) {
                storage.pop_back(); // removing the last element
            }
        } else {
            StatsDuration range_of_storage =
                storage.front().second - storage.back().second;
            // removing samples until the range_of_storage
            // stops exceeding the duration limit
            while (range_of_storage > max_sample_age_.second) {
                storage.pop_back();
                range_of_storage =
                    storage.front().second - storage.back().second;
            }
        }
    }
}

IntegerSample Observation::getInteger() const {
    return (getValueInternal<IntegerSample>(integer_samples_, STAT_INTEGER));
}

BigIntegerSample Observation::getBigInteger() const {
    return (getValueInternal<BigIntegerSample>(big_integer_samples_, STAT_BIG_INTEGER));
}

FloatSample Observation::getFloat() const {
    return (getValueInternal<FloatSample>(float_samples_, STAT_FLOAT));
}

DurationSample Observation::getDuration() const {
    return (getValueInternal<DurationSample>(duration_samples_, STAT_DURATION));
}

StringSample Observation::getString() const {
    return (getValueInternal<StringSample>(string_samples_, STAT_STRING));
}

template<typename SampleType, typename Storage>
SampleType Observation::getValueInternal(Storage& storage, Type exp_type) const {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    }

    if (storage.empty()) {
        // That should never happen. The first element is always initialized in
        // the constructor. reset() sets its value to zero, but the element should
        // still be there.
        isc_throw(Unexpected, "Observation storage container empty");
    }
    return (*storage.begin());
}

std::list<IntegerSample> Observation::getIntegers() const {
    return (getValuesInternal<IntegerSample>(integer_samples_, STAT_INTEGER));
}

std::list<BigIntegerSample> Observation::getBigIntegers() const {
    return (getValuesInternal<BigIntegerSample>(big_integer_samples_, STAT_BIG_INTEGER));
}

std::list<FloatSample> Observation::getFloats() const {
    return (getValuesInternal<FloatSample>(float_samples_, STAT_FLOAT));
}

std::list<DurationSample> Observation::getDurations() const {
    return (getValuesInternal<DurationSample>(duration_samples_, STAT_DURATION));
}

std::list<StringSample> Observation::getStrings() const {
    return (getValuesInternal<StringSample>(string_samples_, STAT_STRING));
}

template<typename SampleType, typename Storage>
std::list<SampleType> Observation::getValuesInternal(Storage& storage,
                                                     Type exp_type) const {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    }

    if (storage.empty()) {
        // That should never happen. The first element is always initialized in
        // the constructor. reset() sets its value to zero, but the element should
        // still be there.
        isc_throw(Unexpected, "Observation storage container empty");
    }
    return (storage);
}

template<typename StorageType>
void Observation::setMaxSampleAgeInternal(StorageType& storage,
                                          const StatsDuration& duration,
                                          Type exp_type) {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    }
    // setting new value of max_sample_age_
    max_sample_age_.first = true;
    max_sample_age_.second = duration;
    // deactivating the max_sample_count_ limit
    max_sample_count_.first = false;

    StatsDuration range_of_storage =
        storage.front().second - storage.back().second;

    while (range_of_storage > duration) {
        // deleting elements which are exceeding the duration limit
        storage.pop_back();
        range_of_storage = storage.front().second - storage.back().second;
    }
}

template<typename StorageType>
void Observation::setMaxSampleCountInternal(StorageType& storage,
                                            uint32_t max_samples,
                                            Type exp_type) {
    if (type_ != exp_type) {
        isc_throw(InvalidStatType, "Invalid statistic type requested: "
                  << typeToText(exp_type) << ", but the actual type is "
                  << typeToText(type_));
    }
    // Should we refuse the max_samples = 0 value here?
    // setting new value of max_sample_count_
    max_sample_count_.first = true;
    max_sample_count_.second = max_samples;
    // deactivating the max_sample_age_ limit
    max_sample_age_.first = false;

    while (storage.size() > max_samples) {
        // deleting elements which are exceeding the max_samples limit
        storage.pop_back();
    }
}

void Observation::setMaxSampleAgeDefault(const StatsDuration& duration) {
    // setting new value of default_max_sample_age_
    default_max_sample_age_.second = duration;
}

void Observation::setMaxSampleCountDefault(uint32_t max_samples) {
    if (max_samples == 0) {
        // deactivating the default_max_sample_count_ limit
        default_max_sample_count_.first = false;
        default_max_sample_age_.first = true;
    } else {
        // setting new value of default_max_sample_count_
        default_max_sample_count_.second = max_samples;
        // deactivating the default_max_sample_age_ limit
        default_max_sample_age_.first = false;
        default_max_sample_count_.first = true;
    }
}

const StatsDuration& Observation::getMaxSampleAgeDefault() {
    return (default_max_sample_age_.second);
}

uint32_t Observation::getMaxSampleCountDefault() {
    if (default_max_sample_count_.first) {
        return (default_max_sample_count_.second);
    } else {
        return (0);
    }
}

std::string Observation::typeToText(Type type) {
    std::stringstream tmp;
    switch (type) {
    case STAT_INTEGER:
        tmp << "integer";
        break;
    case STAT_BIG_INTEGER:
        tmp << "big integer";
        break;
    case STAT_FLOAT:
        tmp << "float";
        break;
    case STAT_DURATION:
        tmp << "duration";
        break;
    case STAT_STRING:
        tmp << "string";
        break;
    default:
        tmp << "unknown";
        break;
    }
    tmp << "(" << type << ")";
    return (tmp.str());
}

isc::data::ConstElementPtr
Observation::getJSON() const {
    ElementPtr list = isc::data::Element::createList(); // multiple observations
    ElementPtr entry;
    ElementPtr value;
    ElementPtr timestamp;

    // Support for retrieving more than one sample
    // retrieving all samples of indicated observation
    switch (type_) {
    case STAT_INTEGER: {
        std::list<IntegerSample> s = getIntegers(); // List of all integer samples

        // Iteration over all elements in the list
        // and adding alternately value and timestamp to the entry
        for (auto const& it : s) {
            entry = isc::data::Element::createList();
            value = isc::data::Element::create(static_cast<int64_t>(it.first));
            timestamp = isc::data::Element::create(isc::util::clockToText(it.second));

            entry->add(value);
            entry->add(timestamp);

            list->add(entry);
        }
        break;
    }
    case STAT_BIG_INTEGER: {
        std::list<BigIntegerSample> const& samples(getBigIntegers());

        // Iterate over all elements in the list and alternately add
        // value and timestamp to the entry.
        for (BigIntegerSample const& i : samples) {
            entry = isc::data::Element::createList();
            value = isc::data::Element::create(i.first);
            timestamp = isc::data::Element::create(isc::util::clockToText(i.second));

            entry->add(value);
            entry->add(timestamp);

            list->add(entry);
        }
        break;
    }
    case STAT_FLOAT: {
        std::list<FloatSample> s = getFloats();

        // Iteration over all elements in the list
        // and adding alternately value and timestamp to the entry
        for (auto const& it : s) {
            entry = isc::data::Element::createList();
            value = isc::data::Element::create(it.first);
            timestamp = isc::data::Element::create(isc::util::clockToText(it.second));

            entry->add(value);
            entry->add(timestamp);

            list->add(entry);
        }
        break;
    }
    case STAT_DURATION: {
        std::list<DurationSample> s = getDurations();

        // Iteration over all elements in the list
        // and adding alternately value and timestamp to the entry
        for (auto const& it : s) {
            entry = isc::data::Element::createList();
            value = isc::data::Element::create(isc::util::durationToText(it.first));
            timestamp = isc::data::Element::create(isc::util::clockToText(it.second));

            entry->add(value);
            entry->add(timestamp);

            list->add(entry);
        }
        break;
    }
    case STAT_STRING: {
        std::list<StringSample> s = getStrings();

        // Iteration over all elements in the list
        // and adding alternately value and timestamp to the entry
        for (auto const& it : s) {
            entry = isc::data::Element::createList();
            value = isc::data::Element::create(it.first);
            timestamp = isc::data::Element::create(isc::util::clockToText(it.second));

            entry->add(value);
            entry->add(timestamp);

            list->add(entry);
        }
        break;
    }
    default:
        isc_throw(InvalidStatType, "Unknown statistic type: "
                  << typeToText(type_));
    };

    return (list);
}

void Observation::reset() {
    switch(type_) {
    case STAT_INTEGER: {
        integer_samples_.clear();
        setValue(static_cast<int64_t>(0));
        return;
    }
    case STAT_BIG_INTEGER: {
        big_integer_samples_.clear();
        setValue(int128_t(0));
        return;
    }
    case STAT_FLOAT: {
        float_samples_.clear();
        setValue(0.0);
        return;
    }
    case STAT_DURATION: {
        duration_samples_.clear();
        setValue(StatsDuration::zero());
        return;
    }
    case STAT_STRING: {
        string_samples_.clear();
        setValue(string(""));
        return;
    }
    default:
        isc_throw(InvalidStatType, "Unknown statistic type: "
                  << typeToText(type_));
    };
}

} // end of namespace stats
} // end of namespace isc