Skip to content

Commit

Permalink
Fix query and format (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioborondo authored Dec 27, 2023
1 parent fcdec9e commit 723c8e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
93 changes: 48 additions & 45 deletions src/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,40 @@ std::string Db::Summary(const std::string& date)
AddRecord(Record{Record::Type::kStop});
}

const auto sql_format_string{"SELECT "
" CAST (SUM(difference) * 24 AS REAL) "
"FROM "
"( "
" SELECT "
" julianday(records2.logout) - julianday(records2.login) AS difference "
" FROM "
" ( "
" SELECT "
" login_records.timestamp AS login, "
" logout_records.timestamp AS logout "
" FROM "
" ( "
" SELECT "
" row_number() OVER (ORDER BY timestamp) AS row_number, "
" timestamp "
" FROM "
" records "
" WHERE "
" date(timestamp) = '{0}' "
" AND "
" type = 1 "
" ) AS login_records, "
" ( "
" SELECT "
" row_number() OVER (ORDER BY timestamp) AS row_number, "
" timestamp "
" FROM "
" records "
" WHERE "
" date(timestamp) = '{0}' "
" AND "
" type = 0 "
" ) AS logout_records "
" WHERE "
" login_records.row_number = logout_records.row_number "
" ) as records2 "
") "};
const auto sql_format_string{R"(
SELECT
SUM(difference)
FROM
(
SELECT
julianday(stop_records.timestamp) -julianday(start_records.timestamp) AS difference
FROM
(
SELECT
row_number() OVER(ORDER BY timestamp) AS row_number,
timestamp
FROM
records
WHERE
date(timestamp) = '{0}'
AND
type=1
) AS start_records,
(
SELECT
row_number() OVER(ORDER BY timestamp) AS row_number,
timestamp
FROM
records
WHERE
date(timestamp) = '{0}'
AND
type=0
) AS stop_records
WHERE
start_records.row_number=stop_records.row_number
)
)"};

const auto sql{fmt::format(sql_format_string, date)};

Expand Down Expand Up @@ -106,13 +102,7 @@ std::string Db::Summary(const std::string& date)
DeleteLast();
}

int hours = result;
double minutesRemainder = (result - hours) * 60;
int minutes = minutesRemainder;
double secondsRemainder = (minutesRemainder - minutes) * 60;
int seconds = secondsRemainder;

return fmt::format("{0:02}:{1:02}:{1:02}", hours, minutes, seconds);
return GetTime(result);
}

int Db::GetLastType()
Expand Down Expand Up @@ -237,3 +227,16 @@ Record Db::GetLastRecord()

return Record{static_cast<Record::Type>(type), timestamp};
}

std::string Db::GetTime(double time_in_days)
{
double hours{time_in_days * 24};

double minutes_in_hours{hours - static_cast<int>(hours)};
double minutes{minutes_in_hours * 60};

double seconds_in_minutes{minutes - static_cast<int>(minutes)};
double seconds{seconds_in_minutes * 60};

return fmt::format("{0:02}:{1:02}:{2:02}", static_cast<int>(hours), static_cast<int>(minutes), static_cast<int>(seconds));
}
3 changes: 3 additions & 0 deletions src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ class Db
bool AddRecord(const Record& record);

Record GetLastRecord();

private:
std::string GetTime(double time_in_days);
};
8 changes: 3 additions & 5 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ int main(int argc, char** argv)
Db db;
if(variables_map.count("start"))
{
std::cout << "Start time tracker" << std::endl;
const Record record{Record::Type::kStart};
db.AddRecord(record);
std::cout << record.GetTimestamp() << std::endl;
}
else if(variables_map.count("stop"))
{
std::cout << "Stop time tracker" << std::endl;
const Record record{Record::Type::kStop};
db.AddRecord(record);
std::cout << record.GetTimestamp() << std::endl;
}
else if(variables_map.count("summary"))
{
std::cout << "Time tracker summary:" << std::endl;
Db db;
std::cout << db.Summary() << " hours" << std::endl;
std::cout << db.Summary() << std::endl;
}
else if(variables_map.count("version"))
{
Expand Down

0 comments on commit 723c8e8

Please sign in to comment.