Skip to content
Merged
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
61 changes: 55 additions & 6 deletions lib/data_pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ def banner

Example usage:

* #{basename} uuid-lookup email1@example.com email2@example.com

* #{basename} uuid-convert partner-uuid1 partner-uuid2

* #{basename} email-lookup uuid1 uuid2

* #{basename} events-summary uuid1 uuid2

* #{basename} ig-request uuid1 uuid2 --requesting-issuer ABC:DEF:GHI

* #{basename} profile-summary uuid1 uuid2

* #{basename} uuid-convert partner-uuid1 partner-uuid2

* #{basename} uuid-export uuid1 uuid2 --requesting-issuer ABC:DEF:GHI

* #{basename} uuid-lookup email1@example.com email2@example.com
Options:
EOS
end
Expand All @@ -52,12 +53,13 @@ def banner
# @return [Class,nil]
def subtask(name)
{
'uuid-lookup' => UuidLookup,
'uuid-convert' => UuidConvert,
'email-lookup' => EmailLookup,
'events-summary' => EventsSummary,
'ig-request' => InspectorGeneralRequest,
'profile-summary' => ProfileSummary,
'uuid-convert' => UuidConvert,
'uuid-export' => UuidExport,
'uuid-lookup' => UuidLookup,
}[name]
end

Expand Down Expand Up @@ -259,4 +261,51 @@ def run(args:, config:)
)
end
end

class EventsSummary
def run(args:, config:)
uuids = args

sql = <<-SQL
SELECT
users.uuid AS uuid
, events.created_at::date AS date
, COUNT(events.id) AS events_count
FROM users
JOIN events ON users.id = events.user_id
WHERE users.uuid IN (:uuids)
GROUP BY
users.uuid
, events.created_at::date
ORDER BY
users.uuid ASC
, events.created_at::date DESC
SQL

results = ActiveRecord::Base.connection.execute(
ApplicationRecord.sanitize_sql_array([sql, { uuids: uuids }]),
)

table = []
table << %w[uuid date events_count]

results.each do |row|
table << [row['uuid'], row['date'], row['events_count']]
end

found_uuids = results.map { |r| r['uuid'] }.uniq

if config.include_missing?
(uuids - found_uuids).each do |missing_uuid|
table << [missing_uuid, '[UUID NOT FOUND]', nil]
end
end

ScriptBase::Result.new(
subtask: 'events-summary',
uuids: found_uuids,
table:,
)
end
end
end
45 changes: 45 additions & 0 deletions spec/lib/data_pull_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,49 @@
end
end
end

describe DataPull::EventsSummary do
subject(:subtask) { DataPull::EventsSummary.new }

let(:user) { create(:user) }

before do
create(
:event,
user: user,
event_type: :account_created,
ip: '1.2.3.4',
created_at: Date.new(2023, 1, 1).in_time_zone('UTC'),
)

create_list(
:event,
5,
user: user,
event_type: :account_created,
ip: '1.2.3.4',
created_at: Date.new(2023, 1, 2).in_time_zone('UTC'),
)
end

let(:args) { [user.uuid, 'uuid-does-not-exist'] }
let(:config) { ScriptBase::Config.new(include_missing: true) }
subject(:result) { subtask.run(args:, config:) }

describe '#run' do
it 'loads events for the users' do
expect(result.table).to match_array(
[
%w[uuid date events_count],
[user.uuid, '2023-01-02', 5],
[user.uuid, '2023-01-01', 1],
['uuid-does-not-exist', '[UUID NOT FOUND]', nil],
],
)

expect(result.subtask).to eq('events-summary')
expect(result.uuids).to match_array([user.uuid])
end
end
end
end