Class: Lich::Common::SessionDatabaseAdapter
- Inherits:
-
Object
- Object
- Lich::Common::SessionDatabaseAdapter
- Defined in:
- documented/common/settings/session_database_adapter.rb
Overview
Handles database interactions for session management.
This class provides methods to upsert, find, delete, and query sessions.
Constant Summary collapse
- DEFAULT_TABLE_NAME =
'session_summary_state'
Instance Method Summary collapse
-
#active_sessions ⇒ Array<Hash>
Retrieves all active sessions from the database.
-
#delete_session(pid:) ⇒ void
Deletes a session from the database by its process ID.
-
#duplicate_active_session_names ⇒ Array<Hash>
Retrieves session names that have duplicates in the active sessions.
-
#find_session(pid:) ⇒ Hash?
Finds a session by its process ID.
-
#initialize(db: nil, data_dir: DATA_DIR, table_name: DEFAULT_TABLE_NAME) ⇒ SessionDatabaseAdapter
constructor
Initializes a new SessionDatabaseAdapter instance.
-
#tracked_live_candidates ⇒ Array<Hash>
Retrieves sessions that are currently tracked and not exited.
-
#upsert_session(payload) ⇒ void
Inserts or updates a session in the database.
Constructor Details
#initialize(db: nil, data_dir: DATA_DIR, table_name: DEFAULT_TABLE_NAME) ⇒ SessionDatabaseAdapter
Initializes a new SessionDatabaseAdapter instance.
20 21 22 23 |
# File 'documented/common/settings/session_database_adapter.rb', line 20 def initialize(db: nil, data_dir: DATA_DIR, table_name: DEFAULT_TABLE_NAME) @db = db || SQLite3::Database.new(File.join(data_dir, 'lich.db3')) @table_name = table_name end |
Instance Method Details
#active_sessions ⇒ Array<Hash>
Retrieves all active sessions from the database.
58 59 60 61 62 |
# File 'documented/common/settings/session_database_adapter.rb', line 58 def active_sessions with_retry do rows_as_hashes("SELECT * FROM #{@table_name} ORDER BY pid ASC;") end end |
#delete_session(pid:) ⇒ void
This method returns an undefined value.
Deletes a session from the database by its process ID.
67 68 69 70 71 |
# File 'documented/common/settings/session_database_adapter.rb', line 67 def delete_session(pid:) with_retry do @db.execute("DELETE FROM #{@table_name} WHERE pid = ?;", [pid]) end end |
#duplicate_active_session_names ⇒ Array<Hash>
Retrieves session names that have duplicates in the active sessions.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'documented/common/settings/session_database_adapter.rb', line 84 def duplicate_active_session_names with_retry do rows_as_hashes(<<~SQL) SELECT session_name, COUNT(*) AS duplicate_count FROM #{@table_name} WHERE session_name IS NOT NULL AND session_name != '' AND COALESCE(state, '') != 'exited' GROUP BY session_name HAVING COUNT(*) > 1 ORDER BY session_name ASC; SQL end end |
#find_session(pid:) ⇒ Hash?
Finds a session by its process ID.
76 77 78 79 80 |
# File 'documented/common/settings/session_database_adapter.rb', line 76 def find_session(pid:) with_retry do rows_as_hashes("SELECT * FROM #{@table_name} WHERE pid = ? LIMIT 1;", [pid.to_i]).first end end |
#tracked_live_candidates ⇒ Array<Hash>
Retrieves sessions that are currently tracked and not exited.
101 102 103 104 105 106 107 108 109 |
# File 'documented/common/settings/session_database_adapter.rb', line 101 def tracked_live_candidates with_retry do rows_as_hashes(<<~SQL) SELECT * FROM #{@table_name} WHERE COALESCE(state, '') != 'exited' ORDER BY pid ASC; SQL end end |
#upsert_session(payload) ⇒ void
This method returns an undefined value.
Inserts or updates a session in the database.
This method will insert a new session or update an existing one based on the provided payload.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'documented/common/settings/session_database_adapter.rb', line 30 def upsert_session(payload) with_retry do @db.execute(<<~SQL, bind_params(payload)) INSERT INTO #{@table_name} ( pid, session_name, role, state, frontend, game_code, hidden, started_at, last_heartbeat_at, os_seen_at, os_seen, os_name, last_utilization_at, metadata_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(pid) DO UPDATE SET session_name = COALESCE(excluded.session_name, #{@table_name}.session_name), role = COALESCE(excluded.role, #{@table_name}.role), state = COALESCE(excluded.state, #{@table_name}.state), frontend = COALESCE(excluded.frontend, #{@table_name}.frontend), game_code = COALESCE(excluded.game_code, #{@table_name}.game_code), hidden = COALESCE(excluded.hidden, #{@table_name}.hidden), started_at = COALESCE(excluded.started_at, #{@table_name}.started_at), last_heartbeat_at = COALESCE(excluded.last_heartbeat_at, #{@table_name}.last_heartbeat_at), os_seen_at = COALESCE(excluded.os_seen_at, #{@table_name}.os_seen_at), os_seen = COALESCE(excluded.os_seen, #{@table_name}.os_seen), os_name = COALESCE(excluded.os_name, #{@table_name}.os_name), last_utilization_at = COALESCE(excluded.last_utilization_at, #{@table_name}.last_utilization_at), metadata_json = COALESCE(excluded.metadata_json, #{@table_name}.metadata_json); SQL end end |