search ...

master
const an teen 2021-05-24 17:05:41 +03:00
parent 2be6bdae5c
commit 447856b0c4
1 changed files with 23 additions and 10 deletions

View File

@ -4,6 +4,7 @@
#include <regex> #include <regex>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <map>
#include <thread> #include <thread>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "tcpsyncclient.h" #include "tcpsyncclient.h"
@ -25,10 +26,14 @@ std::map<std::string, std::string> conf =
std::string search(std::string text) std::string search(std::string text)
{ {
constexpr int maxSize = 10; constexpr int maxSize = 10;
uint64_t success = 0; std::string values; // Строка для возврата
uint64_t found = 0;
std::string values; uint64_t success = 0; // Счетчик уникальных вхождений
std::vector<std::string> matches; uint64_t total = 0; // Общий счетчик вхождений
std::map<std::string, uint64_t> stats; // Линковка даты и ее счетчика
std::vector<std::string> matches; // Значения, компонуемые в итоговую строку
std::regex regex(".*" + text + ".*", std::regex_constants::extended | std::regex_constants::icase); std::regex regex(".*" + text + ".*", std::regex_constants::extended | std::regex_constants::icase);
boost::filesystem::recursive_directory_iterator dir(conf["logpath"]), end; boost::filesystem::recursive_directory_iterator dir(conf["logpath"]), end;
@ -42,13 +47,15 @@ std::string search(std::string text)
{ {
if (std::regex_match(buffer, regex)) if (std::regex_match(buffer, regex))
{ {
std::string date = buffer.substr(0, buffer.find(' ')); ++total;
bool first = true; bool first = true;
std::string date = buffer.substr(0, buffer.find(' '));
stats[date] += 1; // Счетчик конкретной даты
for (auto entry: matches) for (auto entry: matches)
{ {
if (entry.find(date) != std::string::npos) if (entry.find(date) != std::string::npos)
{ {
++found;
first = false; first = false;
} }
} }
@ -59,13 +66,19 @@ std::string search(std::string text)
} }
} }
} }
log.close();
log.close();
} }
if (matches.size() > 0) if (matches.size() > 0)
{ {
for (auto it = matches.begin(), end = matches.end(); it != end; ++it)
{
*it += " (" + std::to_string(stats[*it]) + ")";
}
std::sort(matches.begin(), matches.end()); std::sort(matches.begin(), matches.end());
values += "(" + std::to_string(found) + ") "; values += "[" + text + ": " + std::to_string(total) + "] ";
for (int i = matches.size()-1, count = 0; i >= 0 && count < maxSize ; --i, ++count) for (int i = matches.size()-1, count = 0; i >= 0 && count < maxSize ; --i, ++count)
{ // Компоновка выходной строки { // Компоновка выходной строки
@ -169,7 +182,7 @@ void make_tsc()
void handler() void handler()
{ {
if (!tsc_created) std::this_thread::sleep_for(std::chrono::seconds(1)); if (!tsc_created) std::this_thread::sleep_for(std::chrono::milliseconds(600));
bool handled = false; bool handled = false;
while (true) while (true)
@ -195,7 +208,7 @@ void handler()
std::string result = search(target); std::string result = search(target);
if (result != "") if (result != "")
{ {
tsc->write_to_channel(tsc->get_msg_nick() + ": " + search(target)); tsc->write_to_channel(search(target));
} }
else tsc->write_to_channel(tsc->get_msg_nick() + ", " + conf["notfound"]); else tsc->write_to_channel(tsc->get_msg_nick() + ", " + conf["notfound"]);
} }