1. import java.util.*;
  2. import java.util.concurrent.*;
  3. import java.time.Instant;
  4. import java.time.Duration;
  5. public class TimestampAnomalyDetector {
  6. private final int windowSize; // Time window for anomaly detection (in seconds)
  7. private final int maxRequestsPerWindow; // Maximum requests allowed within the window
  8. private final Queue<Long> timestamps; // Queue to store timestamps
  9. private final Semaphore semaphore; // Semaphore for rate limiting
  10. private final List<TimestampAnomaly> anomalies; // List to store detected anomalies
  11. public TimestampAnomalyDetector(int windowSize, int maxRequestsPerWindow) {
  12. this.windowSize = windowSize;
  13. this.maxRequestsPerWindow = maxRequestsPerWindow;
  14. this.timestamps = new LinkedList<>();
  15. this.semaphore = new Semaphore(maxRequestsPerWindow);
  16. this.anomalies = new ArrayList<>();
  17. }
  18. public synchronized boolean allowRequest() throws InterruptedException {
  19. semaphore.acquire(); // Acquire a permit from the semaphore (rate limiting)
  20. return true;
  21. }
  22. public synchronized void releaseRequest() {
  23. semaphore.release(); // Release the permit
  24. }
  25. public TimestampAnomaly detectAnomalies() {
  26. // Process timestamps and detect anomalies
  27. long currentTime = Instant.now().getEpochSecond();
  28. // Remove timestamps outside the current window
  29. while (!timestamps.isEmpty() && (currentTime - timestamps.peek() > windowSize)) {
  30. timestamps.poll();
  31. }
  32. // Check if the number of timestamps exceeds the rate limit
  33. if (timestamps.size() > maxRequestsPerWindow) {
  34. List<TimestampAnomaly> anomaly = new ArrayList<>();
  35. anomaly.add(new TimestampAnomaly("Timestamp Exceeded Rate Limit", timestamps.size(), currentTime));
  36. anomalies.addAll(anomaly);
  37. timestamps.clear(); // Reset the timestamps
  38. return anomaly.get(0);
  39. }
  40. return null; // No anomaly detected
  41. }
  42. public void addTimestamp(long timestamp) throws InterruptedException {
  43. // Add a new timestamp to the queue
  44. timestamps.offer(timestamp);
  45. // Simulate processing time
  46. try {
  47. Thread.sleep(100); // Simulate some processing time
  48. } catch (InterruptedException e) {
  49. Thread.currentThread().interrupt();
  50. }
  51. }
  52. public List<TimestampAnomaly> getAnomalies() {
  53. return anomalies;
  54. }
  55. // Inner class to represent a timestamp anomaly
  56. public static class TimestampAnomaly {
  57. private final String message;
  58. private final int count;
  59. private final long timestamp;
  60. public TimestampAnomaly(String message, int count, long timestamp) {
  61. this.message = message;
  62. this.count = count;
  63. this.timestamp = timestamp;
  64. }
  65. public String getMessage() {
  66. return message;
  67. }
  68. public int getCount() {
  69. return count;
  70. }
  71. public long getTimestamp() {
  72. return timestamp;
  73. }
  74. }
  75. public static void main(String[] args) throws InterruptedException {
  76. // Example usage
  77. TimestampAnomalyDetector detector = new TimestampAnomalyDetector(60, 10); // 60 seconds window, 10 requests/window
  78. for (int i = 0; i < 15; i++) {
  79. long timestamp = Instant.now().getEpochSecond() - (15 - i) * 30; // Generate timestamps
  80. detector.addTimestamp(timestamp);
  81. TimestampAnomaly anomaly = detector.detectAnomalies();
  82. if (anomaly != null) {
  83. System.out.println("Anomaly detected: " + anomaly.getMessage() + " at timestamp: " + anomaly.getTimestamp());
  84. }
  85. Thread.sleep(500);
  86. }
  87. List<TimestampAnomaly> allAnomalies = detector.getAnomalies();
  88. System.out.println("Total Anomalies: " + allAnomalies.size());
  89. }
  90. }

Add your comment