|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file LogsAnalyzer.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + */ |
| 13 | +#include <vix/cli/commands/logs/LogsAnalyzer.hpp> |
| 14 | +#include <vix/cli/util/Ui.hpp> |
| 15 | + |
| 16 | +#include <algorithm> |
| 17 | +#include <cctype> |
| 18 | +#include <ostream> |
| 19 | +#include <regex> |
| 20 | +#include <string> |
| 21 | +#include <unordered_map> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace vix::commands::logs::analyzer |
| 25 | +{ |
| 26 | + namespace |
| 27 | + { |
| 28 | + std::string trim_copy(std::string value) |
| 29 | + { |
| 30 | + auto not_space = [](unsigned char ch) |
| 31 | + { |
| 32 | + return !std::isspace(ch); |
| 33 | + }; |
| 34 | + |
| 35 | + value.erase( |
| 36 | + value.begin(), |
| 37 | + std::find_if(value.begin(), value.end(), not_space)); |
| 38 | + |
| 39 | + value.erase( |
| 40 | + std::find_if(value.rbegin(), value.rend(), not_space).base(), |
| 41 | + value.end()); |
| 42 | + |
| 43 | + return value; |
| 44 | + } |
| 45 | + |
| 46 | + std::string to_lower_copy(std::string value) |
| 47 | + { |
| 48 | + std::transform( |
| 49 | + value.begin(), |
| 50 | + value.end(), |
| 51 | + value.begin(), |
| 52 | + [](unsigned char ch) |
| 53 | + { |
| 54 | + return static_cast<char>(std::tolower(ch)); |
| 55 | + }); |
| 56 | + |
| 57 | + return value; |
| 58 | + } |
| 59 | + |
| 60 | + std::string normalize_log_line(std::string line) |
| 61 | + { |
| 62 | + line = trim_copy(line); |
| 63 | + |
| 64 | + line = std::regex_replace( |
| 65 | + line, |
| 66 | + std::regex(R"(\b[0-9]{4}-[0-9]{2}-[0-9]{2}[^\s]*\b)"), |
| 67 | + "<timestamp>"); |
| 68 | + |
| 69 | + line = std::regex_replace( |
| 70 | + line, |
| 71 | + std::regex(R"(\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b)"), |
| 72 | + "<ip>"); |
| 73 | + |
| 74 | + line = std::regex_replace( |
| 75 | + line, |
| 76 | + std::regex(R"(\b[0-9]+\b)"), |
| 77 | + "<num>"); |
| 78 | + |
| 79 | + line = std::regex_replace( |
| 80 | + line, |
| 81 | + std::regex("\"([^\"]{32,})\""), |
| 82 | + "\"<value>\""); |
| 83 | + |
| 84 | + line = std::regex_replace( |
| 85 | + line, |
| 86 | + std::regex(R"('([^']{32,})')"), |
| 87 | + "'<value>'"); |
| 88 | + |
| 89 | + line = to_lower_copy(line); |
| 90 | + line = trim_copy(line); |
| 91 | + |
| 92 | + return line; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + RepeatedLogReport analyze_repeated_errors( |
| 97 | + const std::vector<std::string> &lines) |
| 98 | + { |
| 99 | + RepeatedLogReport report; |
| 100 | + report.totalLines = static_cast<int>(lines.size()); |
| 101 | + |
| 102 | + std::unordered_map<std::string, int> counts; |
| 103 | + |
| 104 | + for (const std::string &line : lines) |
| 105 | + { |
| 106 | + const std::string normalized = normalize_log_line(line); |
| 107 | + |
| 108 | + if (normalized.empty()) |
| 109 | + continue; |
| 110 | + |
| 111 | + ++counts[normalized]; |
| 112 | + } |
| 113 | + |
| 114 | + for (const auto &[message, count] : counts) |
| 115 | + { |
| 116 | + if (count <= 1) |
| 117 | + continue; |
| 118 | + |
| 119 | + report.entries.push_back( |
| 120 | + RepeatedLogEntry{ |
| 121 | + message, |
| 122 | + count}); |
| 123 | + } |
| 124 | + |
| 125 | + std::sort( |
| 126 | + report.entries.begin(), |
| 127 | + report.entries.end(), |
| 128 | + [](const RepeatedLogEntry &a, const RepeatedLogEntry &b) |
| 129 | + { |
| 130 | + if (a.count != b.count) |
| 131 | + return a.count > b.count; |
| 132 | + |
| 133 | + return a.message < b.message; |
| 134 | + }); |
| 135 | + |
| 136 | + report.repeatedGroups = |
| 137 | + static_cast<int>(report.entries.size()); |
| 138 | + |
| 139 | + return report; |
| 140 | + } |
| 141 | + |
| 142 | + void print_repeated_report( |
| 143 | + std::ostream &out, |
| 144 | + const RepeatedLogReport &report) |
| 145 | + { |
| 146 | + vix::cli::util::section(out, "Repeated Errors"); |
| 147 | + |
| 148 | + vix::cli::util::kv( |
| 149 | + out, |
| 150 | + "Analyzed lines", |
| 151 | + std::to_string(report.totalLines)); |
| 152 | + |
| 153 | + vix::cli::util::kv( |
| 154 | + out, |
| 155 | + "Repeated groups", |
| 156 | + std::to_string(report.repeatedGroups)); |
| 157 | + |
| 158 | + if (report.entries.empty()) |
| 159 | + { |
| 160 | + vix::cli::util::ok_line( |
| 161 | + out, |
| 162 | + "no repeated errors detected"); |
| 163 | + |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + for (const RepeatedLogEntry &entry : report.entries) |
| 168 | + { |
| 169 | + vix::cli::util::kv( |
| 170 | + out, |
| 171 | + std::to_string(entry.count) + "x", |
| 172 | + entry.message); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments