File size: 10,068 Bytes
fd357f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
"""
DTO Cassandra Client - Interface to Cassandra for durable run history
"""

import json
from datetime import datetime
from typing import Dict, Any, Optional, List
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import dict_factory

class DTOCassandraClient:
    def __init__(self, hosts: List[str] = ["localhost"], port: int = 9042,
                 username: Optional[str] = None, password: Optional[str] = None):
        self.hosts = hosts
        self.port = port
        self.username = username
        self.password = password
        self.cluster = None
        self.session = None
        
    def connect(self) -> bool:
        """Connect to Cassandra cluster"""
        try:
            auth_provider = None
            if self.username and self.password:
                auth_provider = PlainTextAuthProvider(
                    username=self.username,
                    password=self.password
                )
            
            self.cluster = Cluster(
                contact_points=self.hosts,
                port=self.port,
                auth_provider=auth_provider,
                protocol_version=4
            )
            
            self.session = self.cluster.connect()
            self.session.set_keyspace('dto_operations')
            self.session.row_factory = dict_factory
            
            print(f"βœ… Connected to Cassandra at {self.hosts}:{self.port}")
            return True
            
        except Exception as e:
            print(f"❌ Failed to connect to Cassandra: {e}")
            return False
    
    def store_run(self, run_data: Dict[str, Any]) -> bool:
        """Store run metadata in Cassandra"""
        if not self.session:
            if not self.connect():
                return False
        
        try:
            query = """
                INSERT INTO runs (
                    run_id, job_id, manifest_path, data_class, environment, status,
                    start_time, end_time, initiated_by, approvers, data_size_bytes,
                    estimated_duration, final_status, total_duration_seconds,
                    average_throughput_mbps, artifacts, metadata
                ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
            """
            
            self.session.execute(query, (
                run_data.get('run_id'),
                run_data.get('job_id'),
                run_data.get('manifest_path'),
                run_data.get('data_class'),
                run_data.get('environment'),
                run_data.get('status'),
                run_data.get('start_time'),
                run_data.get('end_time'),
                run_data.get('initiated_by'),
                run_data.get('approvers', set()),
                run_data.get('data_size_bytes'),
                run_data.get('estimated_duration'),
                run_data.get('final_status'),
                run_data.get('total_duration_seconds'),
                run_data.get('average_throughput_mbps'),
                run_data.get('artifacts', []),
                run_data.get('metadata', {})
            ))
            
            print(f"βœ… Stored run: {run_data.get('run_id')}")
            return True
            
        except Exception as e:
            print(f"❌ Error storing run: {e}")
            return False
    
    def store_metric(self, run_id: str, metric_name: str, value: float, 
                    labels: Optional[Dict[str, str]] = None) -> bool:
        """Store performance metric"""
        if not self.session:
            if not self.connect():
                return False
        
        try:
            # Create minute-precision bucket
            now = datetime.now()
            bucket = datetime(now.year, now.month, now.day, now.hour, now.minute)
            
            query = """
                INSERT INTO metrics (run_id, metric_name, bucket, timestamp, value, labels)
                VALUES (%s, %s, %s, %s, %s, %s)
            """
            
            self.session.execute(query, (
                run_id,
                metric_name,
                bucket,
                now,
                value,
                labels or {}
            ))
            
            print(f"βœ… Stored metric: {run_id} -> {metric_name}: {value}")
            return True
            
        except Exception as e:
            print(f"❌ Error storing metric: {e}")
            return False
    
    def store_event(self, event_data: Dict[str, Any]) -> bool:
        """Store event in durable event store"""
        if not self.session:
            if not self.connect():
                return False
        
        try:
            query = """
                INSERT INTO events (event_id, event_type, timestamp, run_id, job_id, payload)
                VALUES (%s, %s, %s, %s, %s, %s)
            """
            
            self.session.execute(query, (
                event_data.get('event_id'),
                event_data.get('event_type'),
                event_data.get('timestamp'),
                event_data.get('run_id'),
                event_data.get('job_id'),
                json.dumps(event_data.get('payload', {}))
            ))
            
            print(f"βœ… Stored event: {event_data.get('event_type')} for {event_data.get('run_id')}")
            return True
            
        except Exception as e:
            print(f"❌ Error storing event: {e}")
            return False
    
    def get_run(self, run_id: str) -> Optional[Dict[str, Any]]:
        """Get run by ID"""
        if not self.session:
            if not self.connect():
                return None
        
        try:
            query = "SELECT * FROM runs WHERE run_id = %s LIMIT 1"
            result = self.session.execute(query, (run_id,))
            return result.one()
            
        except Exception as e:
            print(f"❌ Error getting run: {e}")
            return None
    
    def get_run_metrics(self, run_id: str, metric_name: Optional[str] = None) -> List[Dict[str, Any]]:
        """Get metrics for a run"""
        if not self.session:
            if not self.connect():
                return []
        
        try:
            if metric_name:
                query = "SELECT * FROM metrics WHERE run_id = %s AND metric_name = %s"
                result = self.session.execute(query, (run_id, metric_name))
            else:
                query = "SELECT * FROM metrics WHERE run_id = %s"
                result = self.session.execute(query, (run_id,))
            
            return list(result)
            
        except Exception as e:
            print(f"❌ Error getting metrics: {e}")
            return []
    
    def get_run_events(self, run_id: str, event_type: Optional[str] = None) -> List[Dict[str, Any]]:
        """Get events for a run"""
        if not self.session:
            if not self.connect():
                return []
        
        try:
            if event_type:
                query = "SELECT * FROM events WHERE run_id = %s AND event_type = %s"
                result = self.session.execute(query, (run_id, event_type))
            else:
                query = "SELECT * FROM events WHERE run_id = %s"
                result = self.session.execute(query, (run_id,))
            
            return list(result)
            
        except Exception as e:
            print(f"❌ Error getting events: {e}")
            return []
    
    def close(self):
        """Close Cassandra connection"""
        if self.cluster:
            self.cluster.shutdown()

# Test function
def test_cassandra_connectivity():
    """Test Cassandra connectivity and basic operations"""
    client = DTOCassandraClient()
    
    if client.connect():
        # Test run storage
        test_run = {
            'run_id': 'test-run-001',
            'job_id': 'test-job-001',
            'manifest_path': '/manifests/class_a/test.yaml',
            'data_class': 'CLASS_A',
            'environment': 'staging',
            'status': 'completed',
            'start_time': datetime.now(),
            'initiated_by': 'prometheus',
            'data_size_bytes': 107374182400,
            'estimated_duration': '2h',
            'final_status': 'SUCCESS',
            'total_duration_seconds': 7200,
            'average_throughput_mbps': 604.0,
            'artifacts': ['/logs/test-run-001.log', '/reports/test-run-001.pdf'],
            'metadata': {'transfer_method': 'ssh+dd', 'compression': 'none'}
        }
        
        client.store_run(test_run)
        
        # Test metric storage
        client.store_metric('test-run-001', 'throughput_mbps', 604.0, {
            'source_host': 'vast2',
            'target_host': 'vast1',
            'transfer_method': 'ssh+dd'
        })
        
        # Test event storage
        test_event = {
            'event_id': 'test-event-001',
            'event_type': 'RUN_COMPLETED',
            'timestamp': datetime.now(),
            'run_id': 'test-run-001',
            'job_id': 'test-job-001',
            'payload': {
                'success': True,
                'final_status': 'SUCCESS',
                'artifacts': ['/logs/test-run-001.log']
            }
        }
        
        client.store_event(test_event)
        
        # Test retrieval
        run = client.get_run('test-run-001')
        print(f"Retrieved run: {run}")
        
        metrics = client.get_run_metrics('test-run-001')
        print(f"Retrieved metrics: {metrics}")
        
        events = client.get_run_events('test-run-001')
        print(f"Retrieved events: {events}")
        
        client.close()
        print("βœ… All Cassandra operations completed successfully")
        return True
    else:
        print("❌ Cassandra connectivity test failed")
        return False

if __name__ == "__main__":
    print("Testing DTO Cassandra Client...")
    print("=" * 50)
    
    test_cassandra_connectivity()