-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
170 lines (134 loc) · 5.63 KB
/
examples.py
File metadata and controls
170 lines (134 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Examples for using the time_based_storage package in various scenarios.
"""
import time
import threading
from datetime import datetime, timedelta
from time_based_storage import (
TimeBasedStorage,
TimeBasedStorageHeap,
ThreadSafeTimeBasedStorage,
ThreadSafeTimeBasedStorageHeap
)
def example_basic_usage():
"""Demonstrate basic usage of TimeBasedStorage."""
print("\n=== Basic Usage ===")
# Create a storage instance with string values
storage = TimeBasedStorage[str]()
# Add events
now = datetime.now()
storage.add(now - timedelta(minutes=30), "Event from 30 minutes ago")
storage.add(now - timedelta(minutes=20), "Event from 20 minutes ago")
storage.add(now - timedelta(minutes=10), "Event from 10 minutes ago")
storage.add(now, "Current event")
# Get all events
print(f"Total events: {storage.size()}")
all_events = storage.get_all()
print(f"All events: {all_events}")
# Get range of events
start_time = now - timedelta(minutes=25)
end_time = now - timedelta(minutes=5)
range_events = storage.get_range(start_time, end_time)
print(f"Events between 25 and 5 minutes ago: {range_events}")
# Get recent events (within last 15 minutes)
duration = 15 * 60 # 15 minutes in seconds
recent_events = storage.get_duration(duration)
print(f"Events in the last 15 minutes: {recent_events}")
# Remove an event
removal_time = now - timedelta(minutes=20)
storage.remove(removal_time)
print(f"After removal, events: {storage.get_all()}")
def example_timestamp_collision_handling():
"""Demonstrate how to handle timestamp collisions."""
print("\n=== Timestamp Collision Handling ===")
storage = TimeBasedStorage[str]()
# Create a timestamp
timestamp = datetime(2024, 1, 1, 12, 0, 0)
# Add first event
storage.add(timestamp, "First event")
print(f"Added event at {timestamp}")
try:
# Try to add another event with the same timestamp
storage.add(timestamp, "Second event")
except ValueError as e:
print(f"Error: {e}")
# Use add_unique_timestamp to handle collisions
unique_timestamp = storage.add_unique_timestamp(timestamp, "Second event")
print(f"Added with unique timestamp: {unique_timestamp}")
# Get all timestamps
all_timestamps = storage.get_timestamps()
print(f"All timestamps: {all_timestamps}")
# Get all values
all_values = storage.get_all()
print(f"All values: {all_values}")
def example_thread_safe_storage():
"""Demonstrate usage of thread-safe storage with multiple threads."""
print("\n=== Thread-Safe Storage ===")
# Create thread-safe storage
storage = ThreadSafeTimeBasedStorage[int]()
event = threading.Event()
def producer():
"""Add values to the storage."""
for i in range(5):
timestamp = datetime.now()
storage.add(timestamp, i)
print(f"Producer: Added {i} at {timestamp}")
time.sleep(0.5)
event.set() # Signal consumer to stop
def consumer():
"""Read values from the storage."""
while not event.is_set():
if storage.wait_for_data(timeout=0.2):
values = storage.get_all()
timestamps = storage.get_timestamps()
print(f"Consumer: Current values: {values}")
print(f"Consumer: Total entries: {len(timestamps)}")
else:
print("Consumer: No new data")
# Start threads
producer_thread = threading.Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)
producer_thread.start()
consumer_thread.start()
# Wait for threads to complete
producer_thread.join()
consumer_thread.join()
def example_event_monitoring_system():
"""Demonstrate a practical use case: event monitoring system."""
print("\n=== Event Monitoring System Example ===")
monitor = TimeBasedStorageHeap[dict]()
# Simulate monitoring events with different priorities
events = [
{"type": "INFO", "message": "System started", "priority": 1},
{"type": "WARNING", "message": "High CPU usage", "priority": 2},
{"type": "ERROR", "message": "Database connection failed", "priority": 3},
{"type": "INFO", "message": "User logged in", "priority": 1},
{"type": "CRITICAL", "message": "Out of memory", "priority": 4},
]
# Add events with timestamps
now = datetime.now()
for i, event in enumerate(events):
# Simulate events happening at different times
timestamp = now - timedelta(minutes=10) + timedelta(minutes=i*2)
monitor.add(timestamp, event)
# Get all events
all_events = monitor.get_all()
print("All monitoring events:")
for event in all_events:
print(f"- [{event['type']}] {event['message']} (Priority: {event['priority']})")
# Get high priority events (WARNING, ERROR, CRITICAL)
high_priority = [e for e in all_events if e['priority'] >= 2]
print("\nHigh priority events:")
for event in high_priority:
print(f"- [{event['type']}] {event['message']} (Priority: {event['priority']})")
# Get most recent event (last 2 minutes)
duration = 2 * 60 # 2 minutes in seconds
recent = monitor.get_duration(duration)
print("\nMost recent events (last 2 minutes):")
for event in recent:
print(f"- [{event['type']}] {event['message']}")
if __name__ == "__main__":
example_basic_usage()
example_timestamp_collision_handling()
example_thread_safe_storage()
example_event_monitoring_system()