From ab7e6e651d3c993f3058463b9304cf65648be845 Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:44:34 -0400 Subject: [PATCH 1/6] Add a fix language function Added a function to rename the language name to consolidate the same language EX js and javascript to Javascript. Bug report: https://github.com/hackclub/hackatime/issues/402 --- .../api/hackatime/v1/hackatime_controller.rb | 3 ++ lib/wakatime_service.rb | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/controllers/api/hackatime/v1/hackatime_controller.rb b/app/controllers/api/hackatime/v1/hackatime_controller.rb index db4b401f..419d09be 100644 --- a/app/controllers/api/hackatime/v1/hackatime_controller.rb +++ b/app/controllers/api/hackatime/v1/hackatime_controller.rb @@ -239,6 +239,9 @@ def handle_heartbeat(heartbeat_array) # if category is not set, just default to coding heartbeat[:category] ||= "coding" + # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) + heartbeat[:language] = WakatimeService.categorize_language(heartbeat[:language]) if heartbeat[:language].present? + # special case: if the entity is "test.txt", this is a test heartbeat if heartbeat[:entity] == "test.txt" source_type = :test_entry diff --git a/lib/wakatime_service.rb b/lib/wakatime_service.rb index 908de9b9..8b11331f 100644 --- a/lib/wakatime_service.rb +++ b/lib/wakatime_service.rb @@ -128,6 +128,35 @@ def categorize_editor(editor) end end + def self.categorize_language(language) + + # Stole this list of langs from some wikipidia page abt popular langs tehe + # Please double check this list and add others that i missed or added wrong + + return nil if language.blank? + + case language.downcase + when "java" then "Java" + when "javascript", "js" then "JavaScript" + when "typescript", "ts" then "TypeScript" + when "python", "py", "python3" then "Python" + when "c++", "cpp" then "C++" + when "c#", "csharp" then "C#" + when "html" then "HTML" + when "css" then "CSS" + when "json" then "JSON" + when "xml" then "XML" + when "yaml", "yml" then "YAML" + when "markdown", "md" then "Markdown" + when "shell", "bash", "sh" then "Shell" + when "ruby", "rb" then "Ruby" + when "go", "golang" then "Go" + when "rust", "rs" then "Rust" + when "php" then "PHP" + else language.capitalize + end + end + private def convert_to_unix_timestamp(timestamp) From 9b979f4f310aa9771a965b93008946ab2ef021af Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:47:47 -0400 Subject: [PATCH 2/6] Remove white space I literally removed 2 lines of nothing bc the linter was mad Hopefully the linter is happy now --- lib/wakatime_service.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/wakatime_service.rb b/lib/wakatime_service.rb index 8b11331f..22a24fde 100644 --- a/lib/wakatime_service.rb +++ b/lib/wakatime_service.rb @@ -129,10 +129,8 @@ def categorize_editor(editor) end def self.categorize_language(language) - # Stole this list of langs from some wikipidia page abt popular langs tehe # Please double check this list and add others that i missed or added wrong - return nil if language.blank? case language.downcase From 3a67f0b8687ae253095c80869f35b8b433e4f887 Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:50:26 -0400 Subject: [PATCH 3/6] Remove 2 tabs I removed 2 tabs from line 135 The linter was still mad, I hope to all powerful that its happy now --- lib/wakatime_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wakatime_service.rb b/lib/wakatime_service.rb index 22a24fde..100b352e 100644 --- a/lib/wakatime_service.rb +++ b/lib/wakatime_service.rb @@ -132,7 +132,7 @@ def self.categorize_language(language) # Stole this list of langs from some wikipidia page abt popular langs tehe # Please double check this list and add others that i missed or added wrong return nil if language.blank? - + case language.downcase when "java" then "Java" when "javascript", "js" then "JavaScript" From d4a413ff64c6bee8601be7029808136ceed952e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Tue, 5 Aug 2025 19:21:02 -0400 Subject: [PATCH 4/6] Add function to rename languages Added a function to strings to rename the language to a universal name in the main home page --- .../api/hackatime/v1/hackatime_controller.rb | 3 --- app/controllers/static_pages_controller.rb | 21 ++++++++++++++++--- config/initializers/monkey_patches.rb | 9 ++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/hackatime/v1/hackatime_controller.rb b/app/controllers/api/hackatime/v1/hackatime_controller.rb index 419d09be..db4b401f 100644 --- a/app/controllers/api/hackatime/v1/hackatime_controller.rb +++ b/app/controllers/api/hackatime/v1/hackatime_controller.rb @@ -239,9 +239,6 @@ def handle_heartbeat(heartbeat_array) # if category is not set, just default to coding heartbeat[:category] ||= "coding" - # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) - heartbeat[:language] = WakatimeService.categorize_language(heartbeat[:language]) if heartbeat[:language].present? - # special case: if the entity is "test.txt", this is a test heartbeat if heartbeat[:entity] == "test.txt" source_type = :test_entry diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 996a52d8..5e1534ce 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -45,8 +45,10 @@ def index # Process results to get sorted languages and editors language_counts = results - .map { |r| [ r.language&.downcase, r.language_count ] } + .map { |r| [ r.language&.categorize_language, r.language_count ] } # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) .reject { |lang, _| lang.nil? || lang.empty? } + .group_by { |lang, _| lang } + .transform_values { |pairs| pairs.sum { |_, count| count } } .uniq .sort_by { |_, count| -count } @@ -268,7 +270,15 @@ def filterable_dashboard_data 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 == :language + k.categorize_language + elsif %i[operating_system editor].include?(filter) + k.capitalize + else + k + end + } .uniq if params[filter].present? @@ -318,7 +328,12 @@ def filterable_dashboard_data .duration_seconds .each_with_object({}) do |(raw_key, duration), agg| key = raw_key.to_s.presence || "Unknown" - key = key.downcase if %i[editor operating_system].include?(filter) + # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) + if filter == :language + key = key.categorize_language unless key == "Unknown" + elsif %i[editor operating_system].include?(filter) + key = key.downcase + end agg[key] = (agg[key] || 0) + duration end diff --git a/config/initializers/monkey_patches.rb b/config/initializers/monkey_patches.rb index 2cc80e0f..da211c34 100644 --- a/config/initializers/monkey_patches.rb +++ b/config/initializers/monkey_patches.rb @@ -13,3 +13,12 @@ def user Doorkeeper::ApplicationsController.layout "application" # show oauth2 admin in normal hackatime ui end + + + +class String + # Hopefully this is the right place! It a really good monkey patch!! + def categorize_language + WakatimeService.categorize_language(self) + end +end From ac85ed805944f022b5016af54e41e0bf2564e888 Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Tue, 5 Aug 2025 19:22:45 -0400 Subject: [PATCH 5/6] Remove whitespace I removed 3 spaces bc linter was mad --- app/controllers/static_pages_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 5e1534ce..727c08fd 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -45,7 +45,7 @@ def index # Process results to get sorted languages and editors language_counts = results - .map { |r| [ r.language&.categorize_language, r.language_count ] } # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) + .map { |r| [ r.language&.categorize_language, r.language_count ] } # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) .reject { |lang, _| lang.nil? || lang.empty? } .group_by { |lang, _| lang } .transform_values { |pairs| pairs.sum { |_, count| count } } @@ -270,7 +270,7 @@ def filterable_dashboard_data result[filter] = group_by_time.sort_by { |k, v| v } .reverse.map(&:first) .compact_blank - .map { |k| + .map { |k| if filter == :language k.categorize_language elsif %i[operating_system editor].include?(filter) @@ -328,7 +328,7 @@ def filterable_dashboard_data .duration_seconds .each_with_object({}) do |(raw_key, duration), agg| key = raw_key.to_s.presence || "Unknown" - # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) + # fix the bug where langs can have both upper and lower case like JAVA and java found here (https://github.com/hackclub/hackatime/issues/402) if filter == :language key = key.categorize_language unless key == "Unknown" elsif %i[editor operating_system].include?(filter) From b5da309a3a4f04f996e3ff56d8e4f77e72b66071 Mon Sep 17 00:00:00 2001 From: Jeffrey Wang <66625372+JeffreyWangDev@users.noreply.github.com> Date: Tue, 5 Aug 2025 20:50:56 -0400 Subject: [PATCH 6/6] Add name checking in filter Fixed the filters working with the new name thing --- app/controllers/static_pages_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 727c08fd..4d7bf49f 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -287,6 +287,18 @@ def filterable_dashboard_data # search for both lowercase and capitalized versions normalized_arr = filter_arr.flat_map { |v| [ v.downcase, v.capitalize ] }.uniq filtered_heartbeats = filtered_heartbeats.where(filter => normalized_arr) + elsif filter == :language + # find the real name, not the pretty one cause some edditors are stupid and return stuff like JAVASCRIPT and javascript and i need to add stuff to make them both fit the lookup + raw_language_values = [] + current_user.heartbeats.distinct.pluck(filter).compact_blank.each do |raw_lang| + categorized = raw_lang.categorize_language + if filter_arr.include?(categorized) + raw_language_values << raw_lang + end + end + Rails.logger.debug "lang filter: selected=#{filter_arr}, raw_language_values=#{raw_language_values}" # Debug line + + filtered_heartbeats = filtered_heartbeats.where(filter => raw_language_values) if raw_language_values.any? else filtered_heartbeats = filtered_heartbeats.where(filter => filter_arr) end