getSystemStats static method

Map<String, dynamic> getSystemStats()

Get detailed statistics about the entire Zen system Useful for monitoring and performance analysis

Implementation

static Map<String, dynamic> getSystemStats() {
  final allScopes = ZenDebug.allScopes;
  var totalDependencies = 0;
  var totalControllers = 0;
  var totalServices = 0;

  for (final scope in allScopes) {
    if (scope.isDisposed) continue; // Skip disposed scopes

    final breakdown = ZenScopeInspector.getDependencyBreakdown(scope);
    final summary = breakdown['summary'] as Map<String, dynamic>? ?? {};

    totalDependencies += (summary['grandTotal'] as int?) ?? 0;
    totalControllers += (summary['totalControllers'] as int?) ?? 0;
    totalServices += (summary['totalServices'] as int?) ?? 0;
  }

  final activeScopes = allScopes.where((s) => !s.isDisposed).length;
  final disposedScopes = allScopes.where((s) => s.isDisposed).length;

  // Get query count from the query cache
  final totalQueries = ZenQueryCache.instance.queries.length;

  // Try to get navigation info (if NavigationService is registered)
  Map<String, dynamic>? navigationInfo;
  try {
    // Search all scopes for NavigationService
    for (final scope in allScopes) {
      if (scope.isDisposed) continue;

      final instances = ZenScopeInspector.getAllInstances(scope);
      for (final entry in instances.entries) {
        final instance = entry.value;
        if (instance.runtimeType.toString().contains('NavigationService')) {
          try {
            final stats = (instance as dynamic).getStats()
                as Map<String, dynamic>?; // coverage:ignore-line
            if (stats != null) {
              navigationInfo = {
                // coverage:ignore-line
                'currentRoute': stats['currentPath']?.toString() ??
                    '/', // coverage:ignore-line
                'navigationCount': stats['totalNavigations']?.toString() ??
                    '0', // coverage:ignore-line
                'breadcrumbCount': stats['breadcrumbCount']?.toString() ??
                    '0', // coverage:ignore-line
              };
              break;
            }
          } catch (e) {
            // NavigationService doesn't have getStats or different type
          }
        }
      }
      if (navigationInfo != null) break;
    }
  } catch (e) {
    // Navigation tracking not available
  }

  // Return flat structure for inspector views
  return {
    // Flat keys for inspector views
    'totalScopes': activeScopes,
    'totalDependencies': totalDependencies,
    'totalControllers': totalControllers,
    'totalServices': totalServices,
    'totalQueries': totalQueries,

    // Navigation info (if available)
    if (navigationInfo != null)
      'navigation': navigationInfo, // coverage:ignore-line

    // Nested structure for detailed reporting
    'scopes': {
      'total': allScopes.length,
      'active': activeScopes,
      'disposed': disposedScopes,
    },
    'dependencies': {
      'total': totalDependencies,
      'controllers': totalControllers,
      'services': totalServices,
      'others': totalDependencies - totalControllers - totalServices,
    },
    'performance': {
      'averageDependenciesPerScope': activeScopes > 0
          ? (totalDependencies / activeScopes).toStringAsFixed(2)
          : '0',
    }
  };
}