Commit 6d7abad8 authored by Yue Ru Sun's avatar Yue Ru Sun Committed by Commit Bot

Normalize UKM entry to avoid cache duplication

Bug: 944942
Change-Id: I23b7909483f85a704ca02abd99dcbe96306875be
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1559752Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Commit-Queue: Yue Ru Sun <yrsun@chromium.org>
Cr-Commit-Position: refs/heads/master@{#649266}
parent 10f04d6a
......@@ -303,6 +303,19 @@ function populateThreadIds(sources) {
}
}
/**
* Get the string representation of a UKM entry. The array of metrics are sorted
* by name to ensure that two entries containing the same metrics and values in
* different orders have identical string representation to avoid cache
* duplication.
* @param {UkmEntry} entry UKM entry to be stringified.
* @return {string} Normalized string representation of the entry.
*/
function normalizeToString(entry) {
entry.metrics.sort((x, y) => x.name.localeCompare(y.name));
return JSON.stringify(entry);
}
/**
* This function tries to preserve UKM logs around UKM log uploads. There is
* no way of knowing if duplicate entries for a log are actually produced
......@@ -323,11 +336,11 @@ function updateUkmCache(data) {
CachedSources.set(key, mergedSource);
} else {
// Merge distinct entries from the source.
const existingEntries =
new Set(CachedSources.get(key).entries.map(e => JSON.stringify(e)));
for (const entry of source.entries) {
if (!existingEntries.has(JSON.stringify(entry))) {
CachedSources.get(key).entries.push(entry);
const existingEntries = new Set(CachedSources.get(key).entries.map(
cachedEntry => normalizeToString(cachedEntry)));
for (const sourceEntry of source.entries) {
if (!existingEntries.has(normalizeToString(sourceEntry))) {
CachedSources.get(key).entries.push(sourceEntry);
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment