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
-
#filter_tags ⇒ Object
readonly
Returns the value of attribute filter_tags.
-
#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
- #external_links(posts) ⇒ Object
- #filter_posts_by_tags(posts) ⇒ Object
-
#initialize(site, include_drafts: false, filter_tags: nil) ⇒ StatsCalculator
constructor
A new instance of StatsCalculator.
- #internal_links(posts) ⇒ Object
- #normalize_filter_tags(tags) ⇒ Object
- #normalize_tag(tag) ⇒ Object
- #normalize_url(url) ⇒ Object
- #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, filter_tags: nil) ⇒ StatsCalculator
Returns a new instance of StatsCalculator.
11 12 13 14 15 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 11 def initialize(site, include_drafts: false, filter_tags: nil) @site = site @include_drafts = include_drafts @filter_tags = () end |
Instance Attribute Details
#filter_tags ⇒ Object (readonly)
Returns the value of attribute filter_tags.
9 10 11 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 9 def @filter_tags end |
#include_drafts ⇒ Object (readonly)
Returns the value of attribute include_drafts.
9 10 11 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 9 def include_drafts @include_drafts end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
9 10 11 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 9 def site @site end |
Instance Method Details
#calculate ⇒ Object
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 45 46 47 48 49 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 17 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), internal_links: internal_links(posts), external_links: external_links(posts), drafts_count: drafts_count } end |
#category_counts(posts) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 144 def category_counts(posts) counts = Hash.new(0) posts.each do |post| categories = post.data["categories"] || [] categories.each { |cat| counts[normalize_tag(cat)] += 1 } end counts.sort_by { |_, count| -count } .map { |name, count| { name: name, count: count } } end |
#collect_posts ⇒ Object
51 52 53 54 55 56 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 51 def collect_posts posts = site.posts.docs.dup posts += site.drafts if include_drafts && site.respond_to?(:drafts) posts = (posts) if posts end |
#drafts_count ⇒ Object
222 223 224 225 226 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 222 def drafts_count return 0 unless site.respond_to?(:drafts) && site.drafts site.drafts.size end |
#empty_stats ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 228 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: [], internal_links: [], external_links: { total: 0, unique: 0, domains: [] }, drafts_count: 0 } end |
#external_links(posts) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 186 def external_links(posts) exclude = Array(site.config.dig("jekyll-stats", "link_source_exclude_tags")).map { |t| normalize_tag(t) } site_host = (URI.parse(site.config["url"].to_s).host rescue nil) urls = [] posts.each do |post| = (post.data["tags"] || []).map { |t| normalize_tag(t) } next if exclude.any? && (exclude & ).any? post.content.to_s.scan(%r{(?:\]\(|<a\s[^>]*href=["'])(https?://[^"'\s)]+)}i).flatten.each do |href| host = URI.parse(href).host rescue nil next if host.nil? next if site_host && host == site_host urls << [href, host, post] end end unique = urls.uniq { |u, _, _| u } domains = urls.group_by { |_, h, _| h } .map { |host, us| { host: host, count: us.map { |u, _, _| u }.uniq.size, posts: us.map { |_, _, p| p }.uniq.size } } .sort_by { |d| [-d[:count], d[:host]] } { total: urls.size, unique: unique.size, domains: domains } end |
#filter_posts_by_tags(posts) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 58 def (posts) posts.select do |post| = (post.data["tags"] || []).map { |t| normalize_tag(t) } ( & ).any? end end |
#internal_links(posts) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 154 def internal_links(posts) by_url = posts.each_with_object({}) { |p, h| h[normalize_url(p.url)] = p } exclude = Array(site.config.dig("jekyll-stats", "link_source_exclude_tags")).map { |t| normalize_tag(t) } site_url = site.config["url"].to_s inbound = Hash.new { |h, k| h[k] = [] } posts.each do |post| = (post.data["tags"] || []).map { |t| normalize_tag(t) } next if exclude.any? && (exclude & ).any? post.content.to_s.scan(%r{(?:\]\(|<a\s[^>]*href=["'])([^"'\s)]+)}i).flatten.each do |href| href = href.delete_prefix(site_url) unless site_url.empty? next unless href.start_with?("/") target = by_url[normalize_url(href)] next unless target next if target == post inbound[target] << post end end inbound.map { |target, sources| { url: target.url, title: target.data["title"] || "(untitled)", inbound_count: sources.uniq.size, inbound_from: sources.uniq.map(&:url).sort } }.sort_by { |r| [-r[:inbound_count], r[:url]] } end |
#normalize_filter_tags(tags) ⇒ Object
65 66 67 68 69 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 65 def () return nil if .nil? || .empty? .map { |t| normalize_tag(t) } end |
#normalize_tag(tag) ⇒ Object
140 141 142 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 140 def normalize_tag(tag) tag.to_s.strip.gsub(/[,;:]+\z/, "").strip end |
#normalize_url(url) ⇒ Object
218 219 220 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 218 def normalize_url(url) url.sub(/[#?].*\z/, "").sub(%r{/index\.html\z}, "").chomp(".html").chomp("/") end |
#post_info(post, words) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 81 def post_info(post, words) { title: post.data["title"] || "(untitled)", url: post.url, words: words } end |
#post_info_with_date(post) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 89 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
123 124 125 126 127 128 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 123 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
115 116 117 118 119 120 121 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 115 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
108 109 110 111 112 113 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 108 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
103 104 105 106 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 103 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
130 131 132 133 134 135 136 137 138 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 130 def tag_counts(posts) counts = Hash.new(0) posts.each do |post| = post.data["tags"] || [] .each { |tag| counts[normalize_tag(tag)] += 1 } end counts.sort_by { |_, count| -count } .map { |name, count| { name: name, count: count } } end |
#word_count(post) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 71 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
97 98 99 100 101 |
# File 'lib/jekyll-stats/stats_calculator.rb', line 97 def years_active(first_date, last_date) seconds = (last_date - first_date).to_f days = seconds / 86400.0 (days / 365.25).round(1) end |