-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunHistory.cpp
More file actions
153 lines (136 loc) · 4.99 KB
/
Copy pathRunHistory.cpp
File metadata and controls
153 lines (136 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Persistent log of past runs, so results can be reviewed without re-testing.
//
#include "RunHistory.h"
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
namespace {
bool ensureParentDirs(const std::string& path) {
size_t pos = 0;
while ((pos = path.find('/', pos + 1)) != std::string::npos) {
std::string dir = path.substr(0, pos);
if (!dir.empty() && ::mkdir(dir.c_str(), 0755) != 0 && errno != EEXIST)
return false;
}
return true;
}
std::string jsonEscape(const std::string& in) {
std::string out;
out.reserve(in.size());
for (char c : in) {
if (c == '"' || c == '\\') out += '\\';
if (static_cast<unsigned char>(c) < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += c;
}
}
return out;
}
std::string extractString(const std::string& line, const std::string& key) {
std::string needle = "\"" + key + "\":\"";
size_t pos = line.find(needle);
if (pos == std::string::npos) return "";
pos += needle.size();
std::string out;
while (pos < line.size()) {
char c = line[pos];
if (c == '\\' && pos + 1 < line.size()) {
out += line[pos + 1];
pos += 2;
continue;
}
if (c == '"') break;
out += c;
pos++;
}
return out;
}
double extractNumber(const std::string& line, const std::string& key, double fallback) {
std::string needle = "\"" + key + "\":";
size_t pos = line.find(needle);
if (pos == std::string::npos) return fallback;
pos += needle.size();
char* end = nullptr;
double val = std::strtod(line.c_str() + pos, &end);
return end == line.c_str() + pos ? fallback : val;
}
} // namespace
std::string historyFilePath() {
const char* xdg = ::getenv("XDG_STATE_HOME");
const char* home = ::getenv("HOME");
std::string base = (xdg && *xdg) ? xdg : ((home && *home) ? std::string(home) + "/.local/state" : "./.local/state");
return base + "/speedtest++/history.jsonl";
}
std::string historyJsonLine(const RunRecord& r) {
char buf[256];
snprintf(buf, sizeof(buf),
"{\"ts\":%lld,\"ip\":\"%s\",\"isp\":\"%s\",\"city\":\"%s\",\"server\":\"%s\",\"sponsor\":\"%s\",\"host\":\"%s\",\"distance\":%.2f,\"ping\":%ld,\"jitter\":%ld,\"download\":%.2f,\"upload\":%.2f}",
r.ts, jsonEscape(r.ip).c_str(), jsonEscape(r.isp).c_str(), jsonEscape(r.city).c_str(),
jsonEscape(r.server_name).c_str(), jsonEscape(r.server_sponsor).c_str(), jsonEscape(r.server_host).c_str(),
r.distance, r.ping, r.jitter, r.download, r.upload);
return std::string(buf);
}
bool appendHistory(const RunRecord& record) {
std::string path = historyFilePath();
if (!ensureParentDirs(path)) return false;
std::ofstream out(path, std::ios::app);
if (!out) return false;
out << historyJsonLine(record) << "\n";
return out.good();
}
std::vector<RunRecord> readHistory(size_t last_n) {
std::vector<RunRecord> records;
std::ifstream in(historyFilePath());
if (!in || last_n == 0) return records;
std::deque<std::string> tail;
std::string line;
while (std::getline(in, line)) {
if (line.empty()) continue;
tail.push_back(line);
if (tail.size() > last_n) tail.pop_front();
}
for (const auto& l : tail) {
RunRecord r;
r.ts = static_cast<long long>(extractNumber(l, "ts", 0));
r.ip = extractString(l, "ip");
r.isp = extractString(l, "isp");
r.city = extractString(l, "city");
r.server_name = extractString(l, "server");
r.server_sponsor = extractString(l, "sponsor");
r.server_host = extractString(l, "host");
r.distance = extractNumber(l, "distance", -1);
r.ping = static_cast<long>(extractNumber(l, "ping", -1));
r.jitter = static_cast<long>(extractNumber(l, "jitter", -1));
r.download = extractNumber(l, "download", -1);
r.upload = extractNumber(l, "upload", -1);
if (r.ts > 0) records.push_back(r);
}
return records;
}
std::string formatTimestamp(long long ts) {
time_t when = static_cast<time_t>(ts);
struct tm tmbuf;
if (!localtime_r(&when, &tmbuf)) return "?";
char buf[32];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tmbuf);
return std::string(buf);
}
std::string formatAgo(long long ts) {
long long diff = static_cast<long long>(::time(nullptr)) - ts;
if (diff < 0) diff = 0;
char buf[48];
if (diff < 60) return "just now";
if (diff < 3600) snprintf(buf, sizeof(buf), "%lld min ago", diff / 60);
else if (diff < 86400) snprintf(buf, sizeof(buf), "%lldh %lldm ago", diff / 3600, (diff % 3600) / 60);
else snprintf(buf, sizeof(buf), "%lld days ago", diff / 86400);
return std::string(buf);
}