1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. public class LogStreamValidator {
  6. /**
  7. * Validates log streams based on provided conditions with retry logic.
  8. *
  9. * @param logStreams A list of log stream data (simulated).
  10. * @param conditions A list of validation conditions. Each condition is a Map containing
  11. * the field to check and the expected value.
  12. * @param maxRetries The maximum number of retry attempts for each log stream.
  13. * @return A list of validation results. Each result indicates whether a log stream passed or failed
  14. * and the reason for failure (if any).
  15. */
  16. public static List<ValidationResult> validateLogStreams(List<String> logStreams, List<Map<String, String>> conditions, int maxRetries) {
  17. List<ValidationResult> results = new ArrayList<>();
  18. for (String logStream : logStreams) {
  19. int retryCount = 0;
  20. boolean isValid = false;
  21. String errorMessage = null;
  22. while (!isValid && retryCount < maxRetries) {
  23. try {
  24. if (validateLogStream(logStream, conditions)) {
  25. isValid = true;
  26. } else {
  27. errorMessage = "Validation failed on attempt " + (retryCount + 1);
  28. }
  29. } catch (Exception e) {
  30. errorMessage = "Exception during validation: " + e.getMessage();
  31. }
  32. retryCount++;
  33. }
  34. results.add(new ValidationResult(logStream, isValid, errorMessage));
  35. }
  36. return results;
  37. }
  38. private static boolean validateLogStream(String logStream, List<Map<String, String>> conditions) throws Exception {
  39. // Simulate log stream processing and condition validation
  40. for (Map<String, String> condition : conditions) {
  41. String field = condition.keySet().iterator().next(); // Get the field name
  42. String expectedValue = condition.get(field);
  43. String actualValue = extractFieldValue(logStream, field);
  44. if (actualValue == null) {
  45. throw new IllegalArgumentException("Field '" + field + "' not found in log stream.");
  46. }
  47. if (!actualValue.equals(expectedValue)) {
  48. throw new IllegalArgumentException("Field '" + field + "' has incorrect value. Expected: '" + expectedValue + "', Actual: '" + actualValue + "'.");
  49. }
  50. }
  51. return true;
  52. }
  53. private static String extractFieldValue(String logStream, String field) {
  54. // Simulate extracting the value of a field from the log stream.
  55. // This is a placeholder. In a real application, this would involve
  56. // parsing the log stream.
  57. if (logStream.contains(field)) {
  58. return logStream.substring(logStream.indexOf(field) + field.length());
  59. }
  60. return null;
  61. }
  62. public static class ValidationResult {
  63. private final String logStream;
  64. private final boolean isValid;
  65. private final String errorMessage;
  66. public ValidationResult(String logStream, boolean isValid, String errorMessage) {
  67. this.logStream = logStream;
  68. this.isValid = isValid;
  69. this.errorMessage = errorMessage;
  70. }
  71. public String getLogStream() {
  72. return logStream;
  73. }
  74. public boolean isIsValid() {
  75. return isValid;
  76. }
  77. public String getErrorMessage() {
  78. return errorMessage;
  79. }
  80. }
  81. public static void main(String[] args) {
  82. List<String> logStreams = new ArrayList<>();
  83. logStreams.add("timestamp=2024-10-27T10:00:00, status=OK");
  84. logStreams.add("timestamp=2024-10-27T10:00:01, status=ERROR");
  85. logStreams.add("timestamp=2024-10-27T10:00:02, status=OK");
  86. List<Map<String, String>> conditions = new ArrayList<>();
  87. Map<String, String> condition1 = new HashMap<>();
  88. condition1.put("status", "OK");
  89. conditions.add(condition1);
  90. Map<String, String> condition2 = new HashMap<>();
  91. condition2.put("timestamp", "2024-10-27

Add your comment