github-actions[bot] commented on code in PR #41408: URL: https://github.com/apache/beam/pull/41408#discussion_r4592095224
########## sdks/java/extensions/sql/jdbc/src/main/java/org/apache/beam/sdk/extensions/sql/jdbc/LiveResultView.java: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.sql.jdbc; + +import static java.util.stream.Collectors.toList; + +import java.io.IOException; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.function.Consumer; +import org.apache.beam.sdk.PipelineResult.State; +import org.apache.beam.sdk.extensions.sql.impl.rel.LiveResult; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.jline.keymap.BindingReader; +import org.jline.keymap.KeyMap; +import org.jline.terminal.Attributes; +import org.jline.terminal.Size; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; +import org.jline.utils.AttributedString; +import org.jline.utils.Display; +import org.jline.utils.InfoCmp.Capability; +import org.jline.utils.NonBlockingReader; + +/** + * Shows the rows of an unbounded query in a full-screen terminal view while its pipeline runs. + * + * <p>The column names stay at the top and new rows come in at the bottom. The first column, {@code + * op}, shows each row's kind of change: +I, -U, +U or -D. The arrow keys, PgUp, PgDn, Home and End + * scroll, and so do vi's h, j, k, l, g and G. Scrolling up pauses the view on the rows you're + * looking at, and End follows the newest rows again. q or Esc closes the view. Ctrl+C belongs to + * the shell: it cancels the statement, which closes the view too. The pipeline is cancelled once + * the view closes. + * + * <p>The view opens its own JLine 3 terminal while it's on screen, next to the shell's jline2 line + * editor. That terminal leaves the JVM's signal handlers alone, so the shell keeps handling Ctrl+C. + */ +public class LiveResultView implements LiveResult.Viewer { + /** Opens the terminal that the view draws on. The view closes it when it's done. */ + public interface TerminalFactory { + Terminal open() throws IOException; + } + + /** How many of the newest rows the view keeps for scrolling back. */ + private static final int MAX_ROWS = 10_000; + + private static final String TITLE = "Beam SQL live results"; + private static final long FRAME_MILLIS = 100; + private static final long KEY_WAIT_MILLIS = 50; + private static final long SIZE_CHECK_MILLIS = 250; + private static final long ESCAPE_TIMEOUT_MILLIS = 100; + private static final Size DEFAULT_SIZE = new Size(80, 24); + + private final TerminalFactory terminals; + private final Consumer<List<String>> frames; + + /** Shows the rows on the terminal that the shell runs in. */ + public LiveResultView() { + this( + () -> + TerminalBuilder.builder() + .system(true) + // Leave the JVM's signal handlers alone, so the shell's Ctrl+C handler works. + .nativeSignals(false) + .build(), + frame -> {}); + } + + /** + * Shows the rows on the terminal that {@code terminals} opens, and passes the text of each frame + * it draws to {@code frames}. Meant for tests. + */ + public LiveResultView(TerminalFactory terminals, Consumer<List<String>> frames) { + this.terminals = terminals; + this.frames = frames; + } + + @Override + public void show(LiveResult result) throws IOException { + LiveResultScreen screen = new LiveResultScreen(TITLE, result.getSchema(), MAX_ROWS); + Runnable quit = () -> {}; + long start = System.currentTimeMillis(); Review Comment: **๐ข Low**: `System.currentTimeMillis()` is subject to wall-clock adjustments (e.g., NTP syncs), which can cause `now - lastFrame`, `now - lastSizeCheck`, or `elapsed` to jump backward or stall redraws. Using `TimeUnit.NANOSECONDS.toMillis(System.nanoTime())` provides a monotonic clock for elapsed time and frame/resize throttling. ```suggestion long start = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); ``` ########## sdks/java/extensions/sql/jdbc/src/main/java/org/apache/beam/sdk/extensions/sql/jdbc/LiveResultScreen.java: ########## @@ -0,0 +1,480 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.sql.jdbc; + +import java.math.BigDecimal; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import org.apache.beam.sdk.PipelineResult.State; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.ValueKind; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.jline.utils.AttributedString; +import org.jline.utils.AttributedStringBuilder; +import org.jline.utils.AttributedStyle; +import org.joda.time.ReadableInstant; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + +/** + * What the live view shows: the newest rows of a query under a pinned header, scrolled to where the + * user wants. It only works out the lines of the screen. {@link LiveResultView} draws them on the + * terminal and turns keys into calls to this class. + * + * <p>Column widths start at the width of the column names and grow with the data, up to {@link + * #MAX_COLUMN_WIDTH}. Longer values are cut off with "โฆ". + */ +class LiveResultScreen { + static final int MAX_COLUMN_WIDTH = 30; + + /** How far the left and right keys scroll. */ + private static final int SCROLL_COLUMNS = 8; + + /** Lines used by the title, the column names, the line under them, and the footer. */ + private static final int CHROME_LINES = 4; + + private static final String SEPARATOR = " โ "; + private static final String NULL = "NULL"; + private static final String ELLIPSIS = "โฆ"; + private static final String KEYS = "โโ PgUp PgDn Home End โโ scroll ยท q quit"; + + private static final DateTimeFormatter TIMESTAMP_FORMAT = + DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").withZoneUTC(); + + private static final AttributedStyle BAR = AttributedStyle.INVERSE; + private static final AttributedStyle HEADER = AttributedStyle.BOLD; + private static final AttributedStyle RULE = AttributedStyle.DEFAULT.faint(); + private static final AttributedStyle ADDED = + AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN); + private static final AttributedStyle RETRACTED = + AttributedStyle.DEFAULT.foreground(AttributedStyle.RED); + + /** The state of the pipeline, shown in the title and the footer. */ + static class Status { + final State state; + final long elapsedMillis; + final @Nullable String failure; + + Status(State state, long elapsedMillis, @Nullable String failure) { + this.state = state; + this.elapsedMillis = elapsedMillis; + this.failure = failure; + } + } + + /** A row of the result, ready to print. */ + private static class Line { + final ValueKind kind; + final String[] cells; + + Line(ValueKind kind, String[] cells) { + this.kind = kind; + this.cells = cells; + } + } + + private final String title; + private final List<String> names; + private final boolean[] rightAligned; + private final int[] widths; + private final int maxRows; + + /** The newest rows, oldest first, in a ring of {@code maxRows} slots. */ + private final @Nullable Line[] ring; + + private int first; + private int size; + private long received; + private long evicted; + + /** Whether the view keeps the newest rows on screen. */ + private boolean following = true; + + /** The row at the top of the screen while not following. */ + private int top; + + /** Rows that came in since the user scrolled away from the newest rows. */ + private long unseen; + + private int offsetX; + private int bodyHeight = 1; + + LiveResultScreen(String title, Schema schema, int maxRows) { + this.title = title; + this.maxRows = maxRows; + this.ring = new Line[maxRows]; + List<String> names = new ArrayList<>(); + names.add("op"); + names.addAll(schema.getFieldNames()); + this.names = Collections.unmodifiableList(names); + this.rightAligned = new boolean[names.size()]; + this.widths = new int[names.size()]; + for (int i = 0; i < names.size(); i++) { + widths[i] = Math.max(NULL.length(), width(names.get(i))); + } + for (int i = 0; i < schema.getFieldCount(); i++) { + rightAligned[i + 1] = isNumeric(schema.getField(i).getType()); + } + } + + /** Adds the next row of the result. */ + void add(ValueKind kind, Row row) { + String[] cells = new String[names.size()]; + cells[0] = opCode(kind); + for (int i = 0; i < row.getFieldCount(); i++) { + cells[i + 1] = format(row.getValue(i)); + } + for (int i = 0; i < cells.length; i++) { + widths[i] = Math.max(widths[i], Math.min(MAX_COLUMN_WIDTH, width(cells[i]))); + } + + received++; + if (size == maxRows) { + first = (first + 1) % maxRows; + size--; + evicted++; + top = Math.max(0, top - 1); + } + ring[(first + size) % maxRows] = new Line(kind, cells); + size++; + if (!following) { + unseen++; + } + } + + long getReceivedCount() { + return received; + } + + boolean isFollowing() { + return following; + } + + void lineUp() { + scrollTo(currentTop() - 1); + } + + void lineDown() { + scrollTo(currentTop() + 1); + } + + void pageUp() { + scrollTo(currentTop() - bodyHeight); + } + + void pageDown() { + scrollTo(currentTop() + bodyHeight); + } + + void home() { + scrollTo(0); + } + + void end() { + following = true; + unseen = 0; + } + + void left() { + offsetX = Math.max(0, offsetX - SCROLL_COLUMNS); + } + + void right() { + offsetX += SCROLL_COLUMNS; + } + + /** Scrolls so that row {@code row} is at the top, and follows the newest rows at the bottom. */ + private void scrollTo(int row) { + int bottom = Math.max(0, size - bodyHeight); + if (row >= bottom) { + end(); + } else { + following = false; + top = Math.max(0, row); + } + } + + private int currentTop() { + return following ? Math.max(0, size - bodyHeight) : top; + } + + /** Returns the lines of a screen {@code width} columns wide and {@code height} lines high. */ + List<AttributedString> render(int width, int height, Status status) { + List<AttributedString> lines = new ArrayList<>(); + if (height < CHROME_LINES + 1 || width < 20) { + for (int i = 0; i < height; i++) { + lines.add(i == 0 ? fit("Make the terminal bigger to see the rows", width) : blank()); + } + return lines; + } + bodyHeight = height - CHROME_LINES; + + int tableWidth = tableWidth(); + offsetX = Math.max(0, Math.min(offsetX, tableWidth - width)); Review Comment: **๐ก Medium**: When all columns in the result have already reached `MAX_COLUMN_WIDTH` while the user is scrolled to the right, or if the terminal is widened while scrolled, `offsetX` is clamped here during `render`. However, if the user presses `right()` multiple times when `tableWidth <= width` (or beyond `tableWidth - width`), `right()` unconditionally increments `offsetX` and triggers a redundant frame render even though `offsetX` will immediately be clamped back to the same value in `render()`. Clamping or bounding `offsetX` in `right()` (or storing the last `maxOffsetX`) would avoid unnecessary redraws when pressing the right arrow at the horizontal boundary. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
