-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ApplicationJob < ActiveJob::Base | ||
# Automatically retry jobs that encountered a deadlock | ||
# retry_on ActiveRecord::Deadlocked | ||
|
||
# Most jobs are safe to ignore if the underlying records are no longer available | ||
# discard_on ActiveJob::DeserializationError | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class MaterializeVideoAnalyticsJob < ApplicationJob | ||
queue_as :default | ||
|
||
def video_path(video) | ||
Rails.application.routes.url_helpers.playlist_video_path(id: video.slug, playlist_id: video.playlist.slug) | ||
end | ||
|
||
def perform(*args) | ||
total_data = IngestedAnalytic.recordset.group_by_page | ||
recent_data = IngestedAnalytic.recordset.where('day >= ?', 30.days.ago).group_by_page | ||
Video.includes(:playlist).find_each do |video| | ||
url = video_path(video) | ||
total = total_data[url] | ||
recent = recent_data[url] | ||
video.update(views_total: total, views_recent: recent) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# == Schema Information | ||
# | ||
# Table name: ingested_analytics | ||
# | ||
# id :bigint not null, primary key | ||
# day :date not null | ||
# document :jsonb not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_ingested_analytics_on_day (day) | ||
# | ||
class IngestedAnalytic < ApplicationRecord | ||
validates :day, presence: true, uniqueness: true | ||
validates :document, presence: true | ||
|
||
# SELECT ... | ||
# FROM ingested_analytics | ||
# JOIN jsonb_to_recordset(ingested_analytics.document) | ||
# AS row(page text, pageviews int) | ||
# ON true | ||
scope :recordset, -> { joins('JOIN jsonb_to_recordset(document) as row(page text, pageviews int) ON TRUE') } | ||
scope :by_page, ->(page) { recordset.where(row: { page: page }) } | ||
scope :group_by_page, -> { | ||
result = select('page', 'sum(pageviews) AS sum').group('page') | ||
result.map { |r| [r.page, r.sum] }.to_h | ||
} | ||
|
||
scope :total, -> { sum('pageviews') } | ||
scope :data, -> { pluck('day', 'page', 'pageviews') } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Plausible | ||
class Downloader | ||
class << self | ||
def entity_for_day(day) | ||
new(day).entity | ||
end | ||
end | ||
|
||
def initialize(day) | ||
@day = day | ||
end | ||
|
||
def entity | ||
@entity ||= IngestedAnalytic.new(day: @day, document: analytics) | ||
end | ||
|
||
def analytics | ||
@analytics ||= client.breakdown(@day) | ||
end | ||
|
||
def client | ||
Plausible::Integration.client | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateIngestedAnalytics < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :ingested_analytics do |t| | ||
t.timestamps | ||
t.date :day, null: false, index: true, unique: true | ||
t.jsonb :document, null: false # , index: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddViewsToVideos < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :videos, :views_total, :integer, default: 0 | ||
add_column :videos, :views_recent, :integer, default: 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# == Schema Information | ||
# | ||
# Table name: ingested_analytics | ||
# | ||
# id :bigint not null, primary key | ||
# day :date not null | ||
# document :jsonb not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_ingested_analytics_on_day (day) | ||
# | ||
FactoryBot.define do | ||
factory :ingested_analytic do | ||
day { '2024-06-23' } | ||
document { [{ "page" => "/series/cocina/como-cocinar-estofado", "pageviews" => 5 }] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe MaterializeVideoAnalyticsJob, type: :job do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# == Schema Information | ||
# | ||
# Table name: ingested_analytics | ||
# | ||
# id :bigint not null, primary key | ||
# day :date not null | ||
# document :jsonb not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_ingested_analytics_on_day (day) | ||
# | ||
require 'rails_helper' | ||
|
||
RSpec.describe IngestedAnalytic, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters