scirs2-metrics 0.4.3

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Collaboration features for interactive visualization
//!
//! This module provides multi-user collaboration capabilities for
//! shared dashboard editing and real-time synchronization.

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use super::core::{CollaborationConfig, PermissionLevel};
use crate::error::{MetricsError, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Collaboration manager for multi-user dashboards
#[derive(Debug)]
pub struct CollaborationManager {
    /// Configuration
    config: CollaborationConfig,
    /// Active sessions
    sessions: HashMap<String, UserSession>,
    /// Shared state
    shared_state: SharedState,
    /// Operation history
    operation_history: Vec<Operation>,
    /// Conflict resolver
    conflict_resolver: ConflictResolver,
}

/// User session information
#[derive(Debug, Clone)]
pub struct UserSession {
    /// Session ID
    pub session_id: String,
    /// User ID
    pub user_id: String,
    /// User name
    pub user_name: String,
    /// Permission level
    pub permissions: PermissionLevel,
    /// Last activity timestamp
    pub last_activity: Instant,
    /// Current cursor position
    pub cursor_position: Option<CursorPosition>,
    /// Active selections
    pub selections: Vec<Selection>,
}

/// Cursor position in the dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CursorPosition {
    /// X coordinate
    pub x: f64,
    /// Y coordinate
    pub y: f64,
    /// Widget ID (if hovering over widget)
    pub widget_id: Option<String>,
}

/// Selection in the dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Selection {
    /// Selection ID
    pub id: String,
    /// Selected widget IDs
    pub widget_ids: Vec<String>,
    /// Selection bounds
    pub bounds: SelectionBounds,
    /// Selection type
    pub selection_type: SelectionType,
}

/// Selection bounds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelectionBounds {
    /// Top-left X coordinate
    pub x: f64,
    /// Top-left Y coordinate
    pub y: f64,
    /// Width
    pub width: f64,
    /// Height
    pub height: f64,
}

/// Selection type enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SelectionType {
    /// Single widget selection
    Single,
    /// Multiple widget selection
    Multiple,
    /// Area selection
    Area,
    /// Text selection
    Text,
}

/// Shared state for collaborative editing
#[derive(Debug, Clone)]
pub struct SharedState {
    /// Dashboard state
    pub dashboard_state: Value,
    /// Widget states
    pub widget_states: HashMap<String, Value>,
    /// Global settings
    pub global_settings: HashMap<String, Value>,
    /// Version vector for consistency
    pub version_vector: HashMap<String, u64>,
}

/// Collaborative operation
#[derive(Debug, Clone)]
pub struct Operation {
    /// Operation ID
    pub id: String,
    /// Operation type
    pub operation_type: OperationType,
    /// User ID who performed the operation
    pub user_id: String,
    /// Timestamp
    pub timestamp: Instant,
    /// Operation data
    pub data: Value,
    /// Target (widget ID, etc.)
    pub target: String,
    /// Dependencies (operation IDs)
    pub dependencies: Vec<String>,
}

/// Operation type enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OperationType {
    /// Create widget
    Create,
    /// Update widget
    Update,
    /// Delete widget
    Delete,
    /// Move widget
    Move,
    /// Resize widget
    Resize,
    /// Change style
    StyleChange,
    /// Data update
    DataUpdate,
    /// Custom operation
    Custom(String),
}

/// Conflict resolver for handling concurrent operations
#[derive(Debug)]
pub struct ConflictResolver {
    /// Resolution strategy
    strategy: ConflictResolutionStrategy,
    /// Pending conflicts
    pending_conflicts: Vec<Conflict>,
}

/// Conflict resolution strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictResolutionStrategy {
    /// Last writer wins
    LastWriterWins,
    /// First writer wins
    FirstWriterWins,
    /// Operational transform
    OperationalTransform,
    /// Manual resolution
    Manual,
    /// Custom strategy
    Custom(String),
}

/// Conflict between operations
#[derive(Debug, Clone)]
pub struct Conflict {
    /// Conflict ID
    pub id: String,
    /// Conflicting operations
    pub operations: Vec<Operation>,
    /// Conflict type
    pub conflict_type: ConflictType,
    /// Resolution status
    pub status: ConflictStatus,
    /// Proposed resolution
    pub proposed_resolution: Option<Operation>,
}

/// Conflict type enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictType {
    /// Concurrent modifications
    ConcurrentModification,
    /// Version mismatch
    VersionMismatch,
    /// Permission conflict
    PermissionConflict,
    /// Data integrity conflict
    DataIntegrity,
    /// Custom conflict
    Custom(String),
}

/// Conflict status enumeration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ConflictStatus {
    /// Pending resolution
    Pending,
    /// Automatically resolved
    AutoResolved,
    /// Manually resolved
    ManuallyResolved,
    /// Ignored
    Ignored,
}

impl CollaborationManager {
    /// Create new collaboration manager
    pub fn new(config: CollaborationConfig) -> Self {
        Self {
            config,
            sessions: HashMap::new(),
            shared_state: SharedState::new(),
            operation_history: Vec::new(),
            conflict_resolver: ConflictResolver::new(),
        }
    }

    /// Add user session
    pub fn add_session(&mut self, session: UserSession) -> Result<()> {
        if self.sessions.len() >= self.config.max_collaborators as usize {
            return Err(MetricsError::ComputationError(
                "Maximum number of collaborators reached".to_string(),
            ));
        }

        self.sessions.insert(session.session_id.clone(), session);
        Ok(())
    }

    /// Remove user session
    pub fn remove_session(&mut self, session_id: &str) -> Result<()> {
        self.sessions.remove(session_id);
        Ok(())
    }

    /// Apply operation
    pub fn apply_operation(&mut self, operation: Operation) -> Result<()> {
        // Check permissions
        if let Some(session) = self.sessions.get(&operation.user_id) {
            if !self.has_permission(&session.permissions, &operation.operation_type) {
                return Err(MetricsError::InvalidInput(
                    "Insufficient permissions".to_string(),
                ));
            }
        }

        // Check for conflicts
        if let Some(conflict) = self
            .conflict_resolver
            .detect_conflict(&operation, &self.operation_history)
        {
            self.conflict_resolver.pending_conflicts.push(conflict);
            return Ok(()); // Don't apply yet, needs resolution
        }

        // Apply operation to shared state
        self.apply_operation_to_state(&operation)?;

        // Add to history
        self.operation_history.push(operation);

        Ok(())
    }

    /// Apply operation to shared state
    fn apply_operation_to_state(&mut self, operation: &Operation) -> Result<()> {
        match operation.operation_type {
            OperationType::Create => {
                self.shared_state
                    .widget_states
                    .insert(operation.target.clone(), operation.data.clone());
            }
            OperationType::Update => {
                if let Some(widget_state) =
                    self.shared_state.widget_states.get_mut(&operation.target)
                {
                    // Merge update data
                    if let (Value::Object(current), Value::Object(update)) =
                        (widget_state, &operation.data)
                    {
                        for (key, value) in update {
                            current.insert(key.clone(), value.clone());
                        }
                    }
                }
            }
            OperationType::Delete => {
                self.shared_state.widget_states.remove(&operation.target);
            }
            _ => {
                // Handle other operation types
            }
        }

        // Update version vector
        self.shared_state
            .version_vector
            .entry(operation.user_id.clone())
            .and_modify(|v| *v += 1)
            .or_insert(1);

        Ok(())
    }

    /// Check if user has permission for operation
    fn has_permission(&self, permission: &PermissionLevel, operation: &OperationType) -> bool {
        match (permission, operation) {
            (PermissionLevel::Admin, _) => true,
            (
                PermissionLevel::Edit,
                OperationType::Create | OperationType::Update | OperationType::Delete,
            ) => true,
            (PermissionLevel::Comment, _) => false, // Comments not implemented in this example
            (PermissionLevel::ReadOnly, _) => false,
            _ => false,
        }
    }

    /// Get active sessions
    pub fn get_active_sessions(&self) -> Vec<&UserSession> {
        self.sessions.values().collect()
    }

    /// Update user cursor position
    pub fn update_cursor(&mut self, session_id: &str, position: CursorPosition) -> Result<()> {
        if let Some(session) = self.sessions.get_mut(session_id) {
            session.cursor_position = Some(position);
            session.last_activity = Instant::now();
        }
        Ok(())
    }

    /// Get shared state
    pub fn get_shared_state(&self) -> &SharedState {
        &self.shared_state
    }

    /// Resolve conflicts using the conflict resolver
    pub fn resolve_conflicts(&mut self) -> Result<Vec<Operation>> {
        self.conflict_resolver.resolve_conflicts()
    }
}

impl SharedState {
    /// Create new shared state
    pub fn new() -> Self {
        Self {
            dashboard_state: Value::Object(serde_json::Map::new()),
            widget_states: HashMap::new(),
            global_settings: HashMap::new(),
            version_vector: HashMap::new(),
        }
    }
}

impl ConflictResolver {
    /// Create new conflict resolver
    pub fn new() -> Self {
        Self {
            strategy: ConflictResolutionStrategy::LastWriterWins,
            pending_conflicts: Vec::new(),
        }
    }

    /// Detect conflict between operation and history
    pub fn detect_conflict(
        &self,
        operation: &Operation,
        history: &[Operation],
    ) -> Option<Conflict> {
        // Simplified conflict detection
        for existing_op in history.iter().rev().take(10) {
            if existing_op.target == operation.target
                && existing_op.timestamp
                    > operation
                        .timestamp
                        .checked_sub(Duration::from_secs(5))
                        .unwrap_or(operation.timestamp)
            {
                return Some(Conflict {
                    id: format!("conflict_{}", scirs2_core::random::random::<u64>()),
                    operations: vec![existing_op.clone(), operation.clone()],
                    conflict_type: ConflictType::ConcurrentModification,
                    status: ConflictStatus::Pending,
                    proposed_resolution: None,
                });
            }
        }
        None
    }

    /// Resolve pending conflicts
    pub fn resolve_conflicts(&mut self) -> Result<Vec<Operation>> {
        let mut resolved_operations = Vec::new();

        for conflict in &mut self.pending_conflicts {
            match self.strategy {
                ConflictResolutionStrategy::LastWriterWins => {
                    if let Some(latest_op) =
                        conflict.operations.iter().max_by_key(|op| op.timestamp)
                    {
                        resolved_operations.push(latest_op.clone());
                        conflict.status = ConflictStatus::AutoResolved;
                    }
                }
                ConflictResolutionStrategy::FirstWriterWins => {
                    if let Some(earliest_op) =
                        conflict.operations.iter().min_by_key(|op| op.timestamp)
                    {
                        resolved_operations.push(earliest_op.clone());
                        conflict.status = ConflictStatus::AutoResolved;
                    }
                }
                _ => {
                    // Other strategies would be implemented here
                }
            }
        }

        // Remove resolved conflicts
        self.pending_conflicts
            .retain(|conflict| conflict.status == ConflictStatus::Pending);

        Ok(resolved_operations)
    }
}

impl Default for SharedState {
    fn default() -> Self {
        Self::new()
    }
}