Add consistent helper to transform language, editor, and OS names

This commit is contained in:
Zach Latta 2025-10-22 15:36:09 +00:00
parent cb61761644
commit 7e52dfb2ca
5 changed files with 88 additions and 21 deletions

View file

@ -179,6 +179,12 @@ class Api::Hackatime::V1::HackatimeController < ApplicationController
# Format the data for each category
category_durations.map do |name, duration|
name = name.presence || "unknown"
name = case category
when "editor" then ApplicationController.helpers.display_editor_name(name)
when "operating_system" then ApplicationController.helpers.display_os_name(name)
when "language" then ApplicationController.helpers.display_language_name(name)
else name
end
percent = ((duration / total_duration) * 100).round(2)
hours = duration.to_i / 3600
minutes = (duration.to_i % 3600) / 60

View file

@ -56,8 +56,8 @@ class StaticPagesController < ApplicationController
.uniq
.sort_by { |_, count| -count }
@todays_languages = language_counts.map(&:first)
@todays_editors = editor_counts.map(&:first)
@todays_languages = language_counts.map(&:first).map { |language| ApplicationController.helpers.display_language_name(language) }
@todays_editors = editor_counts.map(&:first).map { |editor| ApplicationController.helpers.display_editor_name(editor) }
@todays_duration = current_user.heartbeats.today.duration_seconds
if @todays_duration > 1.minute
@ -260,7 +260,17 @@ class StaticPagesController < ApplicationController
result[filter] = group_by_time.sort_by { |k, v| v }
.reverse.map(&:first)
.compact_blank
.map { |k| %i[operating_system editor].include?(filter) ? k.capitalize : k }
.map { |k|
if filter == :editor
ApplicationController.helpers.display_editor_name(k)
elsif filter == :operating_system
ApplicationController.helpers.display_os_name(k)
elsif filter == :language
ApplicationController.helpers.display_language_name(k)
else
k
end
}
.uniq
if params[filter].present?
@ -294,6 +304,11 @@ class StaticPagesController < ApplicationController
&.first
end
# Transform top editor, OS, and language names
result["top_editor"] = ApplicationController.helpers.display_editor_name(result["top_editor"]) if result["top_editor"]
result["top_operating_system"] = ApplicationController.helpers.display_os_name(result["top_operating_system"]) if result["top_operating_system"]
result["top_language"] = ApplicationController.helpers.display_language_name(result["top_language"]) if result["top_language"]
# Prepare project durations data
result[:project_durations] = filtered_heartbeats
.group(:project)
@ -319,7 +334,13 @@ class StaticPagesController < ApplicationController
.sort_by { |_, duration| -duration }
.first(10)
.map { |k, v|
label = %i[language category].include?(filter) ? k : k.capitalize
label = case filter
when :editor then ApplicationController.helpers.display_editor_name(k)
when :operating_system then ApplicationController.helpers.display_os_name(k)
when :language then ApplicationController.helpers.display_language_name(k)
when :category then k
else k.capitalize
end
[ label, v ]
}
.to_h unless result["singular_#{filter}"]

View file

@ -139,4 +139,46 @@ module ApplicationHelper
"All Time"
end
end
def display_editor_name(editor)
return "Unknown" if editor.blank?
case editor.downcase
when "vscode" then "VS Code"
when "pycharm" then "PyCharm"
when "intellij" then "IntelliJ IDEA"
when "webstorm" then "WebStorm"
when "phpstorm" then "PhpStorm"
when "datagrip" then "DataGrip"
when "ktexteditor" then "Kate"
when "android studio" then "Android Studio"
when "visual studio" then "Visual Studio"
when "sublime text" then "Sublime Text"
when "iterm2" then "iTerm2"
else editor.capitalize
end
end
def display_os_name(os)
return "Unknown" if os.blank?
case os.downcase
when "darwin" then "macOS"
when "macos" then "macOS"
else os.capitalize
end
end
def display_language_name(language)
return "Unknown" if language.blank?
case language.downcase
when "typescript" then "TypeScript"
when "javascript" then "JavaScript"
when "json" then "JSON"
when "sql" then "SQL"
when "yaml" then "YAML"
else language.capitalize
end
end
end

View file

@ -27,7 +27,7 @@
<div class="stat-card">
<div class="stat-label">TOP LANGUAGE</div>
<div class="stat-value" data-stat="top_language">
<%= @top_language || "Unknown" %>
<%= display_language_name(@top_language) || "Unknown" %>
<% if @singular_language %>
<span class="super"><%= FlavorText.obvious.sample %></span>
<% end %>
@ -37,7 +37,7 @@
<div class="stat-card">
<div class="stat-label">TOP OS</div>
<div class="stat-value" data-stat="top_operating_system">
<%= @top_operating_system || "Unknown" %>
<%= display_os_name(@top_operating_system) || "Unknown" %>
<% if @singular_operating_system %>
<span class="super"><%= FlavorText.obvious.sample %></span>
<% end %>
@ -47,7 +47,7 @@
<div class="stat-card">
<div class="stat-label">TOP EDITOR</div>
<div class="stat-value" data-stat="top_editor">
<%= @top_editor || "Unknown" %>
<%= display_editor_name(@top_editor) || "Unknown" %>
<% if @singular_editor %>
<span class="super"><%= FlavorText.obvious.sample %></span>
<% end %>

View file

@ -60,7 +60,7 @@ class WakatimeService
result = []
@scope.group(group_by).duration_seconds.each do |key, value|
result << {
name: key.presence || "Other",
name: transform_display_name(group_by, key),
total_seconds: value,
text: ApplicationController.helpers.short_time_simple(value),
hours: value / 3600,
@ -111,20 +111,18 @@ class WakatimeService
{ os: "", editor: "", err: "failed to parse user agent string" }
end
def categorize_os(os)
case os.downcase
when "win" then "Windows"
when "darwin" then "MacOS"
when os.include?("windows") then "Windows"
else os.capitalize
end
end
def categorize_editor(editor)
case editor.downcase
when "vscode" then "VSCode"
when "KTextEditor" then "Kate"
else editor.capitalize
def transform_display_name(group_by, key)
value = key.presence || "Other"
case group_by
when :editor
ApplicationController.helpers.display_editor_name(value)
when :operating_system
ApplicationController.helpers.display_os_name(value)
when :language
ApplicationController.helpers.display_language_name(value)
else
value
end
end