Skip to content

Commit

Permalink
refactor: move expectTime into printing
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Mar 10, 2024
1 parent a388276 commit d55bd0e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
25 changes: 15 additions & 10 deletions packages/cli/bin/system_to_chart_next.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:cli/caches.dart';
import 'package:cli/central_command.dart';
import 'package:cli/cli.dart';
import 'package:cli/logic/printing.dart';
import 'package:cli/plan/ships.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
Expand All @@ -19,17 +20,21 @@ Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
ShipType.PROBE,
origin: origin,
)!;
const maxJumps = 5;
const maxJumps = 100;
final behaviors = await BehaviorSnapshot.load(db);
final destinationSymbol = await centralCommand.nextWaypointToChart(
ships,
behaviors,
systemsCache,
chartingSnapshot,
systemConnectivity,
ship,
maxJumps: maxJumps,
);

final destinationSymbol = await expectTime(RequestCounts(), db.queryCounts,
'central planning', const Duration(seconds: 1), () async {
await centralCommand.nextWaypointToChart(
ships,
behaviors,
systemsCache,
chartingSnapshot,
systemConnectivity,
ship,
maxJumps: maxJumps,
);
});
if (destinationSymbol == null) {
logger.info('No uncharted waypoints found within $maxJumps jumps.');
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/lib/logic/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:cli/logger.dart';
import 'package:cli/logic/advance.dart';
import 'package:cli/logic/printing.dart';
import 'package:cli/logic/ship_waiter.dart';
import 'package:cli/net/counts.dart';
import 'package:cli/net/exceptions.dart';
import 'package:db/db.dart';
import 'package:types/types.dart';
Expand Down
41 changes: 41 additions & 0 deletions packages/cli/lib/logic/printing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,44 @@ String describeShipNav(ShipNav nav) {
}
return 'Unknown';
}

/// Log the counts.
void logCounts<T>(Counts<T> counts) {
// Print the counts in order from most to least:
final sorted = counts.counts.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));
for (final entry in sorted) {
logger.info(' ${entry.value}: ${entry.key}');
}
}

/// Run a function and record time and request count and log if long.
Future<T> expectTime<T>(
RequestCounts requestCounts,
QueryCounts queryCounts,
String name,
Duration expected,
Future<T> Function() fn,
) async {
final result = await captureTimeAndRequests<T>(
requestCounts,
queryCounts,
fn,
onComplete: (
Duration duration,
int requestCount,
QueryCounts queryCounts,
) {
if (duration <= expected) {
return;
}
final queryCount = queryCounts.total;
logger.warn(
'$name took too long ${duration.inMilliseconds}ms '
'($requestCount requests, $queryCount queries)',
);
logCounts(queryCounts);
},
);
return result;
}
41 changes: 0 additions & 41 deletions packages/cli/lib/net/counts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,3 @@ class CountingApiClient extends ApiClient {
);
}
}

/// Log the counts.
void logCounts<T>(Counts<T> counts) {
// Print the counts in order from most to least:
final sorted = counts.counts.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));
for (final entry in sorted) {
logger.info(' ${entry.value}: ${entry.key}');
}
}

/// Run a function and record time and request count and log if long.
Future<T> expectTime<T>(
RequestCounts requestCounts,
QueryCounts queryCounts,
String name,
Duration expected,
Future<T> Function() fn,
) async {
final result = await captureTimeAndRequests<T>(
requestCounts,
queryCounts,
fn,
onComplete: (
Duration duration,
int requestCount,
QueryCounts queryCounts,
) {
if (duration <= expected) {
return;
}
final queryCount = queryCounts.total;
logger.warn(
'$name took too long ${duration.inMilliseconds}ms '
'($requestCount requests, $queryCount queries)',
);
logCounts(queryCounts);
},
);
return result;
}

0 comments on commit d55bd0e

Please sign in to comment.