public DataFetchCompleteMessage(string userId, string platformId, PlatformDataFetchResult result, string syncLogId) { UserId = userId; PlatformId = platformId; Result = result; SyncLogId = syncLogId; }
private async Task <string> HandleFetchDataResult(string userId, PlatformDataFetchResult result, IAsyncDocumentSession session) { return(await Task.FromResult(userId)); //var platformData = await _connectionManager.AddPlatformData(userId, result.NumberOfGigs, result.PeriodStart, // result.PeriodEnd, result.Ratings, result.AverageRating, result.Reviews, result.RawData, session); //var user = await session.LoadAsync<User>(userId); //var platformConnection = user.PlatformConnections.Single(pc => pc.Type == type); //platformConnection.MarkAsDataFetchSuccessful(); //return platformData.Id; }
protected async Task CompleteDataFetch(string userId, string platformId, PlatformDataFetchResult fetchResult, string syncLogId, CancellationToken cancellationToken = default) { await _bus.SendLocal(new DataFetchCompleteMessage(userId, platformId, fetchResult, syncLogId)); }
public new async Task <OAuthPlatformConnectionInfo> StartDataFetch(string userId, string platformId, OAuthPlatformConnectionInfo connectionInfo, PlatformConnection platformConnection, DataSyncLog syncLog, CancellationToken cancellationToken = default) { using var _ = Logger.BeginPropertyScope((LoggerPropertyNames.DataSyncLogId, syncLog.ExternalId)); Logger.LogTrace($"Will start data fetch from Freelancer for user with id {userId}"); if (connectionInfo.Token.HasExpired()) { Logger.LogInformation($"Token has expired, will try to refresh it"); try { var newToken = await _freelancerAuthenticator.RefreshToken(new OAuthAccessToken { AccessToken = connectionInfo.Token.AccessToken, RefreshToken = connectionInfo.Token.RefreshToken, ExpiresIn = connectionInfo.Token.ExpiresInSeconds }); connectionInfo.Token = new Token(newToken); } catch (UnauthorizedAccessException) { Logger.LogInformation( "The consent to access Freelancer seems to have been revoked. Will act accordingly"); return(await HandleUnauthorized(userId, platformId, syncLog.Id)); } } _apiClient.SetAccessToken(connectionInfo.Token.AccessToken); string rawReviews; ReviewApiResult freelancerReviews; string rawUserProfile; UserInfoApiResult userProfile; try { (userProfile, rawUserProfile) = await _apiClient.GetUserProfile(); (freelancerReviews, rawReviews) = await _apiClient.GetReviews(userProfile.Result.Id); } catch (UnauthorizedAccessException) { Logger.LogInformation( "The consent to access Freelancer seems to have been revoked. Will act accordingly"); return(await HandleUnauthorized(userId, platformId, syncLog.Id)); } var(ratings, reviews) = GetRatingsAndReviews(freelancerReviews); var raw = $"{{data: [{rawUserProfile}, {rawReviews}]}}"; var numberOfCompletedJobs = userProfile.Result.Reputation.EntireHistory.Complete; var averageRating = numberOfCompletedJobs == 0 ? null : new RatingDataFetchResult(userProfile.Result.Reputation.EntireHistory.Overall); //achievements var achievements = new List <AchievementFetchResult>(); var qualifications = userProfile.Result.Qualifications; if (qualifications != null) { foreach (var qualification in qualifications) { var score = new AchievementScoreFetchResult( qualification.ScorePercentage.ToString(CultureInfo.InvariantCulture), "percent"); var achievement = new AchievementFetchResult($"freelancer_qualification_{qualification.Id}", qualification.Name, "qualification", PlatformAchievementType.QualificationAssessment, qualification.Description, $"https://www.freelancer.com{qualification.IconUrl}", score); achievements.Add(achievement); } } var badges = userProfile.Result.Badges; if (badges != null) { foreach (var badge in badges) { var achievement = new AchievementFetchResult($"freelancer_badge_{badge.Id}", badge.Name, "badge", PlatformAchievementType.Badge, badge.Description, $"https://www.freelancer.com{badge.IconUrl}", null); achievements.Add(achievement); } } var registrationDate = DateTimeOffset.FromUnixTimeSeconds(userProfile.Result.RegistrationDate); var result = new PlatformDataFetchResult(numberOfCompletedJobs, registrationDate, DateTimeOffset.UtcNow, averageRating, ratings, reviews, achievements, raw); await CompleteDataFetch(userId, platformId, result, syncLog.Id); Logger.LogTrace($"Freelancer data fetch completed for user with id {userId}"); return(connectionInfo); }
public async Task CompleteDataFetching(string userId, string platformId, PlatformDataFetchResult dataFetchResult, string syncLogId, CancellationToken cancellationToken = default) { await CompleteDataFetch(userId, platformId, dataFetchResult, syncLogId, cancellationToken); }
public async Task Handle(PlatformUserUpdateDataMessage message) { using var _ = _logger.BeginPropertyScope((LoggerPropertyNames.GigPlatformApiRequestId, message.RequestId), ("ResultType", message.ResultType)); _logger.LogInformation($"Got data fetch result for request id {message.RequestId}"); string userId = null; string platformId = null; string syncLogId = null; try { (userId, platformId, syncLogId) = await _intermittentDataManager.GetRequestData(message.RequestId); } catch (CacheDataNotFoundException) { _logger.LogInformation($"Could not find data in cache for requestId: {message.RequestId}."); var headers = MessageContext.Current.Headers; var retryCount = 0; if (!headers.ContainsKey("op-retry-count") || (int.TryParse(headers["op-retry-count"], out retryCount) && retryCount < 5)) { retryCount++; var additionalHeader = new Dictionary <string, string>() { { "op-retry-count", retryCount.ToString() } }; await _bus.DeferLocal(TimeSpan.FromSeconds(30), message, additionalHeader); return; } _logger.LogError($"Could not find request data for requestId: {message.RequestId}. Will ignore message."); throw; } if (message.PlatformData != null) { var numberOfGigs = message.PlatformData.Interactions.Count; var periodStart = message.PlatformData.Interactions.Min(i => i.Period?.Start); var periodEnd = message.PlatformData.Interactions.Max(i => i.Period?.End); var ratings = new List <KeyValuePair <string, GigDataCommon.Library.Models.Rating> >(); if (message.PlatformData.Interactions != null) { foreach (var interaction in message.PlatformData.Interactions) { foreach (var rating in interaction.Outcome.Ratings) { ratings.Add(new KeyValuePair <string, GigDataCommon.Library.Models.Rating>(interaction.Id, rating)); } } } var mappedRatings = new List <RatingDataFetchResult>(); var interactionIdsToReviewIdentifiers = new Dictionary <string, IList <Guid> >(); foreach (var rating in ratings) { var mappedRating = new RatingDataFetchResult(rating.Value.Value); mappedRatings.Add(mappedRating); var ratingKey = rating.Key; if (string.IsNullOrEmpty(ratingKey)) { ratingKey = Guid.NewGuid().ToString(); } if (!interactionIdsToReviewIdentifiers.ContainsKey(ratingKey)) { interactionIdsToReviewIdentifiers.Add(ratingKey, new List <Guid>()); } interactionIdsToReviewIdentifiers[ratingKey].Add(mappedRating.Identifier); } RatingDataFetchResult averageRating = null; if (mappedRatings.Count > 0) { var averageRatingValue = mappedRatings.Average(r => r.Value); averageRating = new RatingDataFetchResult(averageRatingValue); } var mappedReviews = new List <ReviewDataFetchResult>(); if (message.PlatformData.Interactions != null) { foreach (var interaction in message.PlatformData.Interactions) { var interactionId = interaction.Id; if (string.IsNullOrEmpty(interactionId)) { interactionId = Guid.NewGuid().ToString(); } if (interaction.Outcome.Review == null) { continue; } Guid?ratingIdentifier = null; if (interactionIdsToReviewIdentifiers.ContainsKey(interactionId) && interactionIdsToReviewIdentifiers[interactionId].Count > 0) { ratingIdentifier = interactionIdsToReviewIdentifiers[interactionId][0]; } var clientName = interaction.Client?.Name; var clientAvatarUri = interaction.Client?.PhotoUri; var mappedReview = new ReviewDataFetchResult($"{platformId}_{interactionId}", interaction.Period.End, ratingIdentifier, interaction.Outcome.Review.Title, interaction.Outcome.Review.Text, clientName, clientAvatarUri); mappedReviews.Add(mappedReview); } } var mappedAchievements = new List <AchievementFetchResult>(); if (message.PlatformData.Achievements != null) { foreach (var achievement in message.PlatformData.Achievements) { var achievementId = achievement.Id; if (string.IsNullOrEmpty(achievementId)) { achievementId = Guid.NewGuid().ToString(); } AchievementScoreFetchResult achievementScore = null; if (achievement.Score != null) { achievementScore = new AchievementScoreFetchResult(achievement.Score.Value.ToString(CultureInfo.InvariantCulture), achievement.Score.Label); } Common.PlatformAchievementType achievementType = PlatformAchievementType.Badge; switch (achievement.Type) { case GigDataCommon.Library.Models.PlatformAchievementType.Badge: achievementType = PlatformAchievementType.Badge; break; case GigDataCommon.Library.Models.PlatformAchievementType.QualificationAssessment: achievementType = PlatformAchievementType.QualificationAssessment; break; default: throw new ArgumentException($"Unknown platform achievement type {achievement.Type}"); } var mappedAchievement = new AchievementFetchResult(achievementId, achievement.Name, achievement.Type.ToString(), achievementType, achievement.Description, achievement.BadgeIconUri, achievementScore); mappedAchievements.Add(mappedAchievement); } } var platformDataFetchResult = new PlatformDataFetchResult( numberOfGigs, periodStart, periodEnd, averageRating, mappedRatings, mappedReviews, mappedAchievements, message.PlatformData.RawData); await _gigPlatformDataFetcher.CompleteDataFetching(userId, platformId, platformDataFetchResult, syncLogId); } else { if (message.ResultType == PlatformDataUpdateResultType.Succeess || message.ResultType == PlatformDataUpdateResultType.MalformedDataResponse) { _logger.LogInformation("Got result type {ResultType}. Will complete data fetching.", message.ResultType); await _gigPlatformDataFetcher.CompleteDataFetching(userId, platformId, null, syncLogId); } else if (message.ResultType == PlatformDataUpdateResultType.UserNotFound) { _logger.LogInformation("Got result type {ResultType}. User was not found. Will complete data fetching and signal that connection should be removed.", message.ResultType); //we should remove the connection await _gigPlatformDataFetcher.CompleteDataFetchingWithConnectionRemoved(userId, platformId, PlatformConnectionDeleteReason.UserDidNotExist, syncLogId); } else { _logger.LogInformation("Got result type {ResultType}. Will schdule new data fetch.", message.ResultType); //communication error, we should enqueue a new request to try to retrieve the data again. var fetchMessage = new FetchDataForPlatformConnectionMessage(userId, platformId, PlatformIntegrationType.GigDataPlatformIntegration); await _bus.DeferLocal(new TimeSpan(0, 0, 30), fetchMessage); } } }
private async Task <(string PlatformDataId, PlatformConnection platformConnection)> HandleFetchDataResult(string userId, string platformId, PlatformDataFetchResult result, IAsyncDocumentSession session, CancellationToken cancellationToken) { var user = await session.LoadAsync <User>(userId, cancellationToken); var platformConnection = user.PlatformConnections.SingleOrDefault(pc => pc.PlatformId == platformId); if (platformConnection == null) { _logger.LogInformation( "No platform connection for platform {platformId} exists for user with id {userId}. Will return null.", platformId, user.Id); //platform connection has been removed between the time we initiated the data fetch and now. return(null, null); } var platformData = await _platformDataManager.AddPlatformData(userId, platformId, result.NumberOfGigs, result.PeriodStart, result.PeriodEnd, result.Ratings, result.AverageRating, result.Reviews, result.Achievements, result.RawData, session, cancellationToken); platformConnection.MarkAsDataFetchSuccessful(); return(platformData.Id, platformConnection); }
public ConnectionDataMessage(string userId, PlatformDataFetchResult result) { UserId = userId; Result = result; }