reporting added

This commit is contained in:
Patrick Lipka 2021-12-14 14:39:20 +01:00
parent a1c20aad42
commit 10615ed0fa
7 changed files with 118 additions and 10 deletions

View File

@ -8,6 +8,7 @@ Project::Project(std::string name){
Project::name = name; Project::name = name;
Project::num_tasks = 0; Project::num_tasks = 0;
Project::active_task_id = 0; Project::active_task_id = 0;
Project::active_task = NULL;
} }
void Project::add_task(Task task){ void Project::add_task(Task task){
@ -45,6 +46,13 @@ int Project::find_task_id_by_name(std::string task_name){
return -1; return -1;
} }
int Project::get_total_work_time(){
int wtime_proj = 0;
for (int i=0; i<num_tasks; i++){
wtime_proj += tasks[i].work_time;
}
return wtime_proj;
}
// ProjectList definitions: // ProjectList definitions:
@ -57,6 +65,7 @@ ProjectList::ProjectList(std::string month){
void ProjectList::add_project(Project proj){ void ProjectList::add_project(Project proj){
projects.push_back(proj); projects.push_back(proj);
set_active_project(num_projects); set_active_project(num_projects);
active_project->set_active_task(active_project->active_task_id);
num_projects++; num_projects++;
} }
@ -76,9 +85,10 @@ void ProjectList::save(std::string file_name){
std::ofstream of(file_name, std::ios::binary); std::ofstream of(file_name, std::ios::binary);
if (!of){ if (!of){
std::cout << "Could not open file " << file_name << " for writing!" << std::endl; std::cout << "Could not open file " << file_name << " for writing!" << std::endl;
exit(1); // exit(1);
} }
of.write((char*) &num_projects, sizeof(int)); of.write((char*) &num_projects, sizeof(int));
std::cout << "Saving: num_projects=" << num_projects << std::endl;
of.write((char*) &active_project_id, sizeof(int)); of.write((char*) &active_project_id, sizeof(int));
for (int i=0; i<num_projects; i++){ for (int i=0; i<num_projects; i++){
size_t len = projects[i].name.size(); size_t len = projects[i].name.size();
@ -101,9 +111,11 @@ void ProjectList::load(std::string file_name){
int active; int active;
if (!inf){ if (!inf){
std::cout << "Could not open file " << file_name << " for reading!" << std::endl; std::cout << "Could not open file " << file_name << " for reading!" << std::endl;
exit(1); //exit(1);
} }
inf.read((char*) &num_projects,sizeof(int)); inf.read((char*) &num_projects,sizeof(int));
std::cout << "Reading: num_projects=" << num_projects << std::endl;
int number_of_projects = num_projects;
inf.read((char*) &active, sizeof(int)); inf.read((char*) &active, sizeof(int));
int np = num_projects; int np = num_projects;
int *active_task_arr = new int[np]; int *active_task_arr = new int[np];
@ -146,6 +158,7 @@ void ProjectList::load(std::string file_name){
} }
delete [] active_task_arr; delete [] active_task_arr;
inf.close(); inf.close();
num_projects = number_of_projects;
} }
Project* ProjectList::find_project_by_name(std::string proj_name){ Project* ProjectList::find_project_by_name(std::string proj_name){

View File

@ -18,6 +18,7 @@ class Project{
void set_active_task(int id); void set_active_task(int id);
Task *find_task_by_name(std::string task_name); Task *find_task_by_name(std::string task_name);
int find_task_id_by_name(std::string task_name); int find_task_id_by_name(std::string task_name);
int get_total_work_time();
}; };
class ProjectList{ class ProjectList{
@ -34,8 +35,7 @@ class ProjectList{
void save(std::string file_name); void save(std::string file_name);
void load(std::string file_name); void load(std::string file_name);
Project *find_project_by_name(std::string proj_name); Project *find_project_by_name(std::string proj_name);
int find_project_id_by_name(std::string proj_name); int find_project_id_by_name(std::string proj_name);
}; };
#endif #endif

View File

@ -8,5 +8,5 @@ Task::Task(std::string name){
} }
void Task::add_time(int seconds){ void Task::add_time(int seconds){
Task::work_time = std::max(work_time+seconds,0); work_time = std::max(work_time+seconds,0);
} }

View File

@ -9,12 +9,30 @@
#include "project.h" #include "project.h"
#include "track.h" #include "track.h"
#include "ui.h" #include "ui.h"
#include "tt.h"
std::string get_date(){
time_t t = time(NULL);
tm *date = localtime(&t);
int month = 5;//date->tm_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(){ int main(){
// connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects // connect SIGINT signal (CTRL-C) to signal handler from track.h to use it to stop tracking of projects
signal(SIGINT, handler); signal(SIGINT, handler);
tracking = 0; tracking = 0;
user_name = "Patrick";
// TEST: set up test list // TEST: set up test list
ProjectList proj_list("dec"); ProjectList proj_list("dec");
Project proj("Test Project"); Project proj("Test Project");
@ -22,8 +40,7 @@ int main(){
proj.add_task(task1); proj.add_task(task1);
proj.add_task(task2); proj.add_task(task2);
proj.add_task(task3); proj.add_task(task3);
proj_list.add_project(proj); proj_list.add_project(proj);
init_autocomplete(&proj_list); init_autocomplete(&proj_list);
// use GNU readline for auto completion and history when parsing command input // use GNU readline for auto completion and history when parsing command input
while(1) { while(1) {

6
src/tt.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef TT_H
#define TT_H
#include <string>
extern std::string user_name;
std::string get_date();
#endif

View File

@ -4,10 +4,13 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <algorithm> #include <algorithm>
#include <ctime>
#include <iomanip>
#include "ui.h" #include "ui.h"
#include "project.h" #include "project.h"
#include "track.h" #include "track.h"
#include "string_utils.h" #include "string_utils.h"
#include "tt.h"
// Uses GNU readline library for autocompletion and command history // Uses GNU readline library for autocompletion and command history
@ -22,7 +25,8 @@ std::string command_names[num_commands]={
"rm", "rm",
"report", "report",
"ls", "ls",
"start" "start",
"save"
}; };
std::vector<std::string> autocomplete_names; std::vector<std::string> autocomplete_names;
@ -67,6 +71,7 @@ void init_autocomplete(ProjectList *proj_list){
} }
void parse_input(std::string input, ProjectList *proj_list){ void parse_input(std::string input, ProjectList *proj_list){
input = trim(input);
size_t command_end = input.find(" "); size_t command_end = input.find(" ");
std::string command = input.substr(0,command_end); std::string command = input.substr(0,command_end);
std::string argument = input.substr(command_end+1); 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 // add time to active task
proj_list->active_project->active_task->add_time(wtime); 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 << 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; return;
}else{ }else{
// add time to task in same project // 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; 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; i<proj_list->num_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; j<proj->num_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; i<list->num_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; j<proj->num_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;
}

View File

@ -5,7 +5,7 @@
#include <string> #include <string>
#include "project.h" #include "project.h"
const int num_commands=11; const int num_commands=12;
extern std::string command_names[num_commands]; extern std::string command_names[num_commands];
extern std::vector<std::string> autocomplete_names; extern std::vector<std::string> autocomplete_names;
char **tt_name_completion(const char* text, int start, int end); 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_re(std::string input, std::string new_name, ProjectList *proj_lits);
void command_at(std::string input, int wtime, ProjectList *proj_list); void command_at(std::string input, int wtime, ProjectList *proj_list);
void command_rt(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 #endif