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

View file

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