This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from NuGet/dev
[ReleasePrep][2018.08.06] RI of dev into master
- Loading branch information
Showing
21 changed files
with
452 additions
and
59 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/NuGet.Services.Revalidate/Configuration/ApplicationInsightsConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace NuGet.Services.Revalidate | ||
{ | ||
/// <summary> | ||
/// The configuration needed to query an Application Insights account using | ||
/// the REST endpoints. | ||
/// </summary> | ||
public class ApplicationInsightsConfiguration | ||
{ | ||
/// <summary> | ||
/// The Application Insights account identifier. | ||
/// </summary> | ||
public string AppId { get; set; } | ||
|
||
/// <summary> | ||
/// The API Key used to access the Application Insights account. | ||
/// </summary> | ||
public string ApiKey { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/NuGet.Services.Revalidate/Initialization/PreinstalledPackages.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
|
||
namespace NuGet.Services.Revalidate | ||
{ | ||
public class GalleryService : IGalleryService | ||
{ | ||
private static readonly string GalleryEventsQuery = HttpUtility.UrlPathEncode( | ||
"customMetrics | " + | ||
"where name == \"PackagePush\" or name == \"PackageUnlisted\" or name == \"PackageListed\" | " + | ||
"summarize sum(value)"); | ||
|
||
private readonly HttpClient _httpClient; | ||
private readonly ApplicationInsightsConfiguration _appInsightsConfig; | ||
private readonly ILogger<GalleryService> _logger; | ||
|
||
public GalleryService( | ||
HttpClient httpClient, | ||
ApplicationInsightsConfiguration appInsightsConfig, | ||
ILogger<GalleryService> logger) | ||
{ | ||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
_appInsightsConfig = appInsightsConfig ?? throw new ArgumentNullException(nameof(appInsightsConfig)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task<int> CountEventsInPastHourAsync() | ||
{ | ||
try | ||
{ | ||
using (var request = new HttpRequestMessage()) | ||
{ | ||
request.RequestUri = new Uri($"https://api.applicationinsights.io/v1/apps/{_appInsightsConfig.AppId}/query?timespan=PT1H&query={GalleryEventsQuery}"); | ||
request.Method = HttpMethod.Get; | ||
|
||
request.Headers.Add("x-api-key", _appInsightsConfig.ApiKey); | ||
|
||
using (var response = await _httpClient.SendAsync(request)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var json = await response.Content.ReadAsStringAsync(); | ||
var data = JsonConvert.DeserializeObject<QueryResult>(json); | ||
|
||
if (data?.Tables?.Length != 1 || | ||
data.Tables[0]?.Rows?.Length != 1 || | ||
data.Tables[0].Rows[0]?.Length != 1) | ||
{ | ||
throw new InvalidOperationException("Malformed response content"); | ||
} | ||
|
||
// Get the first row's first column's value. | ||
return data.Tables[0].Rows[0][0]; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(0, e, "Exception thrown when getting the Gallery's package event rate."); | ||
|
||
throw new InvalidOperationException("Exception thrown when getting the Gallery's package event rate.", e); | ||
} | ||
} | ||
|
||
private class QueryResult | ||
{ | ||
public QueryTable[] Tables { get; set; } | ||
} | ||
|
||
private class QueryTable | ||
{ | ||
public int[][] Rows { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace NuGet.Services.Revalidate | ||
{ | ||
public interface IGalleryService | ||
{ | ||
/// <summary> | ||
/// Count the number of gallery events (package pushes, listing, and unlisting) in the past hour. | ||
/// </summary> | ||
/// <returns>The number of gallery events in the past hour.</returns> | ||
Task<int> CountEventsInPastHourAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.