obelix74 commented on code in PR #3385:
URL: https://github.com/apache/polaris/pull/3385#discussion_r2723288669


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/models/MetricsReportConverter.java:
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.polaris.persistence.relational.jdbc.models;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.annotation.Nullable;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.iceberg.metrics.CommitMetricsResult;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.CounterResult;
+import org.apache.iceberg.metrics.ScanMetricsResult;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.TimerResult;
+
+/**
+ * Converter utility class for transforming Iceberg metrics reports into 
persistence model classes.
+ */
+public final class MetricsReportConverter {
+
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+  private MetricsReportConverter() {
+    // Utility class
+  }
+
+  /**
+   * Converts an Iceberg ScanReport to a ModelScanMetricsReport.
+   *
+   * @param scanReport the Iceberg scan report
+   * @param realmId the realm ID for multi-tenancy
+   * @param catalogId the catalog ID
+   * @param catalogName the catalog name
+   * @param namespace the namespace (dot-separated)
+   * @param principalName the principal who initiated the scan (optional)
+   * @param requestId the request ID (optional)
+   * @param otelTraceId OpenTelemetry trace ID (optional)
+   * @param otelSpanId OpenTelemetry span ID (optional)
+   * @return the converted ModelScanMetricsReport
+   */
+  public static ModelScanMetricsReport fromScanReport(
+      ScanReport scanReport,
+      String realmId,
+      String catalogId,
+      String catalogName,
+      String namespace,
+      @Nullable String principalName,
+      @Nullable String requestId,
+      @Nullable String otelTraceId,
+      @Nullable String otelSpanId) {
+
+    String reportId = UUID.randomUUID().toString();
+    long timestampMs = System.currentTimeMillis();
+
+    ScanMetricsResult metrics = scanReport.scanMetrics();
+
+    ImmutableModelScanMetricsReport.Builder builder =
+        ImmutableModelScanMetricsReport.builder()
+            .reportId(reportId)
+            .realmId(realmId)
+            .catalogId(catalogId)
+            .catalogName(catalogName)
+            .namespace(namespace)
+            .tableName(scanReport.tableName())
+            .timestampMs(timestampMs)
+            .principalName(principalName)
+            .requestId(requestId)
+            .otelTraceId(otelTraceId)
+            .otelSpanId(otelSpanId)
+            .snapshotId(scanReport.snapshotId())
+            .schemaId(scanReport.schemaId())
+            .filterExpression(scanReport.filter() != null ? 
scanReport.filter().toString() : null)
+            
.projectedFieldIds(formatIntegerList(scanReport.projectedFieldIds()))
+            
.projectedFieldNames(formatStringList(scanReport.projectedFieldNames()));
+
+    // Extract metrics values
+    if (metrics != null) {
+      builder
+          .resultDataFiles(getCounterValue(metrics.resultDataFiles()))
+          .resultDeleteFiles(getCounterValue(metrics.resultDeleteFiles()))
+          .totalFileSizeBytes(getCounterValue(metrics.totalFileSizeInBytes()))
+          .totalDataManifests(getCounterValue(metrics.totalDataManifests()))
+          
.totalDeleteManifests(getCounterValue(metrics.totalDeleteManifests()))
+          
.scannedDataManifests(getCounterValue(metrics.scannedDataManifests()))
+          
.scannedDeleteManifests(getCounterValue(metrics.scannedDeleteManifests()))
+          
.skippedDataManifests(getCounterValue(metrics.skippedDataManifests()))
+          
.skippedDeleteManifests(getCounterValue(metrics.skippedDeleteManifests()))
+          .skippedDataFiles(getCounterValue(metrics.skippedDataFiles()))
+          .skippedDeleteFiles(getCounterValue(metrics.skippedDeleteFiles()))
+          
.totalPlanningDurationMs(getTimerValueMs(metrics.totalPlanningDuration()))
+          .equalityDeleteFiles(getCounterValue(metrics.equalityDeleteFiles()))
+          
.positionalDeleteFiles(getCounterValue(metrics.positionalDeleteFiles()))
+          .indexedDeleteFiles(getCounterValue(metrics.indexedDeleteFiles()))
+          
.totalDeleteFileSizeBytes(getCounterValue(metrics.totalDeleteFileSizeInBytes()));
+    } else {
+      builder
+          .resultDataFiles(0L)
+          .resultDeleteFiles(0L)
+          .totalFileSizeBytes(0L)
+          .totalDataManifests(0L)
+          .totalDeleteManifests(0L)
+          .scannedDataManifests(0L)
+          .scannedDeleteManifests(0L)
+          .skippedDataManifests(0L)
+          .skippedDeleteManifests(0L)
+          .skippedDataFiles(0L)
+          .skippedDeleteFiles(0L)
+          .totalPlanningDurationMs(0L)
+          .equalityDeleteFiles(0L)
+          .positionalDeleteFiles(0L)
+          .indexedDeleteFiles(0L)
+          .totalDeleteFileSizeBytes(0L);
+    }
+
+    // Store additional metadata as JSON
+    Map<String, String> metadata = scanReport.metadata();
+    if (metadata != null && !metadata.isEmpty()) {
+      builder.metadata(toJson(metadata));
+    }
+
+    return builder.build();
+  }
+
+  /**
+   * Converts an Iceberg CommitReport to a ModelCommitMetricsReport.
+   *
+   * @param commitReport the Iceberg commit report
+   * @param realmId the realm ID for multi-tenancy
+   * @param catalogId the catalog ID
+   * @param catalogName the catalog name
+   * @param namespace the namespace (dot-separated)
+   * @param principalName the principal who initiated the commit (optional)
+   * @param requestId the request ID (optional)
+   * @param otelTraceId OpenTelemetry trace ID (optional)
+   * @param otelSpanId OpenTelemetry span ID (optional)
+   * @return the converted ModelCommitMetricsReport
+   */
+  public static ModelCommitMetricsReport fromCommitReport(
+      CommitReport commitReport,
+      String realmId,
+      String catalogId,
+      String catalogName,
+      String namespace,
+      @Nullable String principalName,
+      @Nullable String requestId,
+      @Nullable String otelTraceId,
+      @Nullable String otelSpanId) {
+
+    String reportId = UUID.randomUUID().toString();
+    long timestampMs = System.currentTimeMillis();
+
+    CommitMetricsResult metrics = commitReport.commitMetrics();

Review Comment:
   I can simplify this by using a helper method or creating an empty 
CommitMetricsResult when null. However, looking at the Iceberg API, 
CommitMetricsResult is an interface without a public no-arg constructor or 
empty instance factory.
      
   Two options:
   1. Keep the null check but extract the metrics extraction into a private 
helper method for readability
   2. Use a null-object pattern with default values (0L for all counters)
   



-- 
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]

Reply via email to