From b9fb8a55fe1284010d05f59e27472c4d14c06bfc Mon Sep 17 00:00:00 2001 From: Ryan Kulp Date: Fri, 25 Oct 2024 09:36:00 -0700 Subject: [PATCH] update GitHub commit graph with more stats --- lib/github_commit_graph/_common.html.erb | 64 +++++++++++ .../github_commit_graph.rb | 83 +++++++++++--- lib/github_commit_graph/markup_full.html.erb | 108 +++++++++--------- 3 files changed, 181 insertions(+), 74 deletions(-) create mode 100644 lib/github_commit_graph/_common.html.erb diff --git a/lib/github_commit_graph/_common.html.erb b/lib/github_commit_graph/_common.html.erb new file mode 100644 index 0000000..dd508d9 --- /dev/null +++ b/lib/github_commit_graph/_common.html.erb @@ -0,0 +1,64 @@ + \ No newline at end of file diff --git a/lib/github_commit_graph/github_commit_graph.rb b/lib/github_commit_graph/github_commit_graph.rb index 035af67..c63a9ac 100644 --- a/lib/github_commit_graph/github_commit_graph.rb +++ b/lib/github_commit_graph/github_commit_graph.rb @@ -1,7 +1,7 @@ module Plugins class GithubCommitGraph < Base def locals - { username:, contributions: } + { username:, contributions:, stats: } end private @@ -9,35 +9,80 @@ module Plugins def username = settings['username'] def contributions - query = "query($userName:String!) { - user(login: $userName){ - contributionsCollection { - contributionCalendar { - totalContributions - weeks { - contributionDays { - contributionCount - date + @contributions ||= begin + query = "query($userName:String!) { + user(login: $userName){ + contributionsCollection { + contributionCalendar { + totalContributions + weeks { + contributionDays { + contributionCount + date + } } } } } + }" + body = { + query: query, + variables: { userName: settings['username'] } } - }" - body = { - query: query, - variables: { userName: settings['username'] } - } - url = 'https://api.github.com/graphql' - resp = HTTParty.post(url, body: body.to_json, headers: headers) - data = resp.dig('data', 'user', 'contributionsCollection', 'contributionCalendar') + url = 'https://api.github.com/graphql' + resp = HTTParty.post(url, body: body.to_json, headers: headers) + data = resp.dig('data', 'user', 'contributionsCollection', 'contributionCalendar') - { total: data['totalContributions'], commits: data['weeks'] } + { + total: data['totalContributions'], + commits: data['weeks'] + } + end end def headers { 'authorization' => "Bearer #{Rails.application.credentials.plugins.github_commit_graph_token}" } end + + def stats + days = contributions[:commits].flat_map { |week| week['contributionDays'] } + sorted_days = days.sort_by { |day| Date.parse(day['date']) } + + { + longest_streak: longest_streak(sorted_days), + current_streak: current_streak(sorted_days), + max_contributions: days.map { |day| day['contributionCount'] }.max, + average_contributions: average_contributions(days) + } + end + + def average_contributions(days) + total_contributions = days.sum { |day| day['contributionCount'] } + (total_contributions.to_f / days.size).round(2) + end + + def longest_streak(days) + longest = current = 0 + days.each do |day| + if (day['contributionCount']).positive? + current += 1 + longest = [longest, current].max + else + current = 0 + end + end + longest + end + + def current_streak(days) + streak = 0 + days.reverse_each do |day| + break if (day['contributionCount']).zero? + + streak += 1 + end + streak + end end end diff --git a/lib/github_commit_graph/markup_full.html.erb b/lib/github_commit_graph/markup_full.html.erb index 76c3bce..f3d6dc6 100644 --- a/lib/github_commit_graph/markup_full.html.erb +++ b/lib/github_commit_graph/markup_full.html.erb @@ -1,23 +1,61 @@ -
-
-
-
-
-
- <%= contributions[:total] %> - Contributions in last year +
+
+
+
+
+
+ <%= contributions[:total] %> + Contributions in last year +
+
+
+
+
+
+
+ <%= stats[:longest_streak] %> + Longest streak +
+
+
+
+
+ <%= stats[:current_streak] %> + Current streak +
-
- <% contributions[:commits].each do |week_of_commits| %> - <% week_of_commits['contributionDays'].each do |day| %> - - <% end %> - <% end %> +
+ +
+
+
+
+ <%= stats[:max_contributions] %> + Most in a day +
+
+
+
+
+ <%= stats[:average_contributions] %> + Average per day +
+
+ +
+ +
+ <% contributions[:commits].each do |week_of_commits| %> + <% week_of_commits['contributionDays'].each do |day| %> + + <% end %> + <% end %> +
@@ -27,44 +65,4 @@
- +<%= render 'common' %>