コード例 #1
0
ファイル: UsageTracker.cs プロジェクト: ccf19881030/Unity-1
        private void SendUsage()
        {
            if (MetricsService == null)
            {
                Logger.Warning("Metrics disabled: no service");
                return;
            }

            if (!Enabled)
            {
                Logger.Trace("Metrics disabled");
                return;
            }

            UsageStore usageStore = null;

            lock (_lock)
            {
                usageStore = usageLoader.Load(userId);
            }

            var currentTimeOffset = DateTimeOffset.UtcNow;

            if (usageStore.LastSubmissionDate.Date == currentTimeOffset.Date)
            {
                Logger.Trace("Already sent today");
                return;
            }

            var extractReports = usageStore.Model.SelectReports(currentTimeOffset.Date);

            if (!extractReports.Any())
            {
                Logger.Trace("No items to send");
                return;
            }

            var username = GetUsername();

            if (!String.IsNullOrEmpty(username))
            {
                extractReports.ForEach(x => x.Dimensions.GitHubUser = username);
            }

            try
            {
                MetricsService.PostUsage(extractReports);
            }
            catch (Exception ex)
            {
                Logger.Warning(@"Error sending usage:""{0}"" Message:""{1}""", ex.GetType(), ex.GetExceptionMessageShort());
                return;
            }

            // if we're here, success!
            lock (_lock)
            {
                usageStore = usageLoader.Load(userId);
                usageStore.LastSubmissionDate = currentTimeOffset;
                usageStore.Model.RemoveReports(currentTimeOffset.Date);
                usageLoader.Save(usageStore);
            }

            // update the repo size for the current report, while we're at it
            CaptureRepoSize();
        }
コード例 #2
0
ファイル: UsageTracker.cs プロジェクト: nicknockname/Unity
        private async Task SendUsage()
        {
            var usageStore = usageLoader.Load(userId);

            if (metricsService == null)
            {
                Logger.Warning("No service, not sending usage");
                return;
            }

            if (usageStore.LastUpdated.Date != DateTimeOffset.UtcNow.Date)
            {
                var currentTimeOffset = DateTimeOffset.UtcNow;
                var beforeDate        = currentTimeOffset.Date;

                var success        = false;
                var extractReports = usageStore.Model.SelectReports(beforeDate);
                if (!extractReports.Any())
                {
                    Logger.Trace("No items to send");
                }
                else
                {
                    if (!Enabled)
                    {
                        Logger.Trace("Metrics disabled");
                        return;
                    }

                    try
                    {
                        await metricsService.PostUsage(extractReports);

                        success = true;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(@"Error Sending Usage Exception Type:""{0}"" Message:""{1}""", ex.GetType().ToString(), ex.GetExceptionMessageShort());
                    }
                }

                if (success)
                {
                    usageStore.Model.RemoveReports(beforeDate);
                    usageStore.LastUpdated = currentTimeOffset;
                    usageLoader.Save(usageStore);
                }
            }
        }