Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions lib/features/mongodb/mongo_stats_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,12 @@ class _MongoStatsViewState extends material.State<MongoStatsView>
material.Widget _memoryCard(
material.BuildContext context, Map<String, dynamic> status) {
final mem = status['mem'] as Map<String, dynamic>?;
final resident = _getInt(mem, 'resident') ?? 0;
final virtual = _getInt(mem, 'virtual') ?? 0;
final mapped = _getInt(mem, 'mapped') ?? 0;
final mappedWithJournal = _getInt(mem, 'mappedWithJournal') ?? 0;
// serverStatus reports `mem` in MiB, not bytes.
const mib = 1024 * 1024;
final resident = (_getInt(mem, 'resident') ?? 0) * mib;
final virtual = (_getInt(mem, 'virtual') ?? 0) * mib;
final mapped = (_getInt(mem, 'mapped') ?? 0) * mib;
final mappedWithJournal = (_getInt(mem, 'mappedWithJournal') ?? 0) * mib;
return _card(
context,
'Memory',
Expand Down Expand Up @@ -799,12 +801,7 @@ class _MongoStatsViewState extends material.State<MongoStatsView>
return v?.toString();
}

int? _getInt(Map<String, dynamic>? map, String key) {
final v = map?[key];
if (v is int) return v;
if (v is num) return v.toInt();
return null;
}
int? _getInt(Map<String, dynamic>? map, String key) => mongoStatInt(map?[key]);

int? _getNestedInt(Map<String, dynamic>? map, String key1, String key2,
[String? key3]) {
Expand All @@ -813,15 +810,9 @@ class _MongoStatsViewState extends material.State<MongoStatsView>
if (key3 != null) {
final m2 = m1[key2] as Map<String, dynamic>?;
if (m2 == null) return null;
final v = m2[key3];
if (v is int) return v;
if (v is num) return v.toInt();
return null;
return mongoStatInt(m2[key3]);
}
final v = m1[key2];
if (v is int) return v;
if (v is num) return v.toInt();
return null;
return mongoStatInt(m1[key2]);
}

Map<String, String> _extractServerInfo(Map<String, dynamic> status) {
Expand Down Expand Up @@ -892,3 +883,14 @@ class _MongoStatsViewState extends material.State<MongoStatsView>
return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(1)} GB';
}
}

/// A `serverStatus` number as an [int]: [int], [num], or bson `Int64`, which is
/// not a Dart [num] (counters such as `opcounters` and `network.bytesIn` come
/// as Int64 and used to read as 0).
@visibleForTesting
int? mongoStatInt(Object? v) {
if (v == null) return null;
if (v is int) return v;
if (v is num) return v.toInt();
return int.tryParse(v.toString());
}
22 changes: 22 additions & 0 deletions test/features/mongodb/mongo_stats_numbers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:querya_desktop/features/mongodb/mongo_stats_view.dart';

/// Stands in for bson's `Int64`: not a Dart [num], prints its value.
class _Int64Like {
const _Int64Like(this.value);
final int value;

@override
String toString() => '$value';
}

void main() {
test('serverStatus numbers read as ints, Int64 included', () {
expect(mongoStatInt(42), 42);
expect(mongoStatInt(42.9), 42);
// opcounters and network.bytesIn arrive as Int64; they used to read as 0.
expect(mongoStatInt(const _Int64Like(577674)), 577674);
expect(mongoStatInt(null), isNull);
expect(mongoStatInt('not a number'), isNull);
});
}
Loading