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#include "ores.qt/export.hpp"
29
30namespace ores::qt {
31
35template<typename Entity>
37 auto operator()(const Entity& e) const {
38 return e.recorded_at;
39 }
40};
41
73template<typename Entity,
74 typename KeyExtractor,
75 typename TimestampExtractor = default_timestamp_extractor<Entity>>
77public:
85 explicit RecencyTracker(KeyExtractor key_extractor)
86 : key_extractor_(std::move(key_extractor)),
87 timestamp_extractor_() {}
88
95 RecencyTracker(KeyExtractor key_extractor, TimestampExtractor timestamp_extractor)
96 : key_extractor_(std::move(key_extractor)),
97 timestamp_extractor_(std::move(timestamp_extractor)) {}
98
108 template<typename Container>
109 bool update(const Container& entities) {
110 recent_keys_.clear();
111
112 const QDateTime now = QDateTime::currentDateTime();
113
114 // First load: set baseline timestamp, no highlighting
115 if (!last_reload_time_.isValid()) {
116 last_reload_time_ = now;
117 return false;
118 }
119
120 // Find entities with recorded_at newer than last reload
121 for (const auto& entity : entities) {
122 const auto recorded_at = timestamp_extractor_(entity);
123 if (recorded_at == std::chrono::system_clock::time_point{}) {
124 continue;
125 }
126
127 const auto msecs = std::chrono::duration_cast<std::chrono::milliseconds>(
128 recorded_at.time_since_epoch()).count();
129 QDateTime recorded_dt = QDateTime::fromMSecsSinceEpoch(msecs);
130
131 if (recorded_dt.isValid() && recorded_dt > last_reload_time_) {
132 recent_keys_.insert(key_extractor_(entity));
133 }
134 }
135
136 last_reload_time_ = now;
137 return !recent_keys_.empty();
138 }
139
146 [[nodiscard]] bool is_recent(const std::string& key) const {
147 return recent_keys_.find(key) != recent_keys_.end();
148 }
149
153 [[nodiscard]] std::size_t recent_count() const {
154 return recent_keys_.size();
155 }
156
160 [[nodiscard]] bool has_recent() const {
161 return !recent_keys_.empty();
162 }
163
169 void clear() {
170 recent_keys_.clear();
171 }
172
178 void reset() {
179 recent_keys_.clear();
180 last_reload_time_ = QDateTime();
181 }
182
183private:
184 KeyExtractor key_extractor_;
185 ORES_NO_UNIQUE_ADDRESS TimestampExtractor timestamp_extractor_;
186 std::unordered_set<std::string> recent_keys_;
187 QDateTime last_reload_time_;
188};
189
196template<typename Entity, typename KeyExtractor>
197auto make_recency_tracker(KeyExtractor key_extractor) {
198 return RecencyTracker<Entity, KeyExtractor>(std::move(key_extractor));
199}
200
208template<typename Entity, typename KeyExtractor, typename TimestampExtractor>
209auto make_recency_tracker(KeyExtractor key_extractor,
210 TimestampExtractor timestamp_extractor) {
212 std::move(key_extractor), std::move(timestamp_extractor));
213}
214
215}
216
217#endif
STL namespace.
Qt-based graphical user interface for ORE Studio.
Definition AccountController.hpp:32
auto make_recency_tracker(KeyExtractor key_extractor)
Helper function to create a RecencyTracker with type deduction.
Definition RecencyTracker.hpp:197
Default timestamp extractor that accesses .recorded_at member.
Definition RecencyTracker.hpp:36
Tracks recently-modified records for recency highlighting.
Definition RecencyTracker.hpp:76
RecencyTracker(KeyExtractor key_extractor, TimestampExtractor timestamp_extractor)
Construct a RecencyTracker with custom key and timestamp extractors.
Definition RecencyTracker.hpp:95
bool has_recent() const
Check if there are any recent records.
Definition RecencyTracker.hpp:160
bool update(const Container &entities)
Update the set of recent records by comparing timestamps.
Definition RecencyTracker.hpp:109
std::size_t recent_count() const
Get the number of recent records.
Definition RecencyTracker.hpp:153
bool is_recent(const std::string &key) const
Check if a record with the given key is recent.
Definition RecencyTracker.hpp:146
void clear()
Clear the recent records set.
Definition RecencyTracker.hpp:169
void reset()
Reset the tracker completely.
Definition RecencyTracker.hpp:178
RecencyTracker(KeyExtractor key_extractor)
Construct a RecencyTracker with custom key extractor.
Definition RecencyTracker.hpp:85