Skip to content

Commit

Permalink
Refactor with updated clang-tidy
Browse files Browse the repository at this point in the history
e Please enter the cmmmit message for your changes. Lines starting
  • Loading branch information
SiskaPavel committed Oct 3, 2024
1 parent 0c9304a commit e1174cb
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WarningsAsErrors: '*'

CheckOptions:
- key: readability-identifier-naming.NamespaceCase
value: 'CamelCase'
value: 'camelBack'
- key: readability-identifier-naming.StructCase
value: 'CamelCase'
- key: readability-identifier-naming.FunctionCase
Expand All @@ -45,6 +45,8 @@ CheckOptions:
value: 'camelBack'
- key: readability-identifier-naming.PrivateMemberPrefix
value: 'm_'
- key: readability-identifier-naming.ProtectedMemberPrefix
value: 'm_'
- key: readability-identifier-naming.ConstantMemberCase
value: 'UPPER_CASE'
- key: readability-identifier-naming.EnumConstantCase
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ ifeq ($(RUN_CLANG_TIDY),)
RUN_CLANG_TIDY := run-clang-tidy
endif

SOURCE_DIR = src/ include/
HEADE_FILTER = "$(shell pwd)/src|$(shell pwd)/include"
SOURCE_DIR = "$(shell pwd)/src" "$(shell pwd)/include"
SOURCE_REGEX = '.*\.\(cpp\|hpp\)'

.PHONY: all
Expand All @@ -40,11 +41,11 @@ format-fix:

.PHONY: tidy
tidy: all
$(RUN_CLANG_TIDY) -p build -quiet -j $(shell nproc) $(SOURCE_DIR)
$(RUN_CLANG_TIDY) -p build -quiet -j $(shell nproc) -header-filter=$(HEADE_FILTER) $(SOURCE_DIR)

.PHONY: tidy-fix
tidy-fix: all
$(RUN_CLANG_TIDY) -p build -quiet -fix -j $(shell nproc) $(SOURCE_DIR)
$(RUN_CLANG_TIDY) -p build -quiet -fix -j $(shell nproc) -header-filter=$(HEADE_FILTER) $(SOURCE_DIR)

.PHONY: test
test: build
Expand Down
2 changes: 1 addition & 1 deletion include/telemetry/aggFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AggregatedFile : public File {

FileOps getOps();

const std::string m_filesRegexPattern;
const std::string M_FILES_REGEX_PATTERN;

std::shared_ptr<Directory> m_patternRootDir;
std::vector<std::string> m_paths;
Expand Down
5 changes: 5 additions & 0 deletions include/telemetry/aggMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ enum class AggMethodType { AVG, SUM, JOIN };
*/
struct AggOperation {
AggMethodType method; ///< Aggregation method
// NOLINTNEXTLINE(readability-redundant-string-init)
std::string dictFieldName = ""; ///< Name of the field in the dictionary
// NOLINTNEXTLINE(readability-redundant-string-init)
std::string dictResultName = ""; ///< Name of the field in the aggregated dictionary
};

Expand Down Expand Up @@ -78,6 +80,9 @@ class AggMethod {
protected:
AggContent getAggContent(const Content& content, bool useDictResultName = false);

[[nodiscard]] std::string getDictResultName() const { return m_dictResultname; }

private:
std::string m_dictFieldName;
std::string m_dictResultname;
};
Expand Down
4 changes: 3 additions & 1 deletion include/telemetry/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ class Node : public std::enable_shared_from_this<Node> {
std::string getFullPath();

protected:
std::shared_ptr<Node> m_parent;
std::shared_ptr<Node> getParent() { return m_parent; };

private:
std::shared_ptr<Node> m_parent;

std::mutex m_mutex;
std::string m_name;

Expand Down
6 changes: 3 additions & 3 deletions src/telemetry/aggFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ Content AggregatedFile::read()
if (m_patternRootDir) {
patternRootDir = m_patternRootDir;
} else {
patternRootDir = std::dynamic_pointer_cast<Directory>(m_parent);
patternRootDir = std::dynamic_pointer_cast<Directory>(getParent());
}

const auto files = getFilesMatchingPattern(m_filesRegexPattern, patternRootDir);
const auto files = getFilesMatchingPattern(M_FILES_REGEX_PATTERN, patternRootDir);
if (files.empty()) {
return content;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ AggregatedFile::AggregatedFile(
const std::vector<AggOperation>& ops,
std::shared_ptr<Directory> patternRootDir)
: File(parent, name, getOps())
, m_filesRegexPattern(std::move(aggFilesPattern))
, M_FILES_REGEX_PATTERN(std::move(aggFilesPattern))
, m_patternRootDir(std::move(patternRootDir))
{
validateAggOperations(ops);
Expand Down
6 changes: 3 additions & 3 deletions src/telemetry/aggregator/aggCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static ScalarWithUnit getReferenceVariant(const std::vector<AggContent>& values)

if (std::holds_alternative<Array>(values.front())) {
for (const auto& value : values) {
const Array& array = std::get<Array>(value);
const auto& array = std::get<Array>(value);
if (!array.empty()) {
return {array.front(), ""};
}
Expand Down Expand Up @@ -102,7 +102,7 @@ static bool containsSameScalarAlternative(const std::vector<AggContent>& values)
return false;
}

size_t refIndex = refScalar.index();
const size_t refIndex = refScalar.index();

for (const auto& value : values) {
if (std::holds_alternative<Scalar>(value)) {
Expand All @@ -119,7 +119,7 @@ static bool containsSameScalarAlternative(const std::vector<AggContent>& values)
return false;
}
} else if (std::holds_alternative<Array>(value)) {
const Array& array = std::get<Array>(value);
const auto& array = std::get<Array>(value);
if (std::any_of(array.begin(), array.end(), [&](const auto& scalar) {
return scalar.index() != refIndex;
})) {
Expand Down
2 changes: 1 addition & 1 deletion src/telemetry/aggregator/aggJoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Content AggMethodJoin::aggregate(const std::vector<Content>& contents)
}

const auto& result = aggregateGatheredValues(values);
return createContent(m_dictResultname, result);
return createContent(getDictResultName(), result);
}

} // namespace telemetry
Expand Down
183 changes: 183 additions & 0 deletions src/telemetry/aggregator/aggMinMax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* @file
* @author Pavel Siska <[email protected]>
* @brief Implementaion of the MIN aggregation method for telemetry data.
*
* @note SPDX-License-Identifier: BSD-3-Clause
*/

#include "aggMinMax.hpp"

#include "aggCommon.hpp"

#include <telemetry/node.hpp>

namespace telemetry {

using ResultType = std::variant<Scalar, ScalarWithUnit>;

static void findMin(const Scalar& value, Scalar& result)
{
if (std::holds_alternative<std::monostate>(result)) {
result = value;
return;
}

if (std::holds_alternative<uint64_t>(value)) {
if (std::get<uint64_t>(value) < std::get<uint64_t>(result)) {
result = value;
}
} else if (std::holds_alternative<int64_t>(value)) {
if (std::get<int64_t>(value) < std::get<int64_t>(result)) {
result = value;
}
} else if (std::holds_alternative<double>(value)) {
if (std::get<double>(value) < std::get<double>(result)) {
result = value;
}
} else {
throw TelemetryException("Invalid scalar alternative type for min operation.");
}
}

static void findMax(const Scalar& value, Scalar& result)
{
if (std::holds_alternative<std::monostate>(result)) {
result = value;
return;
}

if (std::holds_alternative<uint64_t>(value)) {
if (std::get<uint64_t>(value) > std::get<uint64_t>(result)) {
result = value;
}
} else if (std::holds_alternative<int64_t>(value)) {
if (std::get<int64_t>(value) > std::get<int64_t>(result)) {
result = value;
}
} else if (std::holds_alternative<double>(value)) {
if (std::get<double>(value) > std::get<double>(result)) {
result = value;
}
} else {
throw TelemetryException("Invalid scalar alternative type for max operation.");
}
}

static Scalar
aggregateScalar(std::vector<AggContent>& values, const AggMethodMinMax::AggMethod& aggMethod)
{
Scalar result = std::monostate();

if (values.empty()) {
return result;
}

if (!std::holds_alternative<Scalar>(values.front())) {
throw TelemetryException("Unexpected variant alternative.");
}

for (const auto& value : values) {
const auto& scalar = std::get<Scalar>(value);
aggMethod(scalar, result);
}

return result;
}

static ScalarWithUnit aggregateScalarWithUnit(
std::vector<AggContent>& values,
const AggMethodMinMax::AggMethod& aggMethod)
{
Scalar result = std::monostate();

if (values.empty()) {
return {};
}

if (!std::holds_alternative<ScalarWithUnit>(values.front())) {
throw TelemetryException("Unexpected variant alternative.");
}

for (const auto& value : values) {
[[maybe_unused]] const auto& [scalar, _] = std::get<ScalarWithUnit>(value);
aggMethod(scalar, result);
}

[[maybe_unused]] const auto& [_, unit] = std::get<ScalarWithUnit>(values.front());

return {result, unit};
}

static ResultType aggregateGatheredValues(
std::vector<AggContent>& values,
const AggMethodMinMax::AggMethod& aggMethod)
{
if (std::holds_alternative<Scalar>(values.front())) {
return aggregateScalar(values, aggMethod);
}

if (std::holds_alternative<ScalarWithUnit>(values.front())) {
return aggregateScalarWithUnit(values, aggMethod);
}

throw TelemetryException("Unexpected variant alternative.");
}

static Content createDictContent(const std::string& dictKey, const ResultType& result)
{
Dict dict;

auto visitor = [&](const auto& arg) -> DictValue { return arg; };
dict[dictKey] = std::visit(visitor, result);

return dict;
}

static Content createContent(const std::string& dictKey, const ResultType& result)
{
if (!dictKey.empty()) {
return createDictContent(dictKey, result);
}

auto visitor = [&](const auto& arg) -> Content { return arg; };
return std::visit(visitor, result);
}

AggMethodMinMax::AggMethodMinMax(const AggMethodType& method)
{
if (method == AggMethodType::MIN) {
m_agregateFunction = findMin;
} else if (method == AggMethodType::MAX) {
m_agregateFunction = findMax;
} else {
throw TelemetryException("Invalid aggregation method.");
}
}

Content AggMethodMinMax::aggregate(const std::vector<Content>& contents)
{
std::vector<AggContent> values;

for (const auto& content : contents) {
const auto& aggContent = getAggContent(content);
values.emplace_back(aggContent);
}

if (!hasOneOfThisAlternative<ScalarWithUnit, Scalar>(values)) {
throw TelemetryException("The contents data does not contain the same variant alternative");
}

if (!hasValidScalarType<uint64_t, int64_t, double, std::monostate>(values)) {
throw TelemetryException("Invalid scalar variant alternative");
}

const auto& result = aggregateGatheredValues(values, m_agregateFunction);
return createContent(getDictResultName(), result);
}

} // namespace telemetry

#ifdef TELEMETRY_ENABLE_TESTS
#include "tests/testAggMinMax.cpp"
#endif
42 changes: 42 additions & 0 deletions src/telemetry/aggregator/aggMinMax.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file
* @author Pavel Siska <[email protected]>
* @brief Interface of the MIN aggregation method for telemetry data.
*
* @note SPDX-License-Identifier: BSD-3-Clause
*/

#pragma once

#include <telemetry/aggMethod.hpp>
#include <telemetry/content.hpp>

#include <functional>
#include <variant>
#include <vector>

namespace telemetry {

/**
* @brief Implementation of the MIN aggregation method.
*/
class AggMethodMinMax : public AggMethod {
public:
AggMethodMinMax(const AggMethodType& method);

/**
* @brief Aggregate telemetry data using the MIN method.
*
* @param contents The vector of telemetry content to aggregate.
* @return The aggregated content.
* @throws TelemetryException if the aggregation encounters an error.
*/
Content aggregate(const std::vector<Content>& contents) override;

using AggMethod = std::function<void(const Scalar&, Scalar&)>;

private:
AggMethod m_agregateFunction;
};

} // namespace telemetry
5 changes: 3 additions & 2 deletions src/telemetry/aggregator/aggSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ static Content createDictContent(const std::string& dictKey, const ResultType& r

Content AggMethodSum::createContent(const ResultType& result)
{
if (!m_dictResultname.empty()) {
return createDictContent(m_dictResultname, result);
const auto dictResultName = getDictResultName();
if (!dictResultName.empty()) {
return createDictContent(dictResultName, result);
}

auto visitor = [&](const auto& arg) -> Content { return arg; };
Expand Down
Loading

0 comments on commit e1174cb

Please sign in to comment.