Ejemplo n.º 1
0
        public void Send(DateTime now)
        {
            var userSettings = Properties.Settings.Default.UserId;

            if (userSettings == null || userSettings.Trim() == string.Empty)
            {
                Logger.Error("UserId is null!!!!");
                return;
            }
            var userId = new Guid(userSettings);

            var usage = new UserUsage
            {
                Start                = _startTime,
                AppNames             = _appNames.Keys.ToArray(),
                AppUsedNameHashCodes = _appsUsed.ToArray(),
                AppUsedSeconds       = _appUsedTimes.ToArray(),
                UserId               = userId
            };

            _transporter.SendAsync(usage);

            Logger.DebugFormat(
                "Send - [_appNames:{0}] [_appsUsed : {1}] [_appUsedTimes: {2}] [_appUsedTimes.Sum(): {3}]",
                _appNames.Count, _appsUsed.Count, _appUsedTimes.Count, _appUsedTimes.Sum());

            Logger.LogUsage(usage);

            Start(now);
        }
Ejemplo n.º 2
0
        public static void LogUsage(UserUsage logUsage)
        {
            var mappings = new Dictionary <uint, string>();

            foreach (string app in logUsage.AppNames)
            {
                mappings[StringUtils.HashString(app)] = app;
            }

            using (FileStream fileStream = File.Open("compare-to-html.csv", FileMode.Append))
                using (var writer = new StreamWriter(fileStream))
                {
                    writer.Write(logUsage.Start.Ticks.ToString(CultureInfo.InvariantCulture));
                    writer.Write(",");
                    writer.Write(logUsage.Start.ToString(CultureInfo.InvariantCulture).Replace('"', '-'));
                    writer.Write(',');
                    writer.Write("AppName,Seconds");
                    writer.WriteLine();

                    for (int i = 0; i < logUsage.AppUsedNameHashCodes.Length; i++)
                    {
                        var hashCode = logUsage.AppUsedNameHashCodes[i];
                        var seconds  = logUsage.AppUsedSeconds[i];

                        writer.Write(",,,");
                        writer.Write(mappings[hashCode]);
                        writer.Write(',');
                        writer.Write(seconds);
                        writer.WriteLine();
                    }
                }
        }
Ejemplo n.º 3
0
 public void SendAsync(UserUsage usage)
 {
     lock (lockObj)
     {
         queue.Enqueue(usage);
     }
 }
Ejemplo n.º 4
0
 public void SendAsync(UserUsage usage)
 {
     lock (lockObj)
     {
         queue.Enqueue(usage);
     }
 }
Ejemplo n.º 5
0
        public static void LogUsage(UserUsage logUsage)
        {
            var mappings = new Dictionary<uint, string>();

            foreach (string app in logUsage.AppNames)
            {
                mappings[StringUtils.HashString(app)] = app;
            }

            using (FileStream fileStream = File.Open("compare-to-html.csv", FileMode.Append))
            using (var writer = new StreamWriter(fileStream))
            {
                writer.Write(logUsage.Start.Ticks.ToString(CultureInfo.InvariantCulture));
                writer.Write(",");
                writer.Write(logUsage.Start.ToString(CultureInfo.InvariantCulture).Replace('"', '-'));
                writer.Write(',');
                writer.Write("AppName,Seconds");
                writer.WriteLine();

                for (int i = 0; i < logUsage.AppUsedNameHashCodes.Length; i++)
                {
                    var hashCode = logUsage.AppUsedNameHashCodes[i];
                    var seconds = logUsage.AppUsedSeconds[i];

                    writer.Write(",,,");
                    writer.Write(mappings[hashCode]);
                    writer.Write(',');
                    writer.Write(seconds);
                    writer.WriteLine();
                }
            }
        }
Ejemplo n.º 6
0
        private static bool TrySend(UserUsage usage)
        {
            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(new Uri(Config.Urls), "api/data"));
                httpWebRequest.ContentType = "text/json";
                httpWebRequest.Method      = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = GetJson(usage.Start, usage.UserId, usage.AppNames, usage.AppUsedNameHashCodes,
                                          usage.AppUsedSeconds);

                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    if (httpResponse.StatusCode != HttpStatusCode.OK &&
                        httpResponse.StatusCode != HttpStatusCode.NoContent)
                    {
                        Logger.ErrorFormat("Response code was no 200 or 204: {0}", httpResponse.StatusCode);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occured during SendAsync", ex);

                return(false);
            }

            return(true);
        }
Ejemplo n.º 7
0
        public void Send(DateTime now)
        {
            var userSettings = Properties.Settings.Default.UserId;
            if (userSettings == null || userSettings.Trim() == string.Empty)
            {
                Logger.Error("UserId is null!!!!");
                return;
            }
            var userId = new Guid(userSettings);

            var usage = new UserUsage
                            {
                                Start = _startTime,
                                AppNames = _appNames.Keys.ToArray(),
                                AppUsedNameHashCodes = _appsUsed.ToArray(),
                                AppUsedSeconds = _appUsedTimes.ToArray(),
                                UserId = userId
                            };

            _transporter.SendAsync(usage);

            Logger.DebugFormat(
                "Send - [_appNames:{0}] [_appsUsed : {1}] [_appUsedTimes: {2}] [_appUsedTimes.Sum(): {3}]",
                _appNames.Count, _appsUsed.Count, _appUsedTimes.Count, _appUsedTimes.Sum());

            Logger.LogUsage(usage);

            Start(now);
        }
Ejemplo n.º 8
0
        private void Retry()
        {
            bool success = TrySend(retry);
            if (success)
            {
                retry = null;
                numRetries = 0;
            }
            else
            {
                numRetries++;
                if (numRetries > 5)
                {
                    retry = null;
                    numRetries = 0;

                    Logger.Error("Failed to send after 5 times!");
                }
            }
        }
Ejemplo n.º 9
0
        private void Retry()
        {
            bool success = TrySend(retry);

            if (success)
            {
                retry      = null;
                numRetries = 0;
            }
            else
            {
                numRetries++;
                if (numRetries > 5)
                {
                    retry      = null;
                    numRetries = 0;

                    Logger.Error("Failed to send after 5 times!");
                }
            }
        }
Ejemplo n.º 10
0
        private void Send()
        {
            UserUsage firstOne = null;

            lock (lockObj)
            {
                if (queue.Count > 0)
                {
                    firstOne = queue.Dequeue();
                }
            }

            if (firstOne != null)
            {
                bool success = TrySend(firstOne);
                if (!success)
                {
                    retry = firstOne;
                }
            }
        }
Ejemplo n.º 11
0
        private void Send()
        {
            UserUsage firstOne = null;
            lock (lockObj)
            {
                if (queue.Count > 0)
                {
                    firstOne = queue.Dequeue();
                }
            }

            if (firstOne != null)
            {
                bool success = TrySend(firstOne);
                if (!success)
                {
                    retry = firstOne;
                }
            }
        }
Ejemplo n.º 12
0
        private static bool TrySend(UserUsage usage)
        {
            // Log to Web Service/Azure/etc

            return true;
        }
Ejemplo n.º 13
0
 public static void LogUsage(UserUsage logUsage)
 {
 }
Ejemplo n.º 14
0
        private static bool TrySend(UserUsage usage)
        {
            // Log to Web Service/Azure/etc

            return(true);
        }
Ejemplo n.º 15
0
 public static void LogUsage(UserUsage logUsage)
 {
 }