Class: Dbwatcher::Storage::Api::SessionAPI

Inherits:
BaseAPI
  • Object
show all
Includes:
Concerns::TableAnalyzer
Defined in:
lib/dbwatcher/storage/api/session_api.rb

Instance Method Summary collapse

Methods included from Concerns::TableAnalyzer

#build_tables_summary, #extract_table_name, #process_session_changes

Methods inherited from BaseAPI

#create, #initialize, #limit, #where

Methods included from Concerns::DataNormalizer

#extract_value, #normalize_change, #normalize_hash_keys, #normalize_operation, #normalize_record_id, #normalize_session_data, #normalize_table_name, #normalize_timestamp

Constructor Details

This class inherits a constructor from Dbwatcher::Storage::Api::BaseAPI

Instance Method Details

#allArray<Hash>

Get all sessions

Returns:

  • (Array<Hash>)

    array of session data



25
26
27
# File 'lib/dbwatcher/storage/api/session_api.rb', line 25

def all
  apply_filters(storage.all)
end

#by_name(pattern) ⇒ SessionAPI

Filter sessions by name pattern

Parameters:

  • pattern (String)

    name pattern to match

Returns:



59
60
61
62
# File 'lib/dbwatcher/storage/api/session_api.rb', line 59

def by_name(pattern)
  filters[:name_pattern] = pattern
  self
end

#by_status(status) ⇒ SessionAPI

Filter sessions by status

Parameters:

  • status (String, Symbol)

    session status (e.g., :active, :completed)

Returns:



50
51
52
53
# File 'lib/dbwatcher/storage/api/session_api.rb', line 50

def by_status(status)
  filters[:status] = status.to_s
  self
end

#diagram_data(session_id, diagram_type = "database_tables") ⇒ Hash

Generate diagram data for a session

Parameters:

  • session_id (String)

    session identifier

  • diagram_type (String) (defaults to: "database_tables")

    type of diagram to generate

Returns:

  • (Hash)

    diagram data



126
127
128
# File 'lib/dbwatcher/storage/api/session_api.rb', line 126

def diagram_data(session_id, diagram_type = "database_tables")
  Dbwatcher::Services::DiagramSystem.generate(session_id, diagram_type)
end

#find(id) ⇒ Session?

Find a specific session by ID

Parameters:

  • id (String)

    the session ID

Returns:

  • (Session, nil)

    the session object or nil if not found



18
19
20
# File 'lib/dbwatcher/storage/api/session_api.rb', line 18

def find(id)
  storage.load(id)
end

#most_active(limit: 10) ⇒ Array<Hash>

Get the most active sessions (by change count)

Parameters:

  • limit (Integer) (defaults to: 10)

    maximum number of sessions to return

Returns:

  • (Array<Hash>)

    sessions ordered by activity



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dbwatcher/storage/api/session_api.rb', line 82

def most_active(limit: 10)
  sessions_with_counts = all.map do |session_info|
    session = find(safe_extract(session_info, :id))
    change_count = session ? session.changes.length : 0
    session_info.merge(change_count: change_count)
  end

  sessions_with_counts
    .sort_by { |s| -s[:change_count] }
    .first(limit)
end

#recent(days: 7) ⇒ SessionAPI

Filter to recent sessions

Parameters:

  • days (Integer) (defaults to: 7)

    number of days back to include

Returns:



33
34
35
36
# File 'lib/dbwatcher/storage/api/session_api.rb', line 33

def recent(days: 7)
  cutoff = Time.now - (days * 24 * 60 * 60)
  where(started_after: cutoff)
end

#summary(session_id) ⇒ Hash

Get comprehensive session analysis including tables and relationships

Parameters:

  • session_id (String)

    session identifier

Returns:

  • (Hash)

    session analysis data



98
99
100
101
102
103
104
105
106
107
# File 'lib/dbwatcher/storage/api/session_api.rb', line 98

def summary(session_id)
  session = find(session_id)
  return { error: "Session not found" } unless session

  {
    tables_summary: tables_summary(session_id),
    total_changes: session.changes&.count || 0,
    session_metadata: (session)
  }
end

#tables_summary(session_id) ⇒ Hash

Get tables summary for a session

Parameters:

  • session_id (String)

    session identifier

Returns:

  • (Hash)

    tables summary



113
114
115
116
117
118
119
# File 'lib/dbwatcher/storage/api/session_api.rb', line 113

def tables_summary(session_id)
  session = find(session_id)
  return { error: "Session not found" } unless session

  analyzer = Dbwatcher::Services::Analyzers::TableSummaryBuilder.new(session)
  analyzer.call
end

#with_changesSessionAPI

Filter sessions that have changes

Returns:



41
42
43
44
# File 'lib/dbwatcher/storage/api/session_api.rb', line 41

def with_changes
  filters[:has_changes] = true
  self
end

#with_table_analysisArray<Hash>

Get sessions with table analysis

Returns:

  • (Array<Hash>)

    sessions with analyzed table data



67
68
69
70
71
72
73
74
75
76
# File 'lib/dbwatcher/storage/api/session_api.rb', line 67

def with_table_analysis
  all.map do |session_info|
    session = find(safe_extract(session_info, :id))
    next session_info unless session

    session_info.merge(
      tables_summary: build_tables_summary(session)
    )
  end.compact
end