25#ifndef PANGEA_MAP_PROGRESS_H
26#define PANGEA_MAP_PROGRESS_H
31#include <condition_variable>
50 using Clock = std::chrono::steady_clock;
66 std::ostringstream text;
67 text.imbue(std::locale::classic());
68 text <<
"TIMING stage=" << std::quoted(
stage_) <<
" wall_seconds="
70 <<
" status=" << status <<
'\n';
71 const auto line = text.str();
72 std::cout.write(
line.data(),
static_cast<std::streamsize
>(
line.size()));
82 const double elapsed = std::chrono::duration<double>(now -
stageStarted_).count();
89 static std::string
duration(Clock::duration value) {
90 const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(value).count();
91 std::ostringstream text;
92 text << std::setfill(
'0') << std::setw(2) <<
seconds / 3600 <<
':'
93 << std::setw(2) <<
seconds / 60 % 60 <<
':' << std::setw(2) <<
seconds % 60;
100 std::string
line(Clock::time_point now)
const {
101 std::ostringstream text;
102 text <<
label_ <<
": " << std::fixed << std::setprecision(1) <<
percentage(now)
107 if (
closing_ && !
complete_) text <<
" | stopped before completion; check status/errors";
108 return text.str() +
'\n';
114 std::unique_lock<std::mutex> lock(
mutex_);
117 const auto text =
line(Clock::now());
118 std::cout.write(text.data(),
static_cast<std::streamsize
>(text.size()));
122 const auto text =
line(Clock::now());
123 std::cout.write(text.data(),
static_cast<std::streamsize
>(text.size()));
137 std::lock_guard<std::mutex> lock(
mutex_);
152 void stage(
const std::string& name,
double first,
double last,
double estimateSeconds = 0) {
153 if (!std::isfinite(first) || !std::isfinite(last) || !std::isfinite(estimateSeconds) ||
154 first < 0 || last < first || last > 99.9 || estimateSeconds < 0)
155 throw std::runtime_error(
"Invalid map progress stage");
157 std::lock_guard<std::mutex> lock(
mutex_);
159 throw std::runtime_error(
"Map progress stage would go backwards");
174 if (!std::isfinite(fraction) || fraction < 0 || fraction > 1)
175 throw std::runtime_error(
"Invalid map progress fraction");
176 std::lock_guard<std::mutex> lock(
mutex_);
182 std::lock_guard<std::mutex> lock(
mutex_);
188 std::lock_guard<std::mutex> lock(
mutex_);
199 const auto require = [](
bool value) {
200 if (!value)
throw std::runtime_error(
"Map progress regression failed");
204 progress.
stage(
"Capture", 0, 35);
211 progress.
stage(
"Dense reconstruction", 35, 80, 900);
212 const auto later = progress.
stageStarted_ + std::chrono::seconds(900);
213 require(std::abs(progress.
percentage(later) - 55.25) < 1e-9);
214 require(progress.
percentage(later + std::chrono::hours(10000)) < 80);
215 progress.
stage(
"Publication", 80, 99);
218 require(progress.
line(progress.
stageStarted_).find(
"(estimated)") != std::string::npos);
220 try { progress.
update(std::numeric_limits<double>::quiet_NaN()); }
221 catch (
const std::runtime_error&) {
rejected =
true; }
225 require(progress.
line(progress.
stageStarted_).find(
"| complete") != std::string::npos);
227 struct CaptureOutput {
228 std::ostringstream text;
229 std::streambuf* previous = std::cout.rdbuf(text.rdbuf());
230 ~CaptureOutput() { std::cout.rdbuf(previous); }
232 std::string heartbeatOutput, interruptedOutput;
237 progress.
stage(
"Long calculation", 0, 99, 1);
238 std::this_thread::sleep_for(std::chrono::milliseconds(5500));
241 heartbeatOutput =
capture.text.str();
243 require(heartbeatOutput.find(
"(estimated) | Long calculation") != std::string::npos &&
244 heartbeatOutput.find(
"| stage 00:00:05") != std::string::npos &&
245 heartbeatOutput.find(
"TIMING stage=\"Long calculation\" wall_seconds=") != std::string::npos &&
246 heartbeatOutput.find(
"100.0% | complete") != std::string::npos);
251 progress.
stage(
"Rejected output", 0, 99);
254 interruptedOutput =
capture.text.str();
256 require(interruptedOutput.find(
"stopped before completion") != std::string::npos &&
257 interruptedOutput.find(
"status=interrupted") != std::string::npos &&
258 interruptedOutput.find(
"100.0%") == std::string::npos);
259 std::cout <<
"PASS: monotonic estimated progress, bounded long-stage guesses, five-second heartbeat, "
260 "explicit interruption and completion-only 100%.\n";
static bool capture(struct transport *transport, FILE *file, const struct capture_settings *settings)
Capture to the camera's SD card and download the resulting JPEG.
Clock::time_point stageStarted_
static void selfTest()
Exercise stage progress, monotonic updates, and completion behavior.
static std::string duration(Clock::duration value)
Format an elapsed duration at whole-second precision.
void stage(const std::string &name, double first, double last, double estimateSeconds=0)
Begin a monotonic percentage range for one named stage.
void stageTiming(Clock::time_point now, const char *status)
Emit at most one elapsed-time record for the active timed stage.
void finish()
Mark the entire operation complete and permit 100 percent output.
Clock::time_point started_
void report()
Run the progress reporter until teardown.
void update(double fraction, const std::string &detail={})
Advance the current stage without allowing its fraction to regress.
std::chrono::steady_clock Clock
double percentage(Clock::time_point now) const
Estimate overall progress from reported work and stage time.
std::string line(Clock::time_point now) const
Format a consistent progress snapshot under the state mutex.
MapProgress(std::string label="Map progress")
Start reporting progress under a human-readable label.
~MapProgress()
Signal the reporter to emit its final status, then join it.
std::condition_variable wake_
void detail(const std::string &text)
Replace the operator-facing detail without changing progress.
static double seconds(Clock::duration duration)
Convert a monotonic interval without truncating fractional seconds.