module Timeless::Storage
Constants
- TIMELESS_FILE
Public Class Methods
balance(from, to, filter)
click to toggle source
# File lib/timeless/storage.rb, line 44 def self.balance from, to, filter # hash of all keys defined in file array = get_tags hash = Hash[array.collect { |item| [item, {}] } ] entries = Timeless::Storage.get(from, to, filter) entries.each do |entry| start = Time.parse(entry[0]) stop = Time.parse(entry[1]) duration = (stop - start) / 60 hash.keys.each do |key| values = extract_kpv key, entry[2] values.each do |value| hash[key][value] = (hash[key][value] || 0) + duration end end end hash end
export()
click to toggle source
# File lib/timeless/storage.rb, line 64 def self.export entries = my_read(TIMELESS_FILE) CSV { |csvout| csvout << ["Start Date", "Start Time", "End Date", "End Time", "Duration (s)", "Project", "Client", "Activity", "Notes"] } CSV do |csvout| entries.each do |entry| if entry[0] and entry[1] start = Time.parse(entry[0]) stop = Time.parse(entry[1]) start_date = start.strftime("%Y-%m-%d") start_time = start.strftime("%H:%M:%S") stop_date = stop.strftime("%Y-%m-%d") stop_time = stop.strftime("%H:%M:%S") duration = stop - start # extract first project, first client, and first activity, if present project = extract_kpv("p", entry[2]).first client = extract_kpv("c", entry[2]).first activity = extract_kpv("a", entry[2]).first notes = entry[2] csvout << [start_date, start_time, stop_date, stop_time, duration, project, client, activity, notes] end end end end
get(from_date=nil, to_date=nil, filter=nil)
click to toggle source
# File lib/timeless/storage.rb, line 17 def self.get from_date=nil, to_date=nil, filter=nil entries = my_read(TIMELESS_FILE) entries.select do |x| (from_date ? Time.parse(x[0]) >= from_date : true) and (to_date ? Time.parse(x[1]) <= to_date : true) and (filter ? x[2].include?(filter) : true) end end
get_key(tag)
click to toggle source
get all unique keys of a given tag in file
# File lib/timeless/storage.rb, line 39 def self.get_key tag entries = my_read(TIMELESS_FILE) entries.map { |x| extract_kpv tag, x[2] }.flatten.uniq.sort end
last()
click to toggle source
# File lib/timeless/storage.rb, line 13 def self.last my_read(TIMELESS_FILE).sort { |x, y| x[1] <=> y[1] }.last end
store(start, stop, notes)
click to toggle source
# File lib/timeless/storage.rb, line 7 def self.store start, stop, notes CSV.open(TIMELESS_FILE, "a") do |csv| csv << [start, stop, notes] end end
Private Class Methods
extract_kpv(key, string)
click to toggle source
return all the values of a given key in string
# File lib/timeless/storage.rb, line 103 def self.extract_kpv key, string (string || "").scan(/(#{key}):([^ ]+)/).map { |x| x[1] } end
my_read(timeless_file)
click to toggle source
read the timeless file into a CSV skipping lines with empty start end stop time
# File lib/timeless/storage.rb, line 98 def self.my_read timeless_file CSV.read(timeless_file).select { |x| x[0] != nil and x[1] != nil } end