Class: JekyllStats::StatsCalculator
- Inherits:
-
Object
- Object
- JekyllStats::StatsCalculator
- Defined in:
- lib/jekyll-stats/stats_calculator.rb
Constant Summary collapse
- WORDS_PER_MINUTE =
200
Instance Attribute Summary collapse
-
#include_drafts ⇒ Object
readonly
Returns the value of attribute include_drafts.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
- #calculate ⇒ Object
- #category_counts(posts) ⇒ Object
- #collect_posts ⇒ Object
- #drafts_count ⇒ Object
- #empty_stats ⇒ Object
-
#initialize(site, include_drafts: false) ⇒ StatsCalculator
constructor
A new instance of StatsCalculator.
- #post_info(post, words) ⇒ Object
- #post_info_with_date(post) ⇒ Object
- #posts_by_day_of_week(posts) ⇒ Object
- #posts_by_month(posts) ⇒ Object
- #posts_by_year(posts) ⇒ Object
- #posts_per_month(count, first_date, last_date) ⇒ Object
- #tag_counts(posts) ⇒ Object
- #word_count(post) ⇒ Object
- #years_active(first_date, last_date) ⇒ Object
Constructor Details
#initialize(site, include_drafts: false) ⇒ StatsCalculator
Returns a new instance of StatsCalculator.
9 10 11 12 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 9 def initialize(site, include_drafts: false) @site = site @include_drafts = include_drafts end |
Instance Attribute Details
#include_drafts ⇒ Object (readonly)
Returns the value of attribute include_drafts.
7 8 9 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 7 def include_drafts @include_drafts end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
7 8 9 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 7 def site @site end |
Instance Method Details
#calculate ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 14 def calculate posts = collect_posts return empty_stats if posts.empty? word_counts = posts.map { |p| [p, word_count(p)] } sorted_by_words = word_counts.sort_by { |_, count| -count } sorted_by_date = posts.sort_by { |p| p.date } total_words = word_counts.sum { |_, count| count } dates = sorted_by_date.map(&:date) { generated_at: Time.now.utc.iso8601, total_posts: posts.size, total_words: total_words, reading_minutes: (total_words / WORDS_PER_MINUTE.to_f).ceil, average_words: (total_words / posts.size.to_f).round, longest_post: post_info(sorted_by_words.first[0], sorted_by_words.first[1]), shortest_post: post_info(sorted_by_words.last[0], sorted_by_words.last[1]), first_post: post_info_with_date(sorted_by_date.first), last_post: post_info_with_date(sorted_by_date.last), years_active: years_active(dates.first, dates.last), posts_per_month: posts_per_month(posts.size, dates.first, dates.last), posts_by_year: posts_by_year(posts), posts_by_month: posts_by_month(posts), posts_by_day_of_week: posts_by_day_of_week(posts), tags: tag_counts(posts), categories: category_counts(posts), drafts_count: drafts_count } end |
#category_counts(posts) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 121 def category_counts(posts) counts = Hash.new(0) posts.each do |post| categories = post.data["categories"] || [] categories.each { |cat| counts[cat] += 1 } end counts.sort_by { |_, count| -count } .map { |name, count| { name: name, count: count } } end |
#collect_posts ⇒ Object
46 47 48 49 50 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 46 def collect_posts posts = site.posts.docs.dup posts += site.drafts if include_drafts && site.respond_to?(:drafts) posts end |
#drafts_count ⇒ Object
131 132 133 134 135 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 131 def drafts_count return 0 unless site.respond_to?(:drafts) && site.drafts site.drafts.size end |
#empty_stats ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 137 def empty_stats { generated_at: Time.now.utc.iso8601, total_posts: 0, total_words: 0, reading_minutes: 0, average_words: 0, longest_post: nil, shortest_post: nil, first_post: nil, last_post: nil, years_active: 0, posts_per_month: 0, posts_by_year: [], posts_by_month: [], posts_by_day_of_week: %w[sunday monday tuesday wednesday thursday friday saturday].each_with_object({}) { |d, h| h[d] = 0 }, tags: [], categories: [], drafts_count: 0 } end |
#post_info(post, words) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 62 def post_info(post, words) { title: post.data["title"] || "(untitled)", url: post.url, words: words } end |
#post_info_with_date(post) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 70 def post_info_with_date(post) { title: post.data["title"] || "(untitled)", url: post.url, date: post.date.strftime("%Y-%m-%d") } end |
#posts_by_day_of_week(posts) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 104 def posts_by_day_of_week(posts) days = %w[sunday monday tuesday wednesday thursday friday saturday] counts = Hash.new(0) posts.each { |p| counts[days[p.date.wday]] += 1 } days.each_with_object({}) { |day, h| h[day] = counts[day] } end |
#posts_by_month(posts) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 96 def posts_by_month(posts) counts = posts.group_by { |p| p.date.strftime("%Y-%m") } .transform_values(&:size) .sort_by { |month, _| month } .reverse counts.map { |month, count| { month: month, count: count } } end |
#posts_by_year(posts) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 89 def posts_by_year(posts) counts = posts.group_by { |p| p.date.year } .transform_values(&:size) .sort_by { |year, _| -year } counts.map { |year, count| { year: year, count: count } } end |
#posts_per_month(count, first_date, last_date) ⇒ Object
84 85 86 87 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 84 def posts_per_month(count, first_date, last_date) months = ((last_date.year - first_date.year) * 12) + (last_date.month - first_date.month) + 1 (count / months.to_f).round(1) end |
#tag_counts(posts) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 111 def tag_counts(posts) counts = Hash.new(0) posts.each do |post| = post.data["tags"] || [] .each { |tag| counts[tag] += 1 } end counts.sort_by { |_, count| -count } .map { |name, count| { name: name, count: count } } end |
#word_count(post) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 52 def word_count(post) content = post.content.to_s text = content.gsub(/<[^>]*>/, " ") text = text.gsub(/```[\s\S]*?```/, " ") text = text.gsub(/`[^`]*`/, " ") text = text.gsub(/\[([^\]]*)\]\([^)]*\)/, '\1') text = text.gsub(/[#*_~`]/, "") text.split(/\s+/).count { |w| w.match?(/\w/) } end |
#years_active(first_date, last_date) ⇒ Object
78 79 80 81 82 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 78 def years_active(first_date, last_date) seconds = (last_date - first_date).to_f days = seconds / 86400.0 (days / 365.25).round(1) end |