ORE Studio 0.0.4
Loading...
Searching...
No Matches
RecencyTracker.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_RECENCY_TRACKER_HPP
21#define ORES_QT_RECENCY_TRACKER_HPP
22
23#include <chrono>
24#include <string>
25#include <unordered_set>
26#include <QDateTime>
27#include <ores.platform/attributes.hpp>
28
29namespace ores::qt {
30
34template<typename Entity>
36 auto operator()(const Entity& e) const {
37 return e.recorded_at;
38 }
39};
40
72template<typename Entity,
73 typename KeyExtractor,
74 typename TimestampExtractor = default_timestamp_extractor<Entity>>
76public:
84 explicit RecencyTracker(KeyExtractor key_extractor)
85 : key_extractor_(std::move(key_extractor)),
86 timestamp_extractor_() {}
87
94 RecencyTracker(KeyExtractor key_extractor, TimestampExtractor timestamp_extractor)
95 : key_extractor_(std::move(key_extractor)),
96 timestamp_extractor_(std::move(timestamp_extractor)) {}
97
107 template<typename Container>
108 bool update(const Container& entities) {
109 recent_keys_.clear();
110
111 const QDateTime now = QDateTime::currentDateTime();
112
113 // First load: set baseline timestamp, no highlighting
114 if (!last_reload_time_.isValid()) {
115 last_reload_time_ = now;
116 return false;
117 }
118
119 // Find entities with recorded_at newer than last reload
120 for (const auto& entity : entities) {
121 const auto recorded_at = timestamp_extractor_(entity);
122 if (recorded_at == std::chrono::system_clock::time_point{}) {
123 continue;
124 }
125
126 const auto msecs = std::chrono::duration_cast<std::chrono::milliseconds>(
127 recorded_at.time_since_epoch()).count();
128 QDateTime recorded_dt = QDateTime::fromMSecsSinceEpoch(msecs);
129
130 if (recorded_dt.isValid() && recorded_dt > last_reload_time_) {
131 recent_keys_.insert(key_extractor_(entity));
132 }
133 }
134
135 last_reload_time_ = now;
136 return !recent_keys_.empty();
137 }
138
145 [[nodiscard]] bool is_recent(const std::string& key) const {
146 return recent_keys_.find(key) != recent_keys_.end();
147 }
148
152 [[nodiscard]] std::size_t recent_count() const {
153 return recent_keys_.size();
154 }
155
159 [[nodiscard]] bool has_recent() const {
160 return !recent_keys_.empty();
161 }
162
168 void clear() {
169 recent_keys_.clear();
170 }
171
177 void reset() {
178 recent_keys_.clear();
179 last_reload_time_ = QDateTime();
180 }
181
182private:
183 KeyExtractor key_extractor_;
184 ORES_NO_UNIQUE_ADDRESS TimestampExtractor timestamp_extractor_;
185 std::unordered_set<std::string> recent_keys_;
186 QDateTime last_reload_time_;
187};
188
195template<typename Entity, typename KeyExtractor>
196auto make_recency_tracker(KeyExtractor key_extractor) {
197 return RecencyTracker<Entity, KeyExtractor>(std::move(key_extractor));
198}
199
207template<typename Entity, typename KeyExtractor, typename TimestampExtractor>
208auto make_recency_tracker(KeyExtractor key_extractor,
209 TimestampExtractor timestamp_extractor) {
211 std::move(key_extractor), std::move(timestamp_extractor));
212}
213
214}
215
216#endif
STL namespace.
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
auto make_recency_tracker(KeyExtractor key_extractor)
Helper function to create a RecencyTracker with type deduction.
Definition RecencyTracker.hpp:196
Default timestamp extractor that accesses .recorded_at member.
Definition RecencyTracker.hpp:35
Tracks recently-modified records for recency highlighting.
Definition RecencyTracker.hpp:75
RecencyTracker(KeyExtractor key_extractor, TimestampExtractor timestamp_extractor)
Construct a RecencyTracker with custom key and timestamp extractors.
Definition RecencyTracker.hpp:94
bool has_recent() const
Check if there are any recent records.
Definition RecencyTracker.hpp:159
bool update(const Container &entities)
Update the set of recent records by comparing timestamps.
Definition RecencyTracker.hpp:108
std::size_t recent_count() const
Get the number of recent records.
Definition RecencyTracker.hpp:152
bool is_recent(const std::string &key) const
Check if a record with the given key is recent.
Definition RecencyTracker.hpp:145
void clear()
Clear the recent records set.
Definition RecencyTracker.hpp:168
void reset()
Reset the tracker completely.
Definition RecencyTracker.hpp:177
RecencyTracker(KeyExtractor key_extractor)
Construct a RecencyTracker with custom key extractor.
Definition RecencyTracker.hpp:84