Class: Fuso::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/fuso/session.rb

Constant Summary collapse

SESSIONS_FILE =
File.join(Config::CONFIG_DIR, "sessions.json")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



14
15
16
# File 'lib/fuso/session.rb', line 14

def initialize
  @entries = {} # date_string => [entry, ...]
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



12
13
14
# File 'lib/fuso/session.rb', line 12

def entries
  @entries
end

Class Method Details

.format_decimal_hours(seconds) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/fuso/session.rb', line 138

def self.format_decimal_hours(seconds)
  return "" if seconds.to_i == 0

  hours = (seconds.to_f / 3600).round(2)
  return "" if hours == 0.0

  hours == hours.to_i ? hours.to_i.to_s : format("%.2f", hours).sub(/0$/, "")
end

.format_duration(seconds) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/fuso/session.rb', line 123

def self.format_duration(seconds)
  seconds = seconds.to_i
  hours = seconds / 3600
  minutes = (seconds % 3600) / 60
  secs = seconds % 60
  format("%02d:%02d:%02d", hours, minutes, secs)
end

.format_duration_hm(seconds) ⇒ Object



131
132
133
134
135
136
# File 'lib/fuso/session.rb', line 131

def self.format_duration_hm(seconds)
  seconds = seconds.to_i
  hours = seconds / 3600
  minutes = (seconds % 3600) / 60
  format("%02d:%02d", hours, minutes)
end

.loadObject



18
19
20
21
22
23
24
25
# File 'lib/fuso/session.rb', line 18

def self.load
  session = new
  if File.exist?(SESSIONS_FILE)
    data = JSON.parse(File.read(SESSIONS_FILE))
    session.instance_variable_set(:@entries, data)
  end
  session
end

.week_ending_dates_for_month(date) ⇒ Object

Returns an array of week-ending dates (Sundays) for the given month. If the month ends mid-week, the last day of month is included as the final ending date.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fuso/session.rb', line 149

def self.week_ending_dates_for_month(date)
  first_day = Date.new(date.year, date.month, 1)
  last_day = Date.new(date.year, date.month, -1)

  dates = []
  d = first_day
  d += 1 until d.sunday?

  while d <= last_day
    dates << d
    d += 7
  end

  # If last day is not a Sunday, add it as the final week-ending date
  if dates.empty? || dates.last < last_day
    dates << last_day
  end

  dates
end

Instance Method Details

#entries_for_month(date) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/fuso/session.rb', line 69

def entries_for_month(date)
  year = date.year
  month = date.month
  result = []
  @entries.each do |date_str, day_entries|
    d = Date.parse(date_str)
    result.concat(day_entries) if d.year == year && d.month == month
  end
  result
end

#entries_for_week(date) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/fuso/session.rb', line 59

def entries_for_week(date)
  monday = date - (date.wday == 0 ? 6 : date.wday - 1)
  sunday = monday + 6
  result = []
  (monday..sunday).each do |d|
    result.concat(@entries[d.to_s] || [])
  end
  result
end

#monthly_breakdown(date) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fuso/session.rb', line 94

def monthly_breakdown(date)
  month_entries = entries_for_month(date)
  num_weeks = weeks_in_month(date)

  # Build: { [project, category] => { week_num => seconds, ... } }
  breakdown = Hash.new { |h, k| h[k] = Hash.new(0) }

  month_entries.each do |entry|
    d = Date.parse(entry["started_at"][0..9])
    wn = week_number_in_month(d)
    key = [entry["project"], entry["category"]]
    breakdown[key][wn] += entry["elapsed_seconds"]
  end

  { breakdown: breakdown, num_weeks: num_weeks }
end

#monthly_breakdown_by_week_ending(date) ⇒ Object

Returns { week_ending_date => { [project, category] => seconds } }



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fuso/session.rb', line 171

def monthly_breakdown_by_week_ending(date)
  month_entries = entries_for_month(date)
  ending_dates = self.class.week_ending_dates_for_month(date)

  breakdown = Hash.new { |h, k| h[k] = Hash.new(0) }

  month_entries.each do |entry|
    d = Date.parse(entry["started_at"][0..9])
    # Find the first week-ending date >= this day
    we = ending_dates.find { |ed| ed >= d } || ending_dates.last
    key = [entry["project"], entry["category"]]
    breakdown[we][key] += entry["elapsed_seconds"]
  end

  { breakdown: breakdown, ending_dates: ending_dates }
end

#record(project:, category:, started_at:, ended_at:, elapsed_seconds:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fuso/session.rb', line 32

def record(project:, category:, started_at:, ended_at:, elapsed_seconds:)
  date_key = started_at.strftime("%Y-%m-%d")
  @entries[date_key] ||= []
  @entries[date_key] << {
    "project" => project,
    "category" => category,
    "started_at" => started_at.iso8601,
    "ended_at" => ended_at.iso8601,
    "elapsed_seconds" => elapsed_seconds.to_i
  }
  save
end

#saveObject



27
28
29
30
# File 'lib/fuso/session.rb', line 27

def save
  FileUtils.mkdir_p(Config::CONFIG_DIR)
  File.write(SESSIONS_FILE, JSON.pretty_generate(@entries))
end

#today_entriesObject



45
46
47
# File 'lib/fuso/session.rb', line 45

def today_entries
  @entries[Date.today.to_s] || []
end

#today_seconds_for(project, category) ⇒ Object



53
54
55
56
57
# File 'lib/fuso/session.rb', line 53

def today_seconds_for(project, category)
  today_entries
    .select { |e| e["project"] == project && e["category"] == category }
    .sum { |e| e["elapsed_seconds"] }
end

#today_total_secondsObject



49
50
51
# File 'lib/fuso/session.rb', line 49

def today_total_seconds
  today_entries.sum { |e| e["elapsed_seconds"] }
end

#week_number_in_month(date) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/fuso/session.rb', line 80

def week_number_in_month(date)
  first_day = Date.new(date.year, date.month, 1)
  first_monday = first_day + ((1 - first_day.wday) % 7)
  first_monday = first_day if first_day.wday == 1
  return 1 if date < first_monday

  ((date - first_monday).to_i / 7) + 1
end

#weekly_breakdown(date) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fuso/session.rb', line 111

def weekly_breakdown(date)
  week_entries = entries_for_week(date)
  totals = Hash.new(0)

  week_entries.each do |entry|
    key = [entry["project"], entry["category"]]
    totals[key] += entry["elapsed_seconds"]
  end

  totals
end

#weeks_in_month(date) ⇒ Object



89
90
91
92
# File 'lib/fuso/session.rb', line 89

def weeks_in_month(date)
  last_day = Date.new(date.year, date.month, -1)
  week_number_in_month(last_day)
end