|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Pavel Siska <[email protected]> |
| 4 | + * @author Richard Plny <[email protected]> |
| 5 | + * @brief ALF classifier implementation |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: BSD-3-Clause |
| 8 | + */ |
| 9 | + |
| 10 | +#include "wif/classifiers/alfClassifier.hpp" |
| 11 | + |
| 12 | +namespace WIF { |
| 13 | + |
| 14 | +AlfClassifier::AlfCallback::AlfCallback(const std::string& modelPath, AlfClassifier& classifier) |
| 15 | + : m_fileModificationChecker(modelPath) |
| 16 | + , m_classifier(classifier) |
| 17 | +{ |
| 18 | +} |
| 19 | + |
| 20 | +void AlfClassifier::AlfCallback::onTick() |
| 21 | +{ |
| 22 | + if (m_fileModificationChecker.isChangeDetected()) { |
| 23 | + m_classifier.markModelAsObsolete(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +AlfClassifier::AlfClassifier( |
| 28 | + ScikitMlClassifier& mlClassifier, |
| 29 | + UnirecReporter& reporter, |
| 30 | + unsigned timerIntervalInSeconds) |
| 31 | + : m_mlClassifier(mlClassifier) |
| 32 | + , m_reporter(reporter) |
| 33 | + , m_timer( |
| 34 | + timerIntervalInSeconds, |
| 35 | + std::make_unique<AlfCallback>(mlClassifier.getMlModelPath(), *this)) |
| 36 | +{ |
| 37 | + updateLastModelLoadTime(); |
| 38 | + m_timer.start(); |
| 39 | +} |
| 40 | + |
| 41 | +ClfResult AlfClassifier::classify(const FlowFeatures& flowFeatures) |
| 42 | +{ |
| 43 | + return classify(std::vector<FlowFeatures> {flowFeatures})[0]; |
| 44 | +} |
| 45 | + |
| 46 | +std::vector<ClfResult> |
| 47 | +AlfClassifier::classify(const std::vector<FlowFeatures>& burstOfFlowsFeatures) |
| 48 | +{ |
| 49 | + if (m_modelReloadNeeded) { |
| 50 | + handleModelUpdate(); |
| 51 | + } |
| 52 | + |
| 53 | + auto burstOfResults = m_mlClassifier.classify(burstOfFlowsFeatures); |
| 54 | + for (unsigned flowIdx = 0; flowIdx < burstOfResults.size(); ++flowIdx) { |
| 55 | + auto& flowFeatures = burstOfFlowsFeatures[flowIdx]; |
| 56 | + auto& results = burstOfResults[flowIdx]; |
| 57 | + handleSingleReport(flowFeatures, results); |
| 58 | + } |
| 59 | + |
| 60 | + return burstOfResults; |
| 61 | +} |
| 62 | + |
| 63 | +void AlfClassifier::updateLastModelLoadTime() |
| 64 | +{ |
| 65 | + const auto timestamp = std::chrono::system_clock::now(); |
| 66 | + m_lastModelLoadTime |
| 67 | + = std::chrono::duration_cast<std::chrono::seconds>(timestamp.time_since_epoch()).count(); |
| 68 | +} |
| 69 | + |
| 70 | +void AlfClassifier::handleModelUpdate() |
| 71 | +{ |
| 72 | + m_mlClassifier.reloadModelFromDisk(); |
| 73 | + m_modelReloadNeeded = false; |
| 74 | + updateLastModelLoadTime(); |
| 75 | +} |
| 76 | + |
| 77 | +void AlfClassifier::handleSingleReport(const FlowFeatures flowFeatures, const ClfResult& result) |
| 78 | +{ |
| 79 | + m_reporter.onRecordStart(); |
| 80 | + |
| 81 | + // Identification fields are reported first |
| 82 | + for (const auto id : sourceFeatureIDs()) { |
| 83 | + m_reporter.report(flowFeatures.getRaw(id)); |
| 84 | + } |
| 85 | + |
| 86 | + // ML features are reported as second |
| 87 | + for (const auto id : m_mlClassifier.sourceFeatureIDs()) { |
| 88 | + m_reporter.report(flowFeatures.getRaw(id)); |
| 89 | + } |
| 90 | + |
| 91 | + // Report last time ML model was reloaded |
| 92 | + m_reporter.report({m_lastModelLoadTime}); |
| 93 | + |
| 94 | + // ML proba array is reported as the last one |
| 95 | + m_reporter.report(result.get<std::vector<double>>()); |
| 96 | + |
| 97 | + m_reporter.onRecordEnd(); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace WIF |
0 commit comments