function _process_event_record
Processes a single event record from a database by normalizing timestamps, adding user information, and parsing JSON details fields.
/tf/active/vicechatdev/CDocs/utils/audit_trail.py
669 - 714
moderate
Purpose
This function transforms raw database event records into a standardized format suitable for data analysis and pandas DataFrame operations. It handles timestamp normalization to prevent NaN values, adds username information from the record, and attempts to parse stringified JSON details into dictionary objects. The function is designed to be robust against various timestamp formats and malformed data.
Source Code
def _process_event_record(record: Dict) -> Dict[str, Any]:
"""
Process a single event record from the database.
Args:
record: Database record with event data
Returns:
Processed event dictionary
"""
event = record['e']
event['userName'] = record.get('userName')
# Ensure timestamp is properly formatted to prevent NaN in pandas
if 'timestamp' in event:
try:
# Convert to string in ISO format if it's a datetime object
if isinstance(event['timestamp'], datetime):
event['timestamp'] = event['timestamp'].isoformat()
# Ensure it's a string that pandas can parse
elif event['timestamp'] is None:
event['timestamp'] = ''
# Make sure we have a non-empty string
elif not event['timestamp']:
event['timestamp'] = ''
# If it's already a string but not ISO format, try to convert it
elif isinstance(event['timestamp'], str) and not event['timestamp'].endswith('Z') and 'T' not in event['timestamp']:
try:
dt = datetime.fromisoformat(event['timestamp'])
event['timestamp'] = dt.isoformat()
except:
pass # Keep as is if we can't parse it
except Exception as e:
logger.error(f"Error formatting timestamp: {e}")
# Provide default value to prevent NaN
event['timestamp'] = ''
# Convert details from string to dictionary if possible
if 'details' in event and isinstance(event['details'], str):
try:
import json
event['details'] = json.loads(event['details'])
except:
pass
return event
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
record |
Dict | - | positional_or_keyword |
Parameter Details
record: A dictionary containing database event data. Expected to have an 'e' key with the event object, and optionally a 'userName' key. The event object may contain 'timestamp' (datetime, string, or None) and 'details' (string or dict) fields.
Return Value
Type: Dict[str, Any]
Returns a dictionary representing the processed event with normalized fields. The dictionary includes the original event data with modifications: 'userName' added from the record, 'timestamp' converted to ISO format string (or empty string if invalid/missing), and 'details' parsed from JSON string to dictionary if applicable. Type: Dict[str, Any]
Dependencies
loggingdatetimejson
Required Imports
from typing import Dict, Any
from datetime import datetime
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
import json
Condition: only when 'details' field exists in event and is a string that needs JSON parsing
OptionalUsage Example
from datetime import datetime
from typing import Dict, Any
import logging
logger = logging.getLogger(__name__)
def _process_event_record(record: Dict) -> Dict[str, Any]:
# ... function code ...
pass
# Example usage
db_record = {
'e': {
'eventId': '123',
'eventType': 'login',
'timestamp': datetime(2024, 1, 15, 10, 30, 0),
'details': '{"ip": "192.168.1.1", "browser": "Chrome"}'
},
'userName': 'john.doe'
}
processed_event = _process_event_record(db_record)
print(processed_event)
# Output: {'eventId': '123', 'eventType': 'login', 'timestamp': '2024-01-15T10:30:00', 'details': {'ip': '192.168.1.1', 'browser': 'Chrome'}, 'userName': 'john.doe'}
Best Practices
- Ensure a logger instance is configured before calling this function to capture timestamp formatting errors
- The function is defensive and handles various timestamp formats (datetime objects, ISO strings, non-ISO strings, None, empty strings)
- Returns empty string for invalid timestamps to prevent NaN values in pandas DataFrames
- JSON parsing of 'details' field fails silently, keeping the original string value if parsing fails
- The function modifies the event dictionary in-place before returning it
- Suitable for use in batch processing pipelines with map/apply operations
- Consider validating the structure of the input record dictionary before calling this function in production code
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function log_event 47.2% similar
-
function log_review_event 44.6% similar
-
function prepare_audit_data_for_document_processor 42.9% similar
-
function migrate_audit_trail 42.4% similar
-
function migrate_audit_events 42.1% similar