Fix list_cursors command for globals (#1840)

This commit is contained in:
Weimin Yu 2022-11-04 11:18:20 -04:00 committed by GitHub
parent 242864d198
commit 558bfd1ca2
2 changed files with 22 additions and 15 deletions

View file

@ -51,31 +51,31 @@ final class ListCursorsCommand implements CommandWithRemoteApi {
@Override
public void run() {
Map<Registry, VKey<Cursor>> registries =
Registries.getTlds().stream()
Map<String, VKey<Cursor>> cursorKeys =
cursorType.isScoped()
? Registries.getTlds().stream()
.map(Registry::get)
.filter(r -> r.getTldType() == filterTldType)
.filter(r -> !filterEscrowEnabled || r.getEscrowEnabled())
.collect(toImmutableMap(r -> r, r -> Cursor.createScopedVKey(cursorType, r)));
.collect(
toImmutableMap(r -> r.getTldStr(), r -> Cursor.createScopedVKey(cursorType, r)))
: ImmutableMap.of(cursorType.name(), Cursor.createGlobalVKey(cursorType));
ImmutableMap<VKey<? extends Cursor>, Cursor> cursors =
tm().transact(() -> tm().loadByKeysIfPresent(registries.values()));
if (!registries.isEmpty()) {
tm().transact(() -> tm().loadByKeysIfPresent(cursorKeys.values()));
if (!cursorKeys.isEmpty()) {
String header = String.format(OUTPUT_FMT, "TLD", "Cursor Time", "Last Update Time");
System.out.printf("%s\n%s\n", header, Strings.repeat("-", header.length()));
registries.entrySet().stream()
.map(
e ->
renderLine(
e.getKey().getTldStr(), Optional.ofNullable(cursors.get(e.getValue()))))
cursorKeys.entrySet().stream()
.map(e -> renderLine(e.getKey(), Optional.ofNullable(cursors.get(e.getValue()))))
.sorted()
.forEach(System.out::println);
}
}
private static String renderLine(String tld, Optional<Cursor> cursor) {
private static String renderLine(String name, Optional<Cursor> cursor) {
return String.format(
OUTPUT_FMT,
tld,
name,
cursor.map(c -> c.getCursorTime().toString()).orElse("(absent)"),
cursor.map(c -> c.getLastUpdateTime().toString()).orElse("(absent)"));
}

View file

@ -15,6 +15,7 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.createTlds;
import static google.registry.testing.DatabaseHelper.persistResource;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -41,6 +42,12 @@ public class ListCursorsCommandTest extends CommandTestCase<ListCursorsCommand>
fakeClock.setTo(DateTime.parse("1984-12-21T06:07:08.789Z"));
}
@Test
void testlistCursors_globalScoped() throws Exception {
createTld("com");
runCommand("--type=RECURRING_BILLING");
}
@Test
void testListCursors_noTlds_printsNothing() throws Exception {
runCommand("--type=BRDA");