From 6eab893a77f95ca121122eb56901676eb4cf0626 Mon Sep 17 00:00:00 2001 From: Rieman Li Date: Thu, 25 May 2023 20:53:23 +0000 Subject: [PATCH] Add getReport. --- .../service/api/v2alpha/ReportsService.kt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha/ReportsService.kt b/src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha/ReportsService.kt index 5dbd8a11a33..193ace569fb 100644 --- a/src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha/ReportsService.kt +++ b/src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha/ReportsService.kt @@ -40,6 +40,7 @@ import org.wfanet.measurement.internal.reporting.v2.report as internalReport import org.wfanet.measurement.reporting.v2alpha.BatchCreateMetricsRequest import org.wfanet.measurement.reporting.v2alpha.CreateMetricRequest import org.wfanet.measurement.reporting.v2alpha.CreateReportRequest +import org.wfanet.measurement.reporting.v2alpha.GetReportRequest import org.wfanet.measurement.reporting.v2alpha.Metric import org.wfanet.measurement.reporting.v2alpha.MetricsGrpcKt.MetricsCoroutineStub import org.wfanet.measurement.reporting.v2alpha.Report @@ -73,6 +74,55 @@ class ReportsService( private val metricSpecBuilder = MetricSpecBuilder(metricSpecConfig) + override suspend fun getReport(request: GetReportRequest): Report { + val reportKey = + grpcRequireNotNull(ReportKey.fromName(request.name)) { + "Report name is either unspecified or invalid" + } + + val principal: ReportingPrincipal = principalFromCurrentContext + when (principal) { + is MeasurementConsumerPrincipal -> { + if (reportKey.cmmsMeasurementConsumerId != principal.resourceKey.measurementConsumerId) { + failGrpc(Status.PERMISSION_DENIED) { + "Cannot get Report belonging to other MeasurementConsumers." + } + } + } + } + + val internalReport = + try { + internalReportsStub.getReport( + internalGetReportRequest { + cmmsMeasurementConsumerId = reportKey.cmmsMeasurementConsumerId + externalReportId = apiIdToExternalId(reportKey.reportId) + } + ) + } catch (e: StatusException) { + throw Exception("Unable to get the report from the reporting database.", e) + } + + // Create metrics. + val metricNames: List = + internalReport.reportingMetricEntriesMap.flatMap { (_, reportingMetricCalculationSpec) -> + reportingMetricCalculationSpec.metricCalculationSpecsList.flatMap { metricCalculationSpec -> + metricCalculationSpec.reportingMetricsList.map { reportingMetric -> + MetricKey( + principal.resourceKey.measurementConsumerId, + externalIdToApiId(reportingMetric.externalMetricId) + ) + .toName() + } + } + } + val metrics: List = + batchGetMetrics(principal.resourceKey.toName(), principal.config.apiKey, metricNames) + + // Convert the internal report to public and return. + return convertInternalReportToPublic(internalReport, metrics) + } + override suspend fun createReport(request: CreateReportRequest): Report { grpcRequireNotNull(MeasurementConsumerKey.fromName(request.parent)) { "Parent is either unspecified or invalid."