Skip to content

Add a fix language function #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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?
Expand All @@ -277,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
Expand Down Expand Up @@ -318,7 +340,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

Expand Down
9 changes: 9 additions & 0 deletions config/initializers/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions lib/wakatime_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ 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)
Expand Down