Skip to content

Commit

Permalink
denormalize event type for request and event logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed May 1, 2024
1 parent 2aa0b0f commit 25135c0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/event_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class EventLog < ApplicationRecord
include Keygen::EE::ProtectedClass[entitlements: %i[event_logs]]
include Denormalizable
include Environmental
include Accountable
include DateRangeable
Expand All @@ -22,6 +23,9 @@ class EventLog < ApplicationRecord
has_environment
has_account

denormalizes :event, from: :event_type, prefix: :event_type
denormalizes :event_type_event, to: :request_log

# NOTE(ezekg) Would love to add a default instead of this, but alas,
# the table is too big and it would break everything.
before_create -> { self.created_date ||= (created_at || Date.current) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEventTypeEventToEventLogs < ActiveRecord::Migration[7.1]
def change
add_column :event_logs, :event_type_event, :string, null: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEventTypeEventToRequestLogs < ActiveRecord::Migration[7.1]
def change
add_column :request_logs, :event_type_event, :string, null: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_04_24_041244) do
ActiveRecord::Schema[7.1].define(version: 2024_05_01_124926) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -110,6 +110,7 @@
t.datetime "updated_at", null: false
t.uuid "environment_id"
t.date "created_date"
t.string "event_type_event"
t.index ["account_id", "created_at"], name: "index_event_logs_on_account_id_and_created_at", order: { created_at: :desc }
t.index ["account_id", "created_date"], name: "index_event_logs_on_account_id_and_created_date", order: { created_date: :desc }
t.index ["environment_id"], name: "index_event_logs_on_environment_id"
Expand Down Expand Up @@ -695,6 +696,7 @@
t.jsonb "response_headers"
t.float "run_time"
t.float "queue_time"
t.string "event_type_event"
t.index ["account_id", "created_at"], name: "index_request_logs_on_account_id_and_created_at"
t.index ["account_id", "created_date"], name: "index_request_logs_on_account_id_and_created_date", order: { created_date: :desc }
t.index ["environment_id"], name: "index_request_logs_on_environment_id"
Expand Down
62 changes: 62 additions & 0 deletions spec/models/event_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,68 @@
require 'spec_helper'

describe EventLog, type: :model do
let(:account) { create(:account) }

it_behaves_like :environmental
it_behaves_like :accountable

describe '#event_type=' do
let(:event_type) { EventType.all.to_a.sample }

context 'on build' do
it 'should denormalize event from event type' do
event_log = build(:event_log, event_type:, account:)

expect(event_log.event_type_event).to eq event_type.event
expect(event_log.event_type_id).to eq be_nil
end

it 'should denormalize event type to request log' do
request_log = build(:request_log, account:)
event_log = build(:event_log, request_log:, event_type:, account:)

expect(request_log.event_type_event).to eq event_type.event
expect(request_log.event_type_id).to be_nil
end
end

context 'on create' do
it 'should denormalize event from event type' do
event_log = create(:event_log, event_type:, account:)

expect(event_log.event_type_event).to eq event_type.event
expect(event_log.event_type_id).to eq event_type.id
end

it 'should denormalize event type to request log' do
request_log = create(:request_log, account:)
event_log = create(:event_log, request_log:, event_type:, account:)

expect(request_log.event_type_event).to eq event_type.event
expect(request_log.event_type_id).to eq event_type.id
end
end

context 'on update' do
it 'should denormalize event from event type' do
event_log = create(:event_log, account:)

event_log.update!(event_type:)

expect(event_log.event_type_event).to eq event_type.event
expect(event_log.event_type_id).to eq event_type.id
end

it 'should denormalize event type to request log' do
request_log = create(:request_log, account:)
event_log = create(:event_log, request_log:, account:)

event_log.update!(event_type:)
request_log.reload

expect(request_log.event_type_event).to eq event_type.event
expect(request_log.event_type_id).to eq event_type.id
end
end
end
end

0 comments on commit 25135c0

Please sign in to comment.