Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rage service changes #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ruby-api/app/services/RageClickService/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ def average_time_between_clicks(interactions)
intervals.sum.to_f / intervals.length
end
end
end
end
2 changes: 1 addition & 1 deletion ruby-api/app/services/RageClickService/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def average_confidence_score
def no_of_occurances
end
end
end
end
17 changes: 16 additions & 1 deletion ruby-api/app/services/events_service/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ def call
user_event_params = build_user_event_params(event_param, action)
user_event = UserEvent.create!(user_event_params)

user_events = []
user_events << user_event
user_event_hashes = user_events.map(&:attributes)
rage_click = RageClickService::Detector.new().analyze_clicks(user_event_hashes)

if rage_click.rage_clicks.present?
aggregate_event_param = aggregate_event_params(rage_click.rage_clicks)
aggregate_event_param.each do |param|
AggregateEvent.create!(param)
end
end

res = action&.user_events&.where(uid: event_param[:uid], captured_at: 5.seconds.ago..user_event.captured_at.to_datetime.utc)
if res && res.count == 5
rage_click_event = create_aggregate_event(user_event, action)
rage_event_ids << rage_click_event.id
end
end
rage_event_ids
end
rescue ActiveRecord::RecordInvalid => e
# Handle transaction failure if necessary (e.g., log or raise an error)
Expand All @@ -28,6 +39,10 @@ def call

private

def aggregate_event_params(rage_clicks)
rage_clicks.map { |hash| hash.slice(:action_id, :uid) }
end

def find_action(param)
Action.find_or_create_by(url: param[:url], element: param[:element_id], action: param[:action])
end
Expand Down
2 changes: 1 addition & 1 deletion ruby-api/db/migrate/20241221100253_drop_unvanted_tables.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class DropUnvantedTables < ActiveRecord::Migration[7.2]
def change
drop_table :events
drop_table :event_aggregations
execute "DROP TABLE event_aggregations CASCADE;"
end
end
8 changes: 8 additions & 0 deletions ruby-api/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
t.index ["user_event_id"], name: "index_aggregate_events_on_user_event_id"
end

create_table "images", force: :cascade do |t|
t.string "attachement"
t.bigint "event_aggregation_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["event_aggregation_id"], name: "index_images_on_event_aggregation_id"
end

create_table "user_events", force: :cascade do |t|
t.datetime "captured_at"
t.text "user_agent"
Expand Down