private void DetectDownloadCountDecreases(DownloadData oldData, DownloadData newData, string id, ref int decreaseCount) { var oldHasId = oldData.TryGetValue(id, out var oldDownloads); if (!oldHasId) { oldDownloads = new DownloadByVersionData(); } var newHasId = newData.TryGetValue(id, out var newDownloads); if (!newHasId) { newDownloads = new DownloadByVersionData(); } var uniqueVersions = new HashSet <string>( oldDownloads.Keys.Concat(newDownloads.Keys), StringComparer.OrdinalIgnoreCase); foreach (var version in uniqueVersions) { var oldHasVersion = oldDownloads.TryGetValue(version, out var oldCount); var newHasVersion = newDownloads.TryGetValue(version, out var newCount); if (newCount < oldCount) { decreaseCount++; // Don't emit too many telemetry events. At a certain point the detail provided by additional events // doesn't help investigation and can overwhelm Application Insights. if (decreaseCount <= _options.Value.MaxDownloadCountDecreases) { _telemetryService.TrackDownloadCountDecrease( id, version, oldHasId, oldHasVersion, oldCount, newHasId, newHasVersion, newCount); } } } }