Skip to content

Commit

Permalink
Add already_seen flag to classification_events (#80)
Browse files Browse the repository at this point in the history
- Migration to add already_seen column to classification_events
- Update fixture to a more recent looking classification payload coming from kinesis stream
(Prior fixture is from 2016, what we send to the kinesis stream from panoptes has updated plenty since then)
- Fetch already_seen from metadata on stream_events
  • Loading branch information
yuenmichelle1 authored Dec 2, 2024
1 parent 94ad8e4 commit 9aa47e6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 3,293 deletions.
4 changes: 4 additions & 0 deletions app/models/stream_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ def self.session_time(event_data)
diff = SESSION_CAP if diff > SESSION_LIMIT
diff
end

def self.already_seen(event_data)
event_data.fetch('metadata')&.fetch('subject_selection_state', nil)&.fetch('already_seen', nil)
end
end
3 changes: 2 additions & 1 deletion app/models/stream_events/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def process
workflow_id: @links.fetch('workflow'),
user_id: @links&.fetch('user', nil),
user_group_ids: @data.fetch('metadata')&.fetch('user_group_ids', []),
session_time: StreamEvents.session_time(@data)
session_time: StreamEvents.session_time(@data),
already_seen: StreamEvents.already_seen(@data)
}
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAlreadySeenToClassificationEvents < ActiveRecord::Migration[7.0]
def change
add_column :classification_events, :already_seen, :boolean
end
end
3 changes: 2 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.0].define(version: 2024_09_26_233924) do
ActiveRecord::Schema[7.0].define(version: 2024_11_27_220919) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "timescaledb"
Expand All @@ -28,6 +28,7 @@
t.float "session_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "already_seen"
t.index ["event_time"], name: "classification_events_event_time_idx", order: :desc
t.index ["user_id"], name: "index_classification_events_on_user_id"
end
Expand Down
3,442 changes: 151 additions & 3,291 deletions spec/fixtures/example_kinesis_classification_payload.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions spec/models/stream_events/classification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
expect(prepared_payload[:user_id]).to eq(links.fetch('user'))
expect(prepared_payload[:user_group_ids]).to eq(data.fetch('metadata').fetch('user_group_ids'))
expect(prepared_payload[:session_time]).to eq(StreamEvents.session_time(data))
expect(prepared_payload[:already_seen]).to eq(StreamEvents.already_seen(data))
end

it 'sets the user_id to nil if no user in stream payload' do
Expand Down
17 changes: 17 additions & 0 deletions spec/models/stream_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@
expect(StreamEvents.session_time(event_data)).to be_zero
end
end

context 'already_seen' do
it 'returns already_seen from payload if provided' do
expected_already_seen = event_metadata['subject_selection_state']['already_seen']
expect(StreamEvents.already_seen(event_data)).to eq(expected_already_seen)
end

it 'returns nil if subject_selection_state is not provided' do
event_metadata.delete('subject_selection_state')
expect(StreamEvents.already_seen(event_data)).to eq(nil)
end

it 'returns nil if subject_selection_state does not have already_seen' do
event_metadata['subject_selection_state'] = {}
expect(StreamEvents.already_seen(event_data)).to eq(nil)
end
end
end

describe 'unknown event' do
Expand Down

0 comments on commit 9aa47e6

Please sign in to comment.