Beispiel #1
0
        public async Task <DbUserReport> UpsertReport(DbUserReport newReport)
        {
            var result = await this._docClient.UpsertDocumentAsync(
                UriFactory.CreateDocumentCollectionUri(
                    _elmDbName,
                    _reportsCollectionName),
                newReport);

            return(JsonConvert.DeserializeObject <DbUserReport>(result.Resource.ToString()));
        }
Beispiel #2
0
        public async Task CalculateReportsForUsers(int staleCutoff, int maxUsersToCalculate)
        {
            var staleDate             = DateTime.UtcNow.AddDays(0 - staleCutoff);
            var usersWithStaleReports = _elmDb.GetUsers(staleDate, maxUsersToCalculate);
            var failedUsers           = new List <DbUser>();

            _log.LogInformation("Users with stale reports: " + usersWithStaleReports.Count());
            foreach (var user in usersWithStaleReports)
            {
                try
                {
                    var userReport   = BuildReportForUser(user.Id);
                    var dbUserReport = new DbUserReport()
                    {
                        CreatedTime = DateTime.UtcNow,
                        Report      = userReport
                    };
                    var savedReport = await _elmDb.UpsertReport(dbUserReport);

                    user.MostRecentReportId     = savedReport.Id;
                    user.MostRecentReportUpdate = savedReport.CreatedTime;
                    await _elmDb.UpsertUserAsync(user);
                }
                catch (Exception ex)
                {
                    _log.LogWarning("Failed when processing user report: " + ex.Message);
                    failedUsers.Add(user);
                }
            }
            if (failedUsers.Any())
            {
                var errorMessage = new StringBuilder("Failed to generate report for 1 or more users, including the following: ");
                for (var i = 0; i < failedUsers.Count && i < 10; i++)
                {
                    errorMessage.Append(failedUsers[i].Id);
                    errorMessage.Append(", ");
                }
                errorMessage.Append(" total failures: ");
                errorMessage.Append(failedUsers.Count);
                _log.LogError(errorMessage.ToString());
            }
            _log.LogInformation("Done calculating reports.");
        }