This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #24 from brminnick/Optimize-GetMicrosoftLearnContr…
…ibutors-API Optimize GetMicrosoftLearnContributors API
- Loading branch information
Showing
8 changed files
with
208 additions
and
61 deletions.
There are no files selected for viewing
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
63 changes: 13 additions & 50 deletions
63
AzureAdvocates.Functions/Functions/GetMicrosoftLearnContributors.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
71 changes: 71 additions & 0 deletions
71
AzureAdvocates.Functions/Functions/UpdateMicrosoftLearnContributors.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,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GitHubApiStatus; | ||
using GitHubReadmeWebTrends.Common; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace AzureAdvocates.Functions | ||
{ | ||
class UpdateMicrosoftLearnContributors | ||
{ | ||
readonly BlobStorageService _blobStorageService; | ||
readonly CloudAdvocateService _cloudAdvocateService; | ||
readonly IGitHubApiStatusService _gitHubApiStatusService; | ||
readonly GitHubGraphQLApiService _gitHubGraphQLApiService; | ||
|
||
public UpdateMicrosoftLearnContributors(BlobStorageService blobStorageService, | ||
CloudAdvocateService cloudAdvocateService, | ||
IGitHubApiStatusService gitHubApiStatusService, | ||
GitHubGraphQLApiService gitHubGraphQLApiService) | ||
{ | ||
_blobStorageService = blobStorageService; | ||
_cloudAdvocateService = cloudAdvocateService; | ||
_gitHubApiStatusService = gitHubApiStatusService; | ||
_gitHubGraphQLApiService = gitHubGraphQLApiService; | ||
} | ||
|
||
[FunctionName(nameof(UpdateMicrosoftLearnContributors))] | ||
public async Task Run([TimerTrigger("0 0 */6 * * *")] TimerInfo timer, ILogger log) | ||
{ | ||
log.LogInformation($"{nameof(UpdateMicrosoftLearnContributors)} Started"); | ||
|
||
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2)); | ||
var gitHubApiStatus = await _gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false); | ||
if (gitHubApiStatus.GraphQLApi.RemainingRequestCount < 4000) | ||
{ | ||
log.LogError($"Maximum GitHub API Limit Reached. GitHub API Limit will reset in {gitHubApiStatus.GraphQLApi.RateLimitReset_TimeRemaining.Minutes + 1} minute(s). Try again at {gitHubApiStatus.GraphQLApi.RateLimitReset_DateTime.UtcDateTime} UTC"); | ||
return; | ||
} | ||
|
||
var cloudAdvocateList = new List<CloudAdvocateGitHubUserModel>(); | ||
await foreach (var advocate in _cloudAdvocateService.GetAzureAdvocates().ConfigureAwait(false)) | ||
{ | ||
cloudAdvocateList.Add(advocate); | ||
} | ||
|
||
var microsoftLearnPullRequests = new List<RepositoryPullRequest>(); | ||
await foreach (var pullRequestList in _gitHubGraphQLApiService.GetMicrosoftLearnPullRequests().ConfigureAwait(false)) | ||
{ | ||
microsoftLearnPullRequests.AddRange(pullRequestList); | ||
log.LogInformation($"Added {pullRequestList.Count} Pull Requests from {pullRequestList.FirstOrDefault()?.RepositoryName}"); | ||
} | ||
|
||
var cloudAdvocateContributions = new List<CloudAdvocateGitHubContributorModel>(); | ||
foreach (var cloudAdvocate in cloudAdvocateList) | ||
{ | ||
var cloudAdvocateContributorModel = new CloudAdvocateGitHubContributorModel(microsoftLearnPullRequests.Where(x => x.Author.Equals(cloudAdvocate.GitHubUserName, StringComparison.OrdinalIgnoreCase)), cloudAdvocate); | ||
|
||
cloudAdvocateContributions.Add(cloudAdvocateContributorModel); | ||
|
||
log.LogInformation($"Added {cloudAdvocateContributorModel.PullRequests.Count} Pull Requests for {cloudAdvocate.FullName}"); | ||
} | ||
|
||
var blobName = $"Contributions_{DateTime.UtcNow:o}.json"; | ||
await _blobStorageService.UploadCloudAdvocateMicrosoftLearnContributions(cloudAdvocateContributions, blobName).ConfigureAwait(false); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,26 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GitHubReadmeWebTrends.Common; | ||
using Newtonsoft.Json; | ||
|
||
namespace GitHubReadmeWebTrends.Functions | ||
namespace AzureAdvocates.Functions | ||
{ | ||
class GitHubContributorModel : CloudAdvocateGitHubUserModel | ||
class CloudAdvocateGitHubContributorModel : CloudAdvocateGitHubUserModel | ||
{ | ||
public GitHubContributorModel(in IEnumerable<RepositoryPullRequest> pullReuests, CloudAdvocateGitHubUserModel cloudAdvocateGitHubUserModel) | ||
: this(pullReuests, cloudAdvocateGitHubUserModel.FullName, cloudAdvocateGitHubUserModel.GitHubUserName, cloudAdvocateGitHubUserModel.MicrosoftAlias, cloudAdvocateGitHubUserModel.MicrosoftTeam) | ||
public CloudAdvocateGitHubContributorModel(IEnumerable<RepositoryPullRequest> pullRequests, CloudAdvocateGitHubUserModel cloudAdvocateGitHubUserModel) | ||
: this(pullRequests, cloudAdvocateGitHubUserModel.FullName, cloudAdvocateGitHubUserModel.GitHubUserName, cloudAdvocateGitHubUserModel.MicrosoftAlias, cloudAdvocateGitHubUserModel.MicrosoftTeam) | ||
{ | ||
|
||
} | ||
|
||
public GitHubContributorModel(in IEnumerable<RepositoryPullRequest> pullReuests, in string fullName, in string gitHubUserName, in string microsoftAlias, in string microsoftTeam) | ||
[JsonConstructor] | ||
public CloudAdvocateGitHubContributorModel(IEnumerable<RepositoryPullRequest> pullRequests, string fullName, string gitHubUserName, string microsoftAlias, string microsoftTeam) | ||
: base(fullName, gitHubUserName, microsoftAlias, microsoftTeam) | ||
{ | ||
PullRequests = pullReuests.ToList(); | ||
PullRequests = pullRequests.ToList(); ; | ||
} | ||
|
||
[JsonProperty("pullRequests")] | ||
public IReadOnlyList<RepositoryPullRequest> PullRequests { get; } | ||
} | ||
} |
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,63 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
using Microsoft.WindowsAzure.Storage.Blob; | ||
using Newtonsoft.Json; | ||
|
||
namespace AzureAdvocates.Functions | ||
{ | ||
class BlobStorageService | ||
{ | ||
const string _microsoftLearnContributionsContainerName = "cloudadvocatemicrosoftlearncontributions"; | ||
readonly CloudBlobClient _blobClient; | ||
|
||
public BlobStorageService(CloudBlobClient cloudBlobClient) => _blobClient = cloudBlobClient; | ||
|
||
public Task UploadCloudAdvocateMicrosoftLearnContributions(IEnumerable<CloudAdvocateGitHubContributorModel> azureDataCenterIpRangeModel, string blobName) | ||
{ | ||
var container = GetBlobContainer(_microsoftLearnContributionsContainerName); | ||
var blob = container.GetBlockBlobReference(blobName); | ||
|
||
return blob.UploadTextAsync(JsonConvert.SerializeObject(azureDataCenterIpRangeModel)); | ||
} | ||
|
||
public async Task<IReadOnlyList<CloudAdvocateGitHubContributorModel>> GetCloudAdvocateMicrosoftLearnContributors() | ||
{ | ||
var blobList = new List<CloudBlockBlob>(); | ||
await foreach (var blob in GetBlobs<CloudBlockBlob>(_microsoftLearnContributionsContainerName).ConfigureAwait(false)) | ||
{ | ||
blobList.Add(blob); | ||
} | ||
|
||
var gitHubContributorListBlob = blobList.OrderByDescending(x => x.Properties.Created).First(); | ||
var serializedGitHubContributorList = await gitHubContributorListBlob.DownloadTextAsync().ConfigureAwait(false); | ||
|
||
return JsonConvert.DeserializeObject<IReadOnlyList<CloudAdvocateGitHubContributorModel>>(serializedGitHubContributorList) ?? throw new NullReferenceException(); | ||
} | ||
|
||
async IAsyncEnumerable<T> GetBlobs<T>(string containerName, string prefix = "", int? maxresultsPerQuery = null, BlobListingDetails blobListingDetails = BlobListingDetails.None) where T : ICloudBlob | ||
{ | ||
var blobContainer = GetBlobContainer(containerName); | ||
|
||
BlobContinuationToken? continuationToken = null; | ||
|
||
do | ||
{ | ||
var response = await blobContainer.ListBlobsSegmentedAsync(prefix, true, blobListingDetails, maxresultsPerQuery, continuationToken, null, null).ConfigureAwait(false); | ||
continuationToken = response?.ContinuationToken; | ||
|
||
var blobListFromResponse = response?.Results?.OfType<T>() ?? Enumerable.Empty<T>(); | ||
|
||
foreach (var blob in blobListFromResponse) | ||
{ | ||
yield return blob; | ||
} | ||
|
||
} while (continuationToken != null); | ||
|
||
} | ||
|
||
CloudBlobContainer GetBlobContainer(string containerName) => _blobClient.GetContainerReference(containerName); | ||
} | ||
} |
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