-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathReportConverter.kt
157 lines (138 loc) · 6.25 KB
/
ReportConverter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2024 The Cross-Media Measurement Authors
//
// Licensed 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 experimental.dp_consistency.src.main.kotlin.tools
import com.google.protobuf.InvalidProtocolBufferException
import com.google.protobuf.util.JsonFormat
import experimental.dp_consistency.src.main.proto.reporting.MeasurementDetailKt
import experimental.dp_consistency.src.main.proto.reporting.ReportSummary
import experimental.dp_consistency.src.main.proto.reporting.measurementDetail
import experimental.dp_consistency.src.main.proto.reporting.reportSummary
import org.wfanet.measurement.reporting.v2alpha.Metric
import org.wfanet.measurement.reporting.v2alpha.Report
import org.wfanet.measurement.reporting.v2alpha.report
data class ReportingSetSummary(val measurementPolicy: String, val dataProviders: List<String>)
data class SetOperationSummary(val isCumulative: Boolean, val setOperation: String)
fun getReportFromJsonString(reportAsJsonString: String): Report {
val report =
try {
val protoBuilder = Report.newBuilder()
JsonFormat.parser().ignoringUnknownFields().merge(reportAsJsonString, protoBuilder)
protoBuilder.build()
} catch (e: InvalidProtocolBufferException) {
Report.getDefaultInstance()
}
return report
}
fun convertJsontoReportSummaries(reportAsJsonString: String): List<ReportSummary> {
val report =
try {
val protoBuilder = Report.newBuilder()
JsonFormat.parser().ignoringUnknownFields().merge(reportAsJsonString, protoBuilder)
protoBuilder.build()
} catch (e: InvalidProtocolBufferException) {
Report.getDefaultInstance()
}
return report.toReportSummaries()
}
fun getMeasurementPolicy(tag: String): String {
when {
"measurement_policy=AMI" in tag -> return "ami"
"measurement_policy=MRC" in tag -> return "mrc"
"measurement_policy=CUSTOM" in tag -> return "custom"
else -> error("Measurement policy not specified in the tag.")
}
}
fun getSetOperation(tag: String): String {
val parts = tag.split(", ")
val setOperationPart = parts.find { it.startsWith("set_operation=") }
return setOperationPart?.let { it.substringAfter("set_operation=") }
?: error("Set operation must be specified.")
}
fun isCumulative(tag: String): Boolean {
return tag.contains("cumulative=true")
}
fun getTargets(tag: String): List<String> {
val parts = tag.split(", ")
val targetPart = parts.find { it.startsWith("target=") }
return targetPart?.let { it.substringAfter("target=").split(",") }
?: error("There must be at least one target.")
}
fun Report.toReportSummaries(): List<ReportSummary> {
require(this.state == Report.State.SUCCEEDED) { "Unsucceeded report is not supported." }
val measurementPoliciesByReportingSet =
this.reportingMetricEntriesList.associate { entry ->
val reportingSet = entry.key
val tag = this.tags.getValue(reportingSet)
reportingSet to ReportingSetSummary(getMeasurementPolicy(tag), getTargets(tag))
}
val metricCalculationSpecs =
this.reportingMetricEntriesList.flatMapTo(mutableSetOf()) {
it.value.metricCalculationSpecsList
}
val setOperationByMetricCalculationSpec =
metricCalculationSpecs.associate { spec ->
val tag = this.tags.getValue(spec)
spec to SetOperationSummary(isCumulative(tag), getSetOperation(tag))
}
val filterGroupByMetricCalculationSpec =
metricCalculationSpecs.associate { spec ->
val tag = this.tags.getValue(spec)
spec to tag.split(", ").find { it.startsWith("grouping=") }
}
val filterGroups = filterGroupByMetricCalculationSpec.values.toSet()
// Groups results by (reporting set x metric calculation spec).
val measurementSets =
this.metricCalculationResultsList.groupBy { Pair(it.metricCalculationSpec, it.reportingSet) }
val reportSummaries = mutableListOf<ReportSummary>()
for (group in filterGroups) {
val reportSummary = reportSummary {
measurementSets.forEach { (key, value) ->
if (filterGroupByMetricCalculationSpec.getValue(key.first) == group) {
measurementDetails += measurementDetail {
measurementPolicy = measurementPoliciesByReportingSet[key.second]!!.measurementPolicy
dataProviders += measurementPoliciesByReportingSet[key.second]!!.dataProviders
isCumulative = setOperationByMetricCalculationSpec[key.first]!!.isCumulative
setOperation = setOperationByMetricCalculationSpec[key.first]!!.setOperation
var measurementList =
value
.flatMap { it.resultAttributesList }
.sortedBy { it.timeInterval.endTime.seconds }
.filter { it.metricResult.hasReach() || it.metricResult.hasReachAndFrequency() }
.map { resultAttribute ->
require(resultAttribute.state == Metric.State.SUCCEEDED) {
"Unsucceeded measurement result is not supported."
}
MeasurementDetailKt.measurementResult {
if (resultAttribute.metricResult.hasReach()) {
reach = resultAttribute.metricResult.reach.value
standardDeviation =
resultAttribute.metricResult.reach.univariateStatistics.standardDeviation
} else {
reach = resultAttribute.metricResult.reachAndFrequency.reach.value
standardDeviation =
resultAttribute.metricResult.reachAndFrequency.reach.univariateStatistics
.standardDeviation
}
metric = resultAttribute.metric
}
}
measurementResults.addAll(measurementList)
}
}
}
}
reportSummaries.add(reportSummary)
}
return reportSummaries
}