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
        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;
            }

            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();
        }
Ejemplo n.º 3
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.º 4
0
        private void SaveUsage(UsageStore store)
        {
            var pathString = storePath.ToString();

            Logger.Trace("SaveUsage: \"{0}\"", pathString);

            try
            {
                var json = SimpleJson.SerializeObject(store);
                storePath.WriteAllText(json, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "SaveUsage Error: \"{0}\"", pathString);
            }
        }
Ejemplo n.º 5
0
        private UsageStore LoadUsage()
        {
            UsageStore result = null;
            string     json   = null;

            if (storePath.FileExists())
            {
                Logger.Trace("LoadUsage: \"{0}\"", storePath);

                try
                {
                    json = storePath.ReadAllText(Encoding.UTF8);
                    if (json != null)
                    {
                        result = SimpleJson.DeserializeObject <UsageStore>(json);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warning(ex, "Error Loading Usage: {0}; Deleting File", storePath);

                    try
                    {
                        storePath.DeleteIfExists();
                    }
                    catch {}
                }
            }

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

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

            return(result);
        }
Ejemplo n.º 6
0
 private Usage GetCurrentUsage(UsageStore usageStore)
 {
     return(usageStore.Model.GetCurrentUsage(ApplicationConfiguration.AssemblyName.Version.ToString(), unityVersion, instanceId));
 }