|
| 1 | +#pragma once |
| 2 | +#include <chimbuko_config.h> |
| 3 | +#include "chimbuko/param.hpp" |
| 4 | +#include "chimbuko/param/hbos_param.hpp" |
| 5 | +#include "chimbuko/util/RunStats.hpp" |
| 6 | +#include <unordered_map> |
| 7 | +#include <nlohmann/json.hpp> |
| 8 | +#include <vector> |
| 9 | +#include <iostream> |
| 10 | + |
| 11 | +namespace chimbuko { |
| 12 | + |
| 13 | + |
| 14 | + /** |
| 15 | + * @@brief Implementation of ParamInterface for COPOD based anomaly detection |
| 16 | + */ |
| 17 | + class CopodParam : public ParamInterface { |
| 18 | + public: |
| 19 | + CopodParam(); |
| 20 | + ~CopodParam(); |
| 21 | + /** |
| 22 | + * @brief Clear all statistics |
| 23 | + */ |
| 24 | + void clear() override; |
| 25 | + |
| 26 | + |
| 27 | + const int find(const unsigned long& func_id); |
| 28 | + |
| 29 | + /** |
| 30 | + * @brief Get the internal map between global function index and statistics |
| 31 | + */ |
| 32 | + const std::unordered_map<unsigned long, Histogram> & get_hbosstats() const{ return m_copodstats; } |
| 33 | + |
| 34 | + /** |
| 35 | + * @brief Get the number of functions for which statistics are being collected |
| 36 | + */ |
| 37 | + size_t size() const override { return m_copodstats.size(); } |
| 38 | + |
| 39 | + /** |
| 40 | + * @brief Convert internal Histogram to string format for IO |
| 41 | + * @return Histogram in string format |
| 42 | + */ |
| 43 | + std::string serialize() const override; |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Update the internal Histogram with those included in the serialized input map |
| 47 | + * @param parameters The parameters in serialized format |
| 48 | + * @param return_update Controls return format |
| 49 | + * @return An empty string if return_update==False, otherwise the serialized updated parameters |
| 50 | + */ |
| 51 | + std::string update(const std::string& parameters, bool return_update=false) override; |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Set the internal Histogram to match those included in the serialized input map. Overwrite performed only for those keys in input. |
| 55 | + * @param parameters The serialized input map |
| 56 | + */ |
| 57 | + void assign(const std::string& parameters) override; |
| 58 | + |
| 59 | + void show(std::ostream& os) const override; |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief Set the internal Histogram to match those included in the input map. Overwrite performed only for those keys in input. |
| 63 | + * @param copodstats The input map between global function index and Histogram |
| 64 | + */ |
| 65 | + void assign(const std::unordered_map<unsigned long, Histogram>& copodstats); |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Convert a Histogram mapping into a Cereal portable binary representration |
| 69 | + * @param copodstats The Histogram mapping |
| 70 | + * @return Histogram in string format |
| 71 | + */ |
| 72 | + static std::string serialize_cerealpb(const std::unordered_map<unsigned long, Histogram>& copodstats); |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Get an element of the internal map |
| 76 | + * @param id The global function index |
| 77 | + */ |
| 78 | + Histogram& operator [](unsigned long id) { return m_copodstats[id]; } |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Convert a Histogram Cereal portable binary representation string into a map |
| 82 | + * @param[in] parameters The parameter string |
| 83 | + * @param[out] copodstats The map between global function index and histogram statistics |
| 84 | + */ |
| 85 | + static void deserialize_cerealpb(const std::string& parameters, |
| 86 | + std::unordered_map<unsigned long, Histogram>& copodstats); |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * @brief Update the internal histogram with those included in the input map |
| 91 | + * @param[in] copodstats The map between global function index and histogram |
| 92 | + */ |
| 93 | + void update(const std::unordered_map<unsigned long, Histogram>& copodstats); |
| 94 | + |
| 95 | + /** |
| 96 | + * @brief Update the internal Histogram with those included in another CopodParam instance. |
| 97 | + * @param[in] other The other CopodParam instance |
| 98 | + */ |
| 99 | + void update(const CopodParam& other) { update(other.m_copodstats); } |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Update the internal histogram with those included in the input map. Input map is then updated to reflect new state. |
| 103 | + * @param[in,out] copodstats The map between global function index and statistics |
| 104 | + */ |
| 105 | + void update_and_return(std::unordered_map<unsigned long, Histogram>& copodstats); |
| 106 | + |
| 107 | + /** |
| 108 | + * @brief Update the internal histogram with those included in another CopodParam instance. Other CopodParam is then updated to reflect new state. |
| 109 | + * @param[in,out] other The other CopodParam instance |
| 110 | + */ |
| 111 | + void update_and_return(CopodParam& other) { update_and_return(other.m_copodstats); } |
| 112 | + |
| 113 | + |
| 114 | + nlohmann::json get_algorithm_params(const unsigned long func_id) const override; |
| 115 | + |
| 116 | + private: |
| 117 | + std::unordered_map<unsigned long, Histogram> m_copodstats; /**< Map of func_id and corresponding Histogram*/ |
| 118 | + }; |
| 119 | + |
| 120 | + |
| 121 | +} // end of chimbuko namespace |
0 commit comments