Skip to content

Commit 72bb02e

Browse files
author
Stephen Fiser
committed
wip
1 parent 2ffd3f4 commit 72bb02e

File tree

14 files changed

+140
-10
lines changed

14 files changed

+140
-10
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ end
6060
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
6161
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
6262

63-
gem "stimulus_reflex", "~> 3.2"
63+
gem "stimulus_reflex", "3.3.0.pre2"
6464

6565
gem "optimism", "~> 0.3.3"

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ GEM
191191
activesupport (>= 4.0)
192192
sprockets (>= 3.0.0)
193193
sqlite3 (1.4.2)
194-
stimulus_reflex (3.2.3)
194+
stimulus_reflex (3.3.0.pre2)
195195
cable_ready (>= 4.1.2)
196196
nokogiri
197197
rack
@@ -246,7 +246,7 @@ DEPENDENCIES
246246
spring
247247
spring-watcher-listen (~> 2.0.0)
248248
sqlite3 (~> 1.4)
249-
stimulus_reflex (~> 3.2)
249+
stimulus_reflex (= 3.3.0.pre2)
250250
turbolinks (~> 5)
251251
tzinfo-data
252252
web-console (>= 3.3.0)

app/assets/stylesheets/stats.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Stats controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: https://sass-lang.com/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Authors
2+
class StatsController < AuthorsController
3+
def index
4+
@count_by_date = PageView.count_by_date(7.days.ago, Date.today)
5+
@unique_count_by_date = PageView.uniq_count_by_date(7.days.ago, Date.today)
6+
end
7+
end
8+
end

app/helpers/stats_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module StatsHelper
2+
end

app/javascript/packs/application.js

+33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ require("channels")
2020
const Trix = require("trix")
2121
require("@rails/actiontext")
2222

23+
const Chart = require('chart.js')
24+
2325
Trix.config.blockAttributes.heading1.tagName = "h3";
2426

2527
import Sortable from 'sortablejs'
@@ -28,6 +30,37 @@ import "controllers"
2830
import '../stylesheets/application'
2931

3032
document.addEventListener('turbolinks:load', () => {
33+
34+
var ctx = document.getElementById('page-views');
35+
if (ctx) {
36+
var myChart = new Chart(ctx, {
37+
type: 'line',
38+
data: {
39+
labels: JSON.parse(ctx.dataset.labels),
40+
datasets: [{
41+
label: 'Page Views',
42+
data: JSON.parse(ctx.dataset.data),
43+
borderWidth: 1
44+
}]
45+
},
46+
});
47+
}
48+
49+
var ctx = document.getElementById('unique-page-views');
50+
if (ctx) {
51+
var myChart = new Chart(ctx, {
52+
type: 'line',
53+
data: {
54+
labels: JSON.parse(ctx.dataset.labels),
55+
datasets: [{
56+
label: 'Unique Page Views',
57+
data: JSON.parse(ctx.dataset.data),
58+
borderWidth: 1
59+
}]
60+
},
61+
});
62+
}
63+
3164
document.addEventListener('click', () => {
3265
let element = event.target.closest('.paragraph-content')
3366
if (!element) return;

app/models/page_view.rb

+23-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@ class PageView < Impression
1212
where(created_at: start_date.beginning_of_day..end_date.end_of_day)
1313
end
1414

15-
scope :count_by_date, -> () do
16-
group('date(created_at)').count
15+
def self.count_by_date(start_date, end_date)
16+
results = for_date_range(start_date, end_date)
17+
.group('date(created_at)').count
18+
19+
with_zeros(start_date, end_date, results)
20+
end
21+
22+
def self.uniq_count_by_date(start_date, end_date)
23+
results = for_date_range(start_date, end_date)
24+
.select(:ip_address)
25+
.distinct
26+
.group('date(created_at)').count
27+
28+
with_zeros(start_date, end_date, results)
1729
end
1830

19-
scope :uniq_count_by_date, -> () do
20-
select(:ip_address).distinct.group('date(created_at)').count
31+
def self.with_zeros(start_date, end_date, results_hash)
32+
date_range = (start_date.to_date..end_date.to_date).to_a.map do |date|
33+
date.strftime('%Y-%m-%d')
34+
end
35+
hash_with_zeros = {}
36+
date_range.each do |date|
37+
hash_with_zeros[date] = results_hash[date] || 0
38+
end
39+
hash_with_zeros
2140
end
2241

2342
end

app/reflexes/elements_reflex.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ def sort
66
element_record = Element.find(element['id'])
77
element_record.update(position: element['position'])
88
end
9-
# NOTE: This is a hack! Might break with future versions of StimulusReflex!
10-
@halted = true
9+
morph :nothing
1110
end
1211

1312
end
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h4 class='mb-4'>
2+
<span>Stats</span>
3+
</h4>
4+
<div class="row">
5+
<div class="col-md-6">
6+
<div class="card">
7+
<div class="card-header">
8+
7 Day Views
9+
</div>
10+
<div class="card-body">
11+
<canvas id="page-views" width="400" height="400" data-labels="<%= @count_by_date.keys %>" data-data="<%= @count_by_date.values %>"></canvas>
12+
</div>
13+
</div>
14+
</div>
15+
<div class="col-md-6">
16+
<div class="card">
17+
<div class="card-header">
18+
7 Day Unique Views
19+
</div>
20+
<div class="card-body">
21+
<canvas id="unique-page-views" width="400" height="400" data-labels="<%= @unique_count_by_date.keys %>" data-data="<%= @unique_count_by_date.values %>"></canvas>
22+
</div>
23+
</div>
24+
</div>
25+
</div>

config/application.rb

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module SimpleBlog
1010
class Application < Rails::Application
1111
# Initialize configuration defaults for originally generated Rails version.
1212
config.load_defaults 6.0
13+
config.time_zone = 'Central Time (US & Canada)'
14+
config.active_record.default_timezone = :local
1315

1416
# Settings in config/environments/* take precedence over those specified here.
1517
# Application configuration can go into files in config/initializers

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
get '/blog/:id' => 'readers/posts#show', as: :blog_post
77

88
scope module: 'authors' do
9+
get 'stats' => 'stats#index'
910
resources :posts do
1011
resources :elements
1112
end

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@rails/ujs": "^6.0.0",
99
"@rails/webpacker": "4.2.2",
1010
"cable_ready": "^4.3.0-pre1",
11+
"chart.js": "^2.9.3",
1112
"sortablejs": "^1.10.2",
1213
"stimulus": "^1.1.1",
1314
"stimulus_reflex": "^3.2.3",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'test_helper'
2+
3+
class StatsControllerTest < ActionDispatch::IntegrationTest
4+
test "should get index" do
5+
get stats_index_url
6+
assert_response :success
7+
end
8+
9+
end

yarn.lock

+29-1
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,29 @@ chalk@^1.1.1:
17951795
strip-ansi "^3.0.0"
17961796
supports-color "^2.0.0"
17971797

1798+
chart.js@^2.9.3:
1799+
version "2.9.3"
1800+
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-2.9.3.tgz#ae3884114dafd381bc600f5b35a189138aac1ef7"
1801+
integrity sha512-+2jlOobSk52c1VU6fzkh3UwqHMdSlgH1xFv9FKMqHiNCpXsGPQa/+81AFa+i3jZ253Mq9aAycPwDjnn1XbRNNw==
1802+
dependencies:
1803+
chartjs-color "^2.1.0"
1804+
moment "^2.10.2"
1805+
1806+
chartjs-color-string@^0.6.0:
1807+
version "0.6.0"
1808+
resolved "https://registry.yarnpkg.com/chartjs-color-string/-/chartjs-color-string-0.6.0.tgz#1df096621c0e70720a64f4135ea171d051402f71"
1809+
integrity sha512-TIB5OKn1hPJvO7JcteW4WY/63v6KwEdt6udfnDE9iCAZgy+V4SrbSxoIbTw/xkUIapjEI4ExGtD0+6D3KyFd7A==
1810+
dependencies:
1811+
color-name "^1.0.0"
1812+
1813+
chartjs-color@^2.1.0:
1814+
version "2.4.1"
1815+
resolved "https://registry.yarnpkg.com/chartjs-color/-/chartjs-color-2.4.1.tgz#6118bba202fe1ea79dd7f7c0f9da93467296c3b0"
1816+
integrity sha512-haqOg1+Yebys/Ts/9bLo/BqUcONQOdr/hoEr2LLTRl6C5LXctUdHxsCYfvQVg5JIxITrfCNUDr4ntqmQk9+/0w==
1817+
dependencies:
1818+
chartjs-color-string "^0.6.0"
1819+
color-convert "^1.9.3"
1820+
17981821
chokidar@^2.1.8:
17991822
version "2.1.8"
18001823
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.8.tgz#804b3a7b6a99358c3c5c61e71d8728f041cff917"
@@ -1904,7 +1927,7 @@ collection-visit@^1.0.0:
19041927
map-visit "^1.0.0"
19051928
object-visit "^1.0.0"
19061929

1907-
color-convert@^1.9.0, color-convert@^1.9.1:
1930+
color-convert@^1.9.0, color-convert@^1.9.1, color-convert@^1.9.3:
19081931
version "1.9.3"
19091932
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
19101933
integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==
@@ -4536,6 +4559,11 @@ mixin-deep@^1.2.0:
45364559
dependencies:
45374560
minimist "^1.2.5"
45384561

4562+
moment@^2.10.2:
4563+
version "2.27.0"
4564+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
4565+
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
4566+
45394567
morphdom@^2.6.1:
45404568
version "2.6.1"
45414569
resolved "https://registry.yarnpkg.com/morphdom/-/morphdom-2.6.1.tgz#e868e24f989fa3183004b159aed643e628b4306e"

0 commit comments

Comments
 (0)