Skip to content

Commit

Permalink
[#5746] feat(CLI): Support table format output for Audit command (#6503)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Support table format output for Audit command.

### Why are the changes needed?

Fix: #5746 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test.
```bash
gcli metalake  details   --output table  -m demo_metalake --audit  -i
+-----------+--------------------------+-----------+--------------------------+
|  Creator  |       Creation at        | Modifier  |       Modified at        |
+-----------+--------------------------+-----------+--------------------------+
| anonymous | 2024-12-04T07:41:18.512Z | anonymous | 2025-01-14T07:56:25.496Z |
+-----------+--------------------------+-----------+--------------------------+

gcli catalog  details --name Hive_catalog  -i --output table  -m demo_metalake --audit
+-----------+--------------------------+-----------+-----------------------------+
|  Creator  |       Creation at        | Modifier  |         Modified at         |
+-----------+--------------------------+-----------+-----------------------------+
| anonymous | 2024-12-05T01:20:40.512Z | anonymous | 2025-02-21T08:36:50.613886Z |
+-----------+--------------------------+-----------+-----------------------------+

gcli schema  details --name Hive_catalog.default --output table  -m demo_metalake --audit -i
+---------+-------------+----------+-------------+
| Creator | Creation at | Modifier | Modified at |
+---------+-------------+----------+-------------+
| public  | N/A         | N/A      | N/A         |
+---------+-------------+----------+-------------+

gcli table  details --name Hive_catalog.default.test_dates --output table  -m demo_metalake --audit -i
+-----------+----------------------+----------+-------------+
|  Creator  |     Creation at      | Modifier | Modified at |
+-----------+----------------------+----------+-------------+
| panchenxi | 2024-07-24T07:20:52Z | N/A      | N/A         |
+-----------+----------------------+----------+-------------+
```
  • Loading branch information
Abyss-lord authored Feb 25, 2025
1 parent fb3d900 commit 3590393
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,10 @@ public void handle() {}
* @param audit from a class that implements the Auditable interface.
*/
public void displayAuditInfo(Audit audit) {
String auditInfo =
"creator,create_time,modified,modified_time"
+ System.lineSeparator()
+ audit.creator()
+ ","
+ audit.createTime()
+ ","
+ audit.lastModifier()
+ ","
+ audit.lastModifiedTime();
if (audit == null) {
return;
}

printResults(auditInfo);
printResults(audit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

if (result != null) {
displayAuditInfo(result.auditInfo());
}
displayAuditInfo(result.auditInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

if (result != null) {
displayAuditInfo(result.auditInfo());
}
displayAuditInfo(result.auditInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.apache.gravitino.Audit;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;
Expand Down Expand Up @@ -85,6 +86,8 @@ public static void output(Object entity, CommandContext context) {
new TableDetailsTableFormat(context).output((Table) entity);
} else if (entity instanceof Table[]) {
new TableListTableFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditTableFormat(context).output((Audit) entity);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -643,4 +646,31 @@ public String getOutput(Table[] tables) {
return getTableFormat(column);
}
}

/**
* Formats a single {@link Audit} instance into a four-column table display. Displays audit
* details, including creator, create time, modified, and modify time.
*/
static final class AuditTableFormat extends TableFormat<Audit> {
public AuditTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Audit audit) {
Column columnCreator = new Column(context, "creator");
Column columnCreateTime = new Column(context, "creation at");
Column columnModified = new Column(context, "modifier");
Column columnModifyTime = new Column(context, "modified at");

columnCreator.addCell(audit.creator());
columnCreateTime.addCell(audit.createTime() == null ? "N/A" : audit.createTime().toString());
columnModified.addCell(audit.lastModifier() == null ? "N/A" : audit.lastModifier());
columnModifyTime.addCell(
audit.lastModifiedTime() == null ? "N/A" : audit.lastModifiedTime().toString());

return getTableFormat(columnCreator, columnCreateTime, columnModified, columnModifyTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import org.apache.gravitino.Audit;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;
Expand Down Expand Up @@ -373,6 +375,48 @@ void testListTableWithTableFormat() {
output);
}

@Test
void testAuditWithTableFormat() {
CommandContext mockContext = getMockContext();
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
when(mockAudit.createTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));
when(mockAudit.lastModifier()).thenReturn("demo_user");
when(mockAudit.lastModifiedTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));

TableFormat.output(mockAudit, mockContext);

String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+-----------+--------------------------+-----------+--------------------------+\n"
+ "| Creator | Creation at | Modifier | Modified at |\n"
+ "+-----------+--------------------------+-----------+--------------------------+\n"
+ "| demo_user | 2021-01-20T02:51:51.111Z | demo_user | 2021-01-20T02:51:51.111Z |\n"
+ "+-----------+--------------------------+-----------+--------------------------+",
output);
}

@Test
void testAuditWithTableFormatWithNullValues() {
CommandContext mockContext = getMockContext();
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
when(mockAudit.createTime()).thenReturn(null);
when(mockAudit.lastModifier()).thenReturn(null);
when(mockAudit.lastModifiedTime()).thenReturn(null);

TableFormat.output(mockAudit, mockContext);

String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+-----------+-------------+----------+-------------+\n"
+ "| Creator | Creation at | Modifier | Modified at |\n"
+ "+-----------+-------------+----------+-------------+\n"
+ "| demo_user | N/A | N/A | N/A |\n"
+ "+-----------+-------------+----------+-------------+",
output);
}

@Test
void testOutputWithUnsupportType() {
CommandContext mockContext = getMockContext();
Expand Down

0 comments on commit 3590393

Please sign in to comment.