Skip to content

Commit

Permalink
dbcopy: add remaining hours and start time to output
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Feb 27, 2020
1 parent 1ef6c73 commit 6735894
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/src/dird/dbcopy/database_import_mysql.cc
Expand Up @@ -203,7 +203,9 @@ int DatabaseImportMysql::ResultHandlerCopy(void* ctx, int fields, char** row)

if (r->progress.Increment()) {
std::cout << std::setw(7) << r->progress.Rate() << "%"
<< " ETA:" << r->progress.Eta() << std::endl;
<< " ETA:" << r->progress.Eta() // estimated time of arrival
<< " (" << r->progress.RemainingHours() << " remaining)"
<< " Started:" << r->progress.StartTime() << std::endl;
}
return 0;
}
Expand Down
26 changes: 22 additions & 4 deletions core/src/dird/dbcopy/progress.cc
Expand Up @@ -64,6 +64,7 @@ Progress::Progress(BareosDb* db,
}
full_amount_ = a.amount;
state_.amount = a.amount;
start_time_ = system_clock::now();
state_.start = system_clock::now();
state_old_ = state_;
is_valid_ = true;
Expand Down Expand Up @@ -94,6 +95,7 @@ bool Progress::Increment()

auto remaining_time =
(state_.duration) * (state_.amount / (state_old_.amount - state_.amount));
remaining_seconds_ = std::chrono::duration_cast<seconds>(remaining_time);

state_.eta = system_clock::now() + remaining_time;

Expand All @@ -108,17 +110,33 @@ bool Progress::Increment()
return changed;
}

static std::string FormatTime(time_point<system_clock> tp)
static std::string FormatTime(time_point<system_clock> tp,
const char* fmt,
bool is_duration = false)
{
std::ostringstream oss;
std::time_t time = system_clock::to_time_t(tp);
oss << std::put_time(std::localtime(&time), "%F %T");
if (is_duration) {
oss << std::put_time(std::gmtime(&time), fmt);
} else
oss << std::put_time(std::localtime(&time), fmt);
return oss.str();
}

std::string Progress::Eta() const { return FormatTime(state_.eta); }
std::string Progress::Eta() const { return FormatTime(state_.eta, "%F %T"); }

std::string Progress::TimeOfDay() const
{
return FormatTime(system_clock::now());
return FormatTime(system_clock::now(), "%F %T");
}

std::string Progress::StartTime() const
{
return FormatTime(start_time_, "%F %T");
}

std::string Progress::RemainingHours() const
{
return FormatTime(time_point<system_clock>() + remaining_seconds_, "%F %T",
true);
}
9 changes: 7 additions & 2 deletions core/src/dird/dbcopy/progress.h
Expand Up @@ -29,6 +29,7 @@
class BareosDb;

using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;
using std::chrono::time_point;

Expand All @@ -37,7 +38,7 @@ struct ProgressState {
time_point<system_clock> start{};
std::size_t amount{};
std::size_t ratio{};
milliseconds duration;
milliseconds duration{};
};

class Progress {
Expand All @@ -54,12 +55,16 @@ class Progress {
std::size_t FullAmount() const { return state_.amount; }
std::size_t Rate() const { return state_.ratio; }

std::string TimeOfDay() const;
std::string Eta() const;
std::string RemainingHours() const;
std::string StartTime() const;
std::string TimeOfDay() const;

private:
ProgressState state_;
ProgressState state_old_;
time_point<system_clock> start_time_{};
seconds remaining_seconds_{};
std::size_t full_amount_{};
bool is_valid_{false};
bool is_initial_run_{true};
Expand Down

0 comments on commit 6735894

Please sign in to comment.