Skip to content

Commit

Permalink
Refactor the process of updating metric IDs and metric specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
riemanli committed May 31, 2023
1 parent 04b617e commit 3d15510
Show file tree
Hide file tree
Showing 6 changed files with 555 additions and 504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ kt_jvm_library(
"//src/main/kotlin/org/wfanet/measurement/api:api_key_constants",
"//src/main/kotlin/org/wfanet/measurement/api:public_api_version",
"//src/main/kotlin/org/wfanet/measurement/reporting/service/api:encryption_key_pair_store",
"//src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha:metric_spec_builder",
"//src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha:principal_server_interceptor",
"//src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha:proto_conversions",
"//src/main/kotlin/org/wfanet/measurement/reporting/service/api/v2alpha:reporting_principal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import org.wfanet.measurement.internal.reporting.v2.StreamMetricsRequestKt
import org.wfanet.measurement.internal.reporting.v2.StreamReportingSetsRequest
import org.wfanet.measurement.internal.reporting.v2.StreamReportingSetsRequestKt
import org.wfanet.measurement.internal.reporting.v2.TimeInterval as InternalTimeInterval
import org.wfanet.measurement.internal.reporting.v2.metricSpec as internalMetricSpec
import org.wfanet.measurement.internal.reporting.v2.TimeIntervals as InternalTimeIntervals
import org.wfanet.measurement.internal.reporting.v2.metricSpec as internalMetricSpec
import org.wfanet.measurement.internal.reporting.v2.periodicTimeInterval as internalPeriodicTimeInterval
import org.wfanet.measurement.internal.reporting.v2.streamMetricsRequest
import org.wfanet.measurement.internal.reporting.v2.streamReportingSetsRequest
Expand All @@ -50,7 +50,6 @@ import org.wfanet.measurement.internal.reporting.v2.timeIntervals as internalTim
import org.wfanet.measurement.reporting.v2alpha.CreateMetricRequest
import org.wfanet.measurement.reporting.v2alpha.ListMetricsPageToken
import org.wfanet.measurement.reporting.v2alpha.ListReportingSetsPageToken
import org.wfanet.measurement.reporting.v2alpha.Metric
import org.wfanet.measurement.reporting.v2alpha.MetricSpec
import org.wfanet.measurement.reporting.v2alpha.MetricSpecKt
import org.wfanet.measurement.reporting.v2alpha.PeriodicTimeInterval
Expand Down Expand Up @@ -568,18 +567,12 @@ fun ListReportingSetsPageToken.toStreamReportingSetsRequest(): StreamReportingSe
}

/** Converts an [InternalReport.MetricCalculationSpec] to a [Report.MetricCalculationSpec]. */
fun InternalReport.MetricCalculationSpec.toMetricCalculationSpec(
requestIdToMetricMap: Map<String, Metric>,
): Report.MetricCalculationSpec {
fun InternalReport.MetricCalculationSpec.toMetricCalculationSpec(): Report.MetricCalculationSpec {
val source = this
val metricSpecs: List<MetricSpec> =
source.createMetricRequestsList
.map { request -> requestIdToMetricMap.getValue(request.requestId).metricSpec }
.distinct()

return ReportKt.metricCalculationSpec {
displayName = source.details.displayName
this.metricSpecs += metricSpecs
metricSpecs += source.details.metricSpecsList.map(InternalMetricSpec::toMetricSpec)
groupings +=
source.details.groupingsList.map { grouping ->
ReportKt.grouping { predicates += grouping.predicatesList }
Expand All @@ -606,23 +599,6 @@ fun InternalTimeIntervals.toTimeIntervals(): TimeIntervals {
}
}

/**
* Converts a [MetricSpec.DifferentialPrivacyParams] to an
* [InternalMetricSpec.DifferentialPrivacyParams].
*/
fun MetricSpec.DifferentialPrivacyParams.toInternal():
InternalMetricSpec.DifferentialPrivacyParams {
val source = this
return InternalMetricSpecKt.differentialPrivacyParams {
if (source.hasEpsilon()) {
this.epsilon = source.epsilon
}
if (source.hasDelta()) {
this.delta = source.delta
}
}
}

/** Converts a public [TimeIntervals] to an [InternalTimeIntervals]. */
fun TimeIntervals.toInternal(): InternalTimeIntervals {
val source = this
Expand All @@ -641,8 +617,8 @@ fun PeriodicTimeInterval.toInternal(): InternalPeriodicTimeInterval {
}
}

/** Converts an [InternalReport.CreateMetricRequest] to a public [CreateMetricRequest]. */
fun InternalReport.CreateMetricRequest.toCreateMetricRequest(
/** Converts an [InternalReport.ReportingMetric] to a public [CreateMetricRequest]. */
fun InternalReport.ReportingMetric.toCreateMetricRequest(
measurementConsumerKey: MeasurementConsumerKey
): CreateMetricRequest {
val source = this
Expand All @@ -655,10 +631,30 @@ fun InternalReport.CreateMetricRequest.toCreateMetricRequest(
externalIdToApiId(source.details.externalReportingSetId)
)
.toName()
this.timeInterval = source.details.timeInterval.toTimeInterval()
this.metricSpec = source.details.metricSpec.toMetricSpec()
this.filters += source.details.filtersList
timeInterval = source.details.timeInterval.toTimeInterval()
metricSpec = source.details.metricSpec.toMetricSpec()
filters += source.details.filtersList
}
requestId = source.requestId
}
}

/**
* Converts an internal ReportingMetricEntry Map.Entry<Long,
* [InternalReport.ReportingMetricCalculationSpec]> to an [Report.ReportingMetricEntry].
*/
fun Map.Entry<Long, InternalReport.ReportingMetricCalculationSpec>.toReportingMetricEntry(
cmmsMeasurementConsumerId: String
): Report.ReportingMetricEntry {
val source = this

return ReportKt.reportingMetricEntry {
key = ReportingSetKey(cmmsMeasurementConsumerId, externalIdToApiId(source.key)).toName()

value =
ReportKt.reportingMetricCalculationSpec {
metricCalculationSpecs +=
source.value.metricCalculationSpecsList.map { it.toMetricCalculationSpec() }
}
}
}
Loading

0 comments on commit 3d15510

Please sign in to comment.