Skip to content

Commit 7e52dfb

Browse files
committed
Add consistent helper to transform language, editor, and OS names
1 parent cb61761 commit 7e52dfb

File tree

5 files changed

+88
-21
lines changed

5 files changed

+88
-21
lines changed

app/controllers/api/hackatime/v1/hackatime_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ def calculate_category_stats(heartbeats, category)
179179
# Format the data for each category
180180
category_durations.map do |name, duration|
181181
name = name.presence || "unknown"
182+
name = case category
183+
when "editor" then ApplicationController.helpers.display_editor_name(name)
184+
when "operating_system" then ApplicationController.helpers.display_os_name(name)
185+
when "language" then ApplicationController.helpers.display_language_name(name)
186+
else name
187+
end
182188
percent = ((duration / total_duration) * 100).round(2)
183189
hours = duration.to_i / 3600
184190
minutes = (duration.to_i % 3600) / 60

app/controllers/static_pages_controller.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def index
5656
.uniq
5757
.sort_by { |_, count| -count }
5858

59-
@todays_languages = language_counts.map(&:first)
60-
@todays_editors = editor_counts.map(&:first)
59+
@todays_languages = language_counts.map(&:first).map { |language| ApplicationController.helpers.display_language_name(language) }
60+
@todays_editors = editor_counts.map(&:first).map { |editor| ApplicationController.helpers.display_editor_name(editor) }
6161
@todays_duration = current_user.heartbeats.today.duration_seconds
6262

6363
if @todays_duration > 1.minute
@@ -260,7 +260,17 @@ def filterable_dashboard_data
260260
result[filter] = group_by_time.sort_by { |k, v| v }
261261
.reverse.map(&:first)
262262
.compact_blank
263-
.map { |k| %i[operating_system editor].include?(filter) ? k.capitalize : k }
263+
.map { |k|
264+
if filter == :editor
265+
ApplicationController.helpers.display_editor_name(k)
266+
elsif filter == :operating_system
267+
ApplicationController.helpers.display_os_name(k)
268+
elsif filter == :language
269+
ApplicationController.helpers.display_language_name(k)
270+
else
271+
k
272+
end
273+
}
264274
.uniq
265275

266276
if params[filter].present?
@@ -294,6 +304,11 @@ def filterable_dashboard_data
294304
&.first
295305
end
296306

307+
# Transform top editor, OS, and language names
308+
result["top_editor"] = ApplicationController.helpers.display_editor_name(result["top_editor"]) if result["top_editor"]
309+
result["top_operating_system"] = ApplicationController.helpers.display_os_name(result["top_operating_system"]) if result["top_operating_system"]
310+
result["top_language"] = ApplicationController.helpers.display_language_name(result["top_language"]) if result["top_language"]
311+
297312
# Prepare project durations data
298313
result[:project_durations] = filtered_heartbeats
299314
.group(:project)
@@ -319,7 +334,13 @@ def filterable_dashboard_data
319334
.sort_by { |_, duration| -duration }
320335
.first(10)
321336
.map { |k, v|
322-
label = %i[language category].include?(filter) ? k : k.capitalize
337+
label = case filter
338+
when :editor then ApplicationController.helpers.display_editor_name(k)
339+
when :operating_system then ApplicationController.helpers.display_os_name(k)
340+
when :language then ApplicationController.helpers.display_language_name(k)
341+
when :category then k
342+
else k.capitalize
343+
end
323344
[ label, v ]
324345
}
325346
.to_h unless result["singular_#{filter}"]

app/helpers/application_helper.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,46 @@ def human_interval_name(key, from: nil, to: nil)
139139
"All Time"
140140
end
141141
end
142+
143+
def display_editor_name(editor)
144+
return "Unknown" if editor.blank?
145+
146+
case editor.downcase
147+
when "vscode" then "VS Code"
148+
when "pycharm" then "PyCharm"
149+
when "intellij" then "IntelliJ IDEA"
150+
when "webstorm" then "WebStorm"
151+
when "phpstorm" then "PhpStorm"
152+
when "datagrip" then "DataGrip"
153+
when "ktexteditor" then "Kate"
154+
when "android studio" then "Android Studio"
155+
when "visual studio" then "Visual Studio"
156+
when "sublime text" then "Sublime Text"
157+
when "iterm2" then "iTerm2"
158+
else editor.capitalize
159+
end
160+
end
161+
162+
def display_os_name(os)
163+
return "Unknown" if os.blank?
164+
165+
case os.downcase
166+
when "darwin" then "macOS"
167+
when "macos" then "macOS"
168+
else os.capitalize
169+
end
170+
end
171+
172+
def display_language_name(language)
173+
return "Unknown" if language.blank?
174+
175+
case language.downcase
176+
when "typescript" then "TypeScript"
177+
when "javascript" then "JavaScript"
178+
when "json" then "JSON"
179+
when "sql" then "SQL"
180+
when "yaml" then "YAML"
181+
else language.capitalize
182+
end
183+
end
142184
end

app/views/static_pages/_filterable_dashboard_content.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<div class="stat-card">
2828
<div class="stat-label">TOP LANGUAGE</div>
2929
<div class="stat-value" data-stat="top_language">
30-
<%= @top_language || "Unknown" %>
30+
<%= display_language_name(@top_language) || "Unknown" %>
3131
<% if @singular_language %>
3232
<span class="super"><%= FlavorText.obvious.sample %></span>
3333
<% end %>
@@ -37,7 +37,7 @@
3737
<div class="stat-card">
3838
<div class="stat-label">TOP OS</div>
3939
<div class="stat-value" data-stat="top_operating_system">
40-
<%= @top_operating_system || "Unknown" %>
40+
<%= display_os_name(@top_operating_system) || "Unknown" %>
4141
<% if @singular_operating_system %>
4242
<span class="super"><%= FlavorText.obvious.sample %></span>
4343
<% end %>
@@ -47,7 +47,7 @@
4747
<div class="stat-card">
4848
<div class="stat-label">TOP EDITOR</div>
4949
<div class="stat-value" data-stat="top_editor">
50-
<%= @top_editor || "Unknown" %>
50+
<%= display_editor_name(@top_editor) || "Unknown" %>
5151
<% if @singular_editor %>
5252
<span class="super"><%= FlavorText.obvious.sample %></span>
5353
<% end %>

lib/wakatime_service.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def generate_summary_chunk(group_by)
6060
result = []
6161
@scope.group(group_by).duration_seconds.each do |key, value|
6262
result << {
63-
name: key.presence || "Other",
63+
name: transform_display_name(group_by, key),
6464
total_seconds: value,
6565
text: ApplicationController.helpers.short_time_simple(value),
6666
hours: value / 3600,
@@ -111,20 +111,18 @@ def self.parse_user_agent(user_agent)
111111
{ os: "", editor: "", err: "failed to parse user agent string" }
112112
end
113113

114-
def categorize_os(os)
115-
case os.downcase
116-
when "win" then "Windows"
117-
when "darwin" then "MacOS"
118-
when os.include?("windows") then "Windows"
119-
else os.capitalize
120-
end
121-
end
122114

123-
def categorize_editor(editor)
124-
case editor.downcase
125-
when "vscode" then "VSCode"
126-
when "KTextEditor" then "Kate"
127-
else editor.capitalize
115+
def transform_display_name(group_by, key)
116+
value = key.presence || "Other"
117+
case group_by
118+
when :editor
119+
ApplicationController.helpers.display_editor_name(value)
120+
when :operating_system
121+
ApplicationController.helpers.display_os_name(value)
122+
when :language
123+
ApplicationController.helpers.display_language_name(value)
124+
else
125+
value
128126
end
129127
end
130128

0 commit comments

Comments
 (0)