From 10615ed0fac798f01ce3c89f1f2d25883d54625c Mon Sep 17 00:00:00 2001 From: Patrick Lipka Date: Tue, 14 Dec 2021 14:39:20 +0100 Subject: [PATCH] reporting added --- src/project.cpp | 17 ++++++++++-- src/project.h | 4 +-- src/task.cpp | 2 +- src/tt.cpp | 23 +++++++++++++--- src/tt.h | 6 +++++ src/ui.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++- src/ui.h | 4 ++- 7 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 src/tt.h diff --git a/src/project.cpp b/src/project.cpp index 0ddf22d..bcc49ac 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -8,6 +8,7 @@ Project::Project(std::string name){ Project::name = name; Project::num_tasks = 0; Project::active_task_id = 0; + Project::active_task = NULL; } void Project::add_task(Task task){ @@ -45,6 +46,13 @@ int Project::find_task_id_by_name(std::string task_name){ return -1; } +int Project::get_total_work_time(){ + int wtime_proj = 0; + for (int i=0; iset_active_task(active_project->active_task_id); num_projects++; } @@ -76,9 +85,10 @@ void ProjectList::save(std::string file_name){ std::ofstream of(file_name, std::ios::binary); if (!of){ std::cout << "Could not open file " << file_name << " for writing!" << std::endl; - exit(1); + // exit(1); } of.write((char*) &num_projects, sizeof(int)); + std::cout << "Saving: num_projects=" << num_projects << std::endl; of.write((char*) &active_project_id, sizeof(int)); for (int i=0; itm_mon+1; + int year = date->tm_year+1900; + std::string month_str; + if (month < 10){ + month_str = "0"+std::to_string(month); + }else{ + month_str = std::to_string(month); + } + std::string date_str = std::to_string(year)+"-"+month_str; + return date_str; +} +std::string user_name; int main(){ // connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects signal(SIGINT, handler); tracking = 0; - + user_name = "Patrick"; + // TEST: set up test list ProjectList proj_list("dec"); Project proj("Test Project"); @@ -22,8 +40,7 @@ int main(){ proj.add_task(task1); proj.add_task(task2); proj.add_task(task3); - proj_list.add_project(proj); - + proj_list.add_project(proj); init_autocomplete(&proj_list); // use GNU readline for auto completion and history when parsing command input while(1) { diff --git a/src/tt.h b/src/tt.h new file mode 100644 index 0000000..84e2355 --- /dev/null +++ b/src/tt.h @@ -0,0 +1,6 @@ +#ifndef TT_H +#define TT_H +#include +extern std::string user_name; +std::string get_date(); +#endif diff --git a/src/ui.cpp b/src/ui.cpp index eda3619..0e70653 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -4,10 +4,13 @@ #include #include #include +#include +#include #include "ui.h" #include "project.h" #include "track.h" #include "string_utils.h" +#include "tt.h" // Uses GNU readline library for autocompletion and command history @@ -22,7 +25,8 @@ std::string command_names[num_commands]={ "rm", "report", "ls", - "start" + "start", + "save" }; std::vector autocomplete_names; @@ -67,6 +71,7 @@ void init_autocomplete(ProjectList *proj_list){ } void parse_input(std::string input, ProjectList *proj_list){ + input = trim(input); size_t command_end = input.find(" "); std::string command = input.substr(0,command_end); std::string argument = input.substr(command_end+1); @@ -159,6 +164,14 @@ void parse_input(std::string input, ProjectList *proj_list){ } } + }else if (command == "report"){ + if(command_end == std::string::npos){ + command_report("",proj_list); + }else{ + command_report(argument,proj_list); + } + }else if (command == "save"){ + command_save(proj_list); } } @@ -445,6 +458,7 @@ void command_at(std::string input, int wtime, ProjectList *proj_list){ // add time to active task proj_list->active_project->active_task->add_time(wtime); std::cout << wtime << "s added to task " << proj_list->active_project->name << "/" << proj_list->active_project->active_task->name << std::endl; + std::cout << "New time: "<< proj_list->active_project->active_task->work_time << std::endl; return; }else{ // add time to task in same project @@ -510,3 +524,59 @@ void command_rt(std::string input, int wtime, ProjectList *proj_list){ std::cout << "Task " << underscore_to_space(input) << " does not exist." << std::endl; } } + +void command_report(std::string date_str, ProjectList* proj_list){ + date_str = trim(date_str); + size_t place_of_minus = date_str.find("-"); + std::string year = date_str.substr(0,place_of_minus); + std::string month = date_str.substr(place_of_minus+1); + + if(date_str.length() == 0){ + // report for current month + // get date: + std::string date = get_date(); + std::cout << "Report for " << user_name << ", month: " << date << std::endl << std::endl; + + for (int i=0; inum_projects; i++){ + // compute task and total times + Project *proj = &(proj_list->projects[i]); + float wtime_proj = proj->get_total_work_time() / 3600.0; + if(wtime_proj >= 0.01){ + std::cout << "Project: " << proj->name << std::endl; + for(int j=0; jnum_tasks; j++){ + float wtime_task = proj->tasks[j].work_time / 3600.0; + if(wtime_task >= 0.01) std::cout << "--- " << proj->tasks[j].name << ": " << std::fixed << std::setprecision(2) << wtime_task << std::endl; + } + std::cout << "Total: " << std::fixed << std::setprecision(2) << wtime_proj << std::endl << std::endl; + } + } + }else{ + ProjectList *list = new ProjectList(month); + list->load(date_str); + if (list->num_projects > 0){ + std::cout << "Report for " << user_name << ", month: " << date_str << std::endl << std::endl; + for (int i=0; inum_projects; i++){ + // compute task and total times + Project *proj = &(list->projects[i]); + float wtime_proj = proj->get_total_work_time() / 3600.0; + if(wtime_proj >= 0.01){ + std::cout << "Project: " << proj->name << std::endl; + for(int j=0; jnum_tasks; j++){ + float wtime_task = proj->tasks[j].work_time / 3600.0; + if(wtime_task >= 0.01) std::cout << "--- " << proj->tasks[j].name << ": " << std::fixed << std::setprecision(2) << wtime_task << std::endl; + } + std::cout << "Total: " << std::fixed << std::setprecision(2) << wtime_proj << std::endl << std::endl; + } + } + }else{ + std::cout << "report: Project list is empty." << std::endl; + } + } +} + +void command_save(ProjectList *proj_list){ + std::string date_str = get_date(); + std::string file_name = date_str; + proj_list->save(file_name); + std::cout << "Tracking data saved to file " << file_name << std::endl; +} diff --git a/src/ui.h b/src/ui.h index 71240d1..a000d14 100644 --- a/src/ui.h +++ b/src/ui.h @@ -5,7 +5,7 @@ #include #include "project.h" -const int num_commands=11; +const int num_commands=12; extern std::string command_names[num_commands]; extern std::vector autocomplete_names; char **tt_name_completion(const char* text, int start, int end); @@ -28,4 +28,6 @@ void command_nt(std::string input, ProjectList *proj_list); void command_re(std::string input, std::string new_name, ProjectList *proj_lits); void command_at(std::string input, int wtime, ProjectList *proj_list); void command_rt(std::string input, int wtime, ProjectList *proj_list); +void command_report(std::string date_str, ProjectList *proj_list); +void command_save(ProjectList *proj_list); #endif