Class: Dbwatcher::Services::Analyzers::SessionDataProcessor

Inherits:
BaseService
  • Object
show all
Defined in:
lib/dbwatcher/services/analyzers/session_data_processor.rb

Overview

Handles session data processing and change iteration

This service extracts and processes individual changes from session data, providing a clean interface for iterating over table changes.

Examples:

processor = SessionDataProcessor.new(session)
processor.process_changes do |table_name, change, tables|
  # Process each change
end

Instance Method Summary collapse

Methods inherited from BaseService

call

Methods included from Logging

#debug_enabled?, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(session) ⇒ SessionDataProcessor

Initialize with session

Parameters:

  • session (Session)

    session to process



20
21
22
23
# File 'lib/dbwatcher/services/analyzers/session_data_processor.rb', line 20

def initialize(session)
  @session = session
  super()
end

Instance Method Details

#callHash

Process all changes in the session

Returns:

  • (Hash)

    tables hash with processed data



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dbwatcher/services/analyzers/session_data_processor.rb', line 28

def call
  log_service_start "Processing session changes", session_context
  start_time = Time.current

  tables = {}

  process_changes do |table_name, change, tables_ref|
    yield(table_name, change, tables_ref) if block_given?
  end

  log_service_completion(start_time, { tables_found: tables.keys.length })
  tables
end

#extract_session_tablesArray<String>

Extract session tables that were modified

Returns:

  • (Array<String>)

    unique table names



74
75
76
77
78
79
80
# File 'lib/dbwatcher/services/analyzers/session_data_processor.rb', line 74

def extract_session_tables
  return [] unless session&.changes

  session.changes.map do |change|
    extract_table_name(change)
  end.compact.uniq
end

#extract_table_name(change) ⇒ String?

Extract table name from change data

Parameters:

  • change (Hash)

    change data

Returns:

  • (String, nil)

    table name or nil



65
66
67
68
69
# File 'lib/dbwatcher/services/analyzers/session_data_processor.rb', line 65

def extract_table_name(change)
  return nil unless change.is_a?(Hash)

  change[:table_name]
end

#process_changes {|table_name, change, tables| ... } ⇒ Hash

Process changes with block

Yields:

  • (table_name, change, tables)

    for each change

Returns:

  • (Hash)

    tables hash



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dbwatcher/services/analyzers/session_data_processor.rb', line 46

def process_changes
  return {} unless session&.changes.respond_to?(:each)

  tables = {}

  session.changes.each do |change|
    table_name = extract_table_name(change)
    next unless table_name

    yield(table_name, change, tables) if block_given?
  end

  tables
end