1. /**
  2. * Filters log stream data for staging environments, supporting older versions.
  3. *
  4. * @param {Array<Object>} logs - An array of log objects. Each object is expected to have a 'environment' and 'version' property.
  5. * @param {string} targetEnvironment - The target environment to filter for (e.g., 'staging').
  6. * @param {string} minVersion - The minimum supported version (e.g., '1.0'). Older versions will be excluded.
  7. * @returns {Array<Object>} - An array containing only the log objects from the specified environment and version.
  8. */
  9. function filterLogStreams(logs, targetEnvironment, minVersion) {
  10. if (!Array.isArray(logs)) {
  11. return []; // Return empty array if input is not an array
  12. }
  13. const filteredLogs = logs.filter(log => {
  14. // Check if the log object has the required properties
  15. if (!log.environment || !log.version) {
  16. return false; // Skip logs without environment or version
  17. }
  18. // Compare the environment and version
  19. return log.environment === targetEnvironment && log.version >= minVersion;
  20. });
  21. return filteredLogs;
  22. }
  23. //Example Usage (for testing)
  24. // const logs = [
  25. // { environment: 'staging', version: '1.1' },
  26. // { environment: 'production', version: '2.0' },
  27. // { environment: 'staging', version: '1.0' },
  28. // { environment: 'staging', version: '1.2' },
  29. // { environment: 'staging', version: '0.9' }
  30. // ];
  31. // const filtered = filterLogStreams(logs, 'staging', '1.0');
  32. // console.log(filtered);

Add your comment