diff --git a/lib/data_pull.rb b/lib/data_pull.rb index 2f00f3e4141..3f70c278484 100644 --- a/lib/data_pull.rb +++ b/lib/data_pull.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/lib/data_pull_spec.rb b/spec/lib/data_pull_spec.rb index 202712c50de..ec4efaf551e 100644 --- a/spec/lib/data_pull_spec.rb +++ b/spec/lib/data_pull_spec.rb @@ -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