-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve management of list of Assignments
- Implement a cache so we don't have to fetch all tasks on every call - Browse Assigments recursively so we handle TaskGroup (and any other forms of tree if it's ever allowed by Dovico) - Improve formatter so we have a nicer display per day
- Loading branch information
Théophile Helleboid
committed
Nov 4, 2019
1 parent
58408d9
commit dce51f7
Showing
18 changed files
with
597 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
require 'active_support' | ||
require 'active_support/core_ext/hash' | ||
|
||
module Dovico | ||
APP_DIRECTORY = "#{Dir.home}/.dovico/" | ||
end | ||
|
||
require 'dovico/version' | ||
require 'dovico/api_client' | ||
require 'dovico/app' | ||
require 'dovico/config_parser' | ||
require 'dovico/model/assignment' | ||
require 'dovico/model/assignments' | ||
require 'dovico/model/time_entry_formatter' | ||
require 'dovico/model/time_entry_generator' | ||
require 'dovico/model/employee' | ||
require 'dovico/model/project' | ||
require 'dovico/model/task' | ||
require 'dovico/model/task_group' | ||
require 'dovico/model/time_entry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'active_attr' | ||
|
||
module Dovico | ||
class Assignments | ||
include ActiveAttr::Model | ||
|
||
attribute :assignments | ||
|
||
CACHE_FILE = "#{APP_DIRECTORY}/assignments.json" | ||
CACHE_VERSION = "2" | ||
|
||
def initialize(**params) | ||
super(**params) | ||
save_to_cache | ||
end | ||
|
||
def self.load(force_refresh: false) | ||
if !force_refresh | ||
assignments = load_from_cache | ||
end | ||
assignments ||= Assignment.fetch_all | ||
|
||
new(assignments: assignments) | ||
end | ||
|
||
def self.load_from_cache | ||
if File.exists?(CACHE_FILE) && json = JSON.parse(File.read(CACHE_FILE)) | ||
if json["version"] == CACHE_VERSION | ||
Assignment.unserialize(json["assignments"]) | ||
end | ||
end | ||
rescue JSON::ParserError => e | ||
end | ||
private_class_method :load_from_cache | ||
|
||
def format_tree | ||
assignments.map(&:to_s).join("\n") | ||
end | ||
|
||
def find_project_task(project_id, task_id) | ||
project = assignments.find {|assignment| assignment.find_object(Project, project_id) } | ||
task = project.find_object(Task, task_id) | ||
|
||
if project.nil? || task.nil? | ||
raise "Can't find project##{project_id}/task##{task_id}. Try with --force-refresh option" | ||
end | ||
|
||
[project, task] | ||
end | ||
|
||
private | ||
def save_to_cache | ||
cache = { | ||
version: CACHE_VERSION, | ||
assignments: assignments, | ||
} | ||
|
||
FileUtils.mkdir_p(APP_DIRECTORY) | ||
File.write(CACHE_FILE, cache.to_json) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'active_attr' | ||
|
||
module Dovico | ||
class TaskGroup < Assignment | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
module Dovico | ||
class TimeEntryFormatter | ||
|
||
def initialize(projects) | ||
@projects = projects | ||
def initialize(assignments) | ||
@assignments = assignments | ||
end | ||
|
||
def format_entries(time_entries) | ||
text = "" | ||
time_entries.map do |time_entry| | ||
text += "#{}" | ||
time_entry_text(time_entry) | ||
end.join("\n") | ||
time_entries.group_by(&:date).map do |date, day_time_entries| | ||
hours = 0 | ||
|
||
day_time_entries.map do |time_entry| | ||
string = "#{date} #{progress_bar(hours, time_entry.total_hours)} #{time_entry_text(time_entry)}" | ||
hours += time_entry.total_hours.to_f | ||
|
||
string | ||
end | ||
end.flatten.join("\n") | ||
end | ||
|
||
private | ||
attr_accessor :projects | ||
attr_accessor :assignments | ||
|
||
def progress_bar(shift, total_hours) | ||
progress_bar_width = (total_hours.to_f * 2).to_i | ||
sprintf( | ||
"[%- 14s]", " " * shift * 2 + "×" * progress_bar_width | ||
) | ||
end | ||
|
||
def time_entry_text(time_entry) | ||
project, task = project_task(time_entry.project_id, time_entry.task_id) | ||
project, task = assignments.find_project_task(time_entry.project_id, time_entry.task_id) | ||
|
||
progress_bar_width = (time_entry.total_hours.to_f * 2).to_i | ||
sprintf("%s [%s] %s : [%8s] %2sh %s %s", | ||
time_entry.date, | ||
"×" * progress_bar_width, | ||
" " * [16 - progress_bar_width, 0].max, | ||
sprintf("[%-12s] %2sh %s %s", | ||
time_entry.formal_sheet_status, | ||
time_entry.total_hours, | ||
project.name, | ||
task.name, | ||
) | ||
end | ||
|
||
def project_task(project_id, task_id) | ||
project = projects.select{ |project| project.id == project_id }.first | ||
task = project.tasks.select{ |task| task.id == task_id }.first | ||
|
||
[project, task] | ||
end | ||
end | ||
end |
Oops, something went wrong.