Ejemplo n.º 1
0
        public UsageStore Load(string userId)
        {
            UsageStore result = null;
            string     json   = null;

            if (path.FileExists())
            {
                try
                {
                    json   = path.ReadAllText(Encoding.UTF8);
                    result = json?.FromJson <UsageStore>(lowerCase: true);
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.Warning(ex, "Error Loading Usage: {0}; Deleting File", path);
                    try
                    {
                        path.DeleteIfExists();
                    }
                    catch { }
                }
            }

            if (result == null)
            {
                result = new UsageStore();
            }

            if (String.IsNullOrEmpty(result.Model.Guid))
            {
                result.Model.Guid = userId;
            }

            return(result);
        }
Ejemplo n.º 2
0
 public void Save(UsageStore store)
 {
     try
     {
         var json = store.ToJson(lowerCase: true);
         path.WriteAllText(json, Encoding.UTF8);
     }
     catch (Exception ex)
     {
         LogHelper.Instance.Error(ex, "SaveUsage Error: \"{0}\"", path);
     }
 }
Ejemplo n.º 3
0
        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();
        }