<?php
/**
* Indexes content of log entries for short-lived tasks.
*
* @param array $log_entries Array of log entries, each entry is an associative array.
* Example: ['timestamp' => '2023-10-27 10:00:00', 'message' => 'Task started', 'data' => ['key1' => 'value1']]
* @return array Associative array where keys are indexed terms and values are arrays of log entries containing that term.
*/
function indexLogEntries(array $log_entries): array
{
$index = [];
foreach ($log_entries as $entry) {
// Iterate through all fields in each log entry
foreach ($entry as $key => $value) {
// Normalize the value to lowercase for consistent indexing
$term = strtolower(trim($value));
// If the term is not empty
if (!empty($term)) {
// If the term is not already a key in the index, create a new array for it
if (!isset($index[$term])) {
$index[$term] = [];
}
// Add the current log entry to the array associated with the term
$index[$term][] = $entry;
}
}
}
return $index;
}
// Example usage:
/*
$log_entries = [
['timestamp' => '2023-10-27 10:00:00', 'message' => 'Task started', 'data' => ['key1' => 'value1']],
['timestamp' => '2023-10-27 10:00:05', 'message' => 'Processing data', 'data' => ['key2' => 'value2']],
['timestamp' => '2023-10-27 10:00:10', 'message' => 'Task completed', 'data' => ['key1' => 'value1']],
['timestamp' => '2023-10-27 10:00:15', 'message' => 'Error: Invalid data', 'data' => ['key3' => 'value3']]
];
$index = indexLogEntries($log_entries);
// Print the index (for demonstration)
print_r($index);
*/
?>
Add your comment