Skip to content

Commit

Permalink
dbcopy: calculate remaining hours and rows to copy
Browse files Browse the repository at this point in the history
- for number of rows, separate thousands with \'
  • Loading branch information
franku committed Feb 27, 2020
1 parent 6735894 commit 290ed9d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
7 changes: 4 additions & 3 deletions core/src/dird/dbcopy/database_import_mysql.cc
Expand Up @@ -70,7 +70,7 @@ struct ResultHandlerContext {

static void PrintCopyStartToStdout(const Progress& progress)
{
if (progress.FullAmount() != 0) {
if (progress.FullNumberOfRows() != 0) {
std::cout << "--> copying " << progress.FullAmount() << " rows..."
<< std::endl;
std::cout << "--> Start: " << progress.TimeOfDay() << std::endl;
Expand Down Expand Up @@ -204,8 +204,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() // estimated time of arrival
<< " (" << r->progress.RemainingHours() << " remaining)"
<< " Started:" << r->progress.StartTime() << std::endl;
<< " (running:" << r->progress.RunningHours() << ","
<< " remaining:" << r->progress.RemainingHours() << ")"
<< std::endl;
}
return 0;
}
Expand Down
63 changes: 48 additions & 15 deletions core/src/dird/dbcopy/progress.cc
Expand Up @@ -22,9 +22,11 @@
#include "include/bareos.h"
#include "cats/cats.h"
#include "dird/dbcopy/progress.h"
#include "include/make_unique.h"

#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
#include <thread>

Expand Down Expand Up @@ -97,7 +99,7 @@ bool Progress::Increment()
(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;
state_.eta = system_clock::now() + remaining_seconds_;

state_.ratio =
Ratio::num - (state_.amount * Ratio::num) / (full_amount_ * Ratio::den);
Expand All @@ -110,33 +112,64 @@ bool Progress::Increment()
return changed;
}

static std::string FormatTime(time_point<system_clock> tp,
const char* fmt,
bool is_duration = false)
struct separate_thousands : std::numpunct<char> {
char_type do_thousands_sep() const override { return '\''; }
string_type do_grouping() const override { return "\3"; }
};

std::string Progress::FullAmount() const
{
auto thousands = std::make_unique<separate_thousands>();
std::stringstream ss;
ss.imbue(std::locale(std::cout.getloc(), thousands.release()));
ss << full_amount_;
return ss.str();
}

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

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

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

std::string Progress::StartTime() const
std::string Progress::StartTime() const { return FormatTime(start_time_); }

struct Time {
Time(seconds duration)
{
hours = duration.count() / 3600;
min = (duration.count() % 3600) / 60;
sec = duration.count() % 60;
}
int hours, min, sec;
};

std::string Progress::RemainingHours() const
{
return FormatTime(start_time_, "%F %T");
Time duration(remaining_seconds_);

std::array<char, 100> buffer;
sprintf(buffer.data(), "%0dh%02dm%02ds", duration.hours, duration.min,
duration.sec);
return std::string(buffer.data());
}

std::string Progress::RemainingHours() const
std::string Progress::RunningHours() const
{
return FormatTime(time_point<system_clock>() + remaining_seconds_, "%F %T",
true);
Time duration(
std::chrono::duration_cast<seconds>(system_clock::now() - start_time_));

std::array<char, 100> buffer;
sprintf(buffer.data(), "%0dh%02dm%02ds", duration.hours, duration.min,
duration.sec);
return std::string(buffer.data());
}
4 changes: 3 additions & 1 deletion core/src/dird/dbcopy/progress.h
Expand Up @@ -52,10 +52,12 @@ class Progress {
using Ratio = std::ratio<100, 1>;
static constexpr std::size_t number_of_rows_per_increment_ = 250000;

std::size_t FullAmount() const { return state_.amount; }
std::size_t FullNumberOfRows() const { return full_amount_; }
std::size_t Rate() const { return state_.ratio; }

std::string Eta() const;
std::string FullAmount() const;
std::string RunningHours() const;
std::string RemainingHours() const;
std::string StartTime() const;
std::string TimeOfDay() const;
Expand Down

0 comments on commit 290ed9d

Please sign in to comment.