loadRecentInteractionData

open suspend override fun loadRecentInteractionData(userId: String, limit: Int, type: String?): List<InteractionData>

Retrieves recent user interaction data with optional filtering and limiting.

This method queries the interaction_data table to fetch the most recent interactions for a specified user, supporting both filtered and unfiltered queries with configurable result limits.

Query Variants:

1. Type-Filtered Query:

SELECT user_id, type, details, timestamp
FROM interaction_data
WHERE user_id = ? AND type = ?
ORDER BY timestamp DESC
LIMIT ?

2. Unfiltered Query:

SELECT user_id, type, details, timestamp
FROM interaction_data
WHERE user_id = ?
ORDER BY timestamp DESC
LIMIT ?

Performance Optimization:

  • Index Utilization: Leverages idx_interaction_user_timestamp for fast retrieval

  • Descending Order: Most recent interactions returned first

  • Result Limiting: Prevents excessive memory usage with large datasets

  • Conditional Filtering: Optional type filtering for specific interaction categories

Data Reconstruction:

  • Object Mapping: Database rows mapped to InteractionData instances

  • Simplified Parsing: Details stored as single string in "stored" key

  • Temporal Ordering: Results reversed to chronological order before return

  • Type Preservation: Interaction type classifications maintained

Error Handling & Recovery:

  • SQLException: Database access errors with comprehensive logging

  • Graceful Degradation: Returns empty list on any error

  • Resource Management: Automatic cleanup of ResultSet and PreparedStatement

  • Null Safety: Handles missing or corrupted data gracefully

Use Cases & Applications:

  • Recent Activity: Show user's latest interactions in UI

  • Pattern Analysis: Analyze recent behavior for prediction improvement

  • Context Awareness: Understand current user workflow context

  • Debugging: Investigate user interaction sequences for troubleshooting

  • Recommendation: Base suggestions on recent interaction patterns

Filtering Examples:

// Get last 10 interactions of any type
val recent = loadRecentInteractionData("user123", 10, null)

// Get last 5 command predictions specifically
val predictions = loadRecentInteractionData("user123", 5, "command_prediction")

// Get last 20 user acceptance events
val acceptances = loadRecentInteractionData("user123", 20, "user_acceptance")

Threading Context:

  • All database operations execute on Dispatchers.IO

  • Non-blocking operation suitable for reactive UI updates

  • Safe to call from any coroutine context

Return

List of InteractionData instances ordered chronologically (oldest first), containing up to limit recent interactions. Returns empty list if no interactions exist or if an error occurs during retrieval.

Parameters

userId

Unique identifier for the user whose interactions should be retrieved. Must match user IDs used in saveInteractionData operations.

limit

Maximum number of interaction records to return. Should be reasonable to prevent excessive memory usage (e.g., 1-1000 range).

type

Optional interaction type filter. If provided, only interactions matching this type will be returned. If null, all interaction types are included in the results.

See also

The interaction event data structure being retrieved

Corresponding interaction persistence operation

Dispatchers.IO

Coroutine context for database operations