1. <?php
  2. /**
  3. * Aggregates values from log files with limited memory usage.
  4. *
  5. * @param array $logFiles An array of log file paths.
  6. * @param string $aggregationKey The key to aggregate values by.
  7. * @param callable $aggregationFunction A function to apply for aggregation (e.g., array_sum, array_count_values).
  8. * @return array An associative array containing the aggregated values.
  9. */
  10. function aggregateLogFiles(array $logFiles, string $aggregationKey, callable $aggregationFunction): array
  11. {
  12. $aggregatedData = [];
  13. foreach ($logFiles as $logFile) {
  14. if (file_exists($logFile)) {
  15. $logData = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Read log file
  16. if ($logData) {
  17. foreach ($logData as $line) {
  18. if (strpos($line, $aggregationKey) !== false) {
  19. // Extract the value based on the aggregation key
  20. preg_match('/' . preg_quote($aggregationKey, '/') . '=\s*([^\s]+)/', $line, $matches);
  21. if (isset($matches[1])) {
  22. $value = trim($matches[1]);
  23. if (isset($aggregatedData[$value])) {
  24. $aggregatedData[$value][] = $value; // Add to existing array
  25. } else {
  26. $aggregatedData[$value] = [$value]; // Create new array
  27. }
  28. }
  29. }
  30. }
  31. }
  32. } else {
  33. error_log("Log file not found: " . $logFile);
  34. }
  35. }
  36. // Apply the aggregation function to the aggregated data
  37. foreach ($aggregatedData as $key => $values) {
  38. $aggregatedData[$key] = $aggregationFunction($values);
  39. }
  40. return $aggregatedData;
  41. }
  42. // Example Usage:
  43. /*
  44. $logFiles = ['log1.txt', 'log2.txt'];
  45. $aggregationKey = 'user_id';
  46. $aggregationFunction = function (array $values) {
  47. return array_count_values($values); // Count occurrences of each user_id
  48. };
  49. $aggregatedResults = aggregateLogFiles($logFiles, $aggregationKey, $aggregationFunction);
  50. print_r($aggregatedResults);
  51. */
  52. ?>

Add your comment