public static async void SendHeartbeat(string reason)
        {
            string jwt    = GetJwt();
            bool   online = await IsOnlineAsync();

            if (online && jwt != null)
            {
                string version = Constants.EditorVersion;

                JsonObject jsonObj = new JsonObject();
                jsonObj.Add("version", SoftwareCoPackage.GetVersion());
                jsonObj.Add("os", SoftwareCoPackage.GetOs());
                jsonObj.Add("pluginId", Constants.PluginId);
                jsonObj.Add("start", SoftwareCoUtil.getNowInSeconds());
                jsonObj.Add("trigger_annotation", reason);
                jsonObj.Add("hostname", SoftwareCoUtil.getHostname());

                string api      = "/data/heartbeat";
                string jsonData = jsonObj.ToString();
                HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync(HttpMethod.Post, api, jsonData, jwt);

                if (!SoftwareHttpManager.IsOk(response))
                {
                    Logger.Warning("Code Time: Unable to send heartbeat");
                }
            }
        }
        public static async Task <string> CreateAnonymousUserAsync(bool online)
        {
            // get the app jwt
            try
            {
                string app_jwt = await GetAppJwtAsync(online);

                if (app_jwt != null && online)
                {
                    string creation_annotation = "NO_SESSION_FILE";
                    string osUsername          = Environment.UserName;
                    string timezone            = "";
                    if (TimeZone.CurrentTimeZone.DaylightName != null &&
                        TimeZone.CurrentTimeZone.DaylightName != TimeZone.CurrentTimeZone.StandardName)
                    {
                        timezone = TimeZone.CurrentTimeZone.DaylightName;
                    }
                    else
                    {
                        timezone = TimeZone.CurrentTimeZone.StandardName;
                    }

                    JsonObject jsonObj = new JsonObject();
                    jsonObj.Add("timezone", timezone);
                    jsonObj.Add("username", osUsername);
                    jsonObj.Add("hostname", SoftwareCoUtil.getHostname());
                    jsonObj.Add("creation_annotation", creation_annotation);

                    string api      = "/data/onboard";
                    string jsonData = jsonObj.ToString();
                    HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync(HttpMethod.Post, api, jsonData, app_jwt);

                    if (SoftwareHttpManager.IsOk(response))
                    {
                        string responseBody = await response.Content.ReadAsStringAsync();

                        IDictionary <string, object> respObj = (IDictionary <string, object>)SimpleJson.DeserializeObject(responseBody, new Dictionary <string, object>());
                        respObj.TryGetValue("jwt", out object jwtObj);
                        string jwt = (jwtObj == null) ? null : Convert.ToString(jwtObj);
                        if (jwt != null)
                        {
                            FileManager.setItem("jwt", jwt);
                            return(jwt);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("CreateAnonymousUserAsync, error: " + ex.Message, ex);
            }


            return(null);
        }
Пример #3
0
        private async Task ValidateAndUpdateCumulativeDataAsync(long session_seconds)
        {
            TimeData td = await TimeDataManager.Instance.UpdateSessionAndFileSecondsAsync(this.project, session_seconds);

            // get the current payloads so we can compare our last cumulative seconds
            PluginData lastKpm = FileManager.GetLastSavedKeystrokeStats();

            if (SoftwareCoUtil.IsNewDay())
            {
                // the days don't match. don't use the editor or session seconds for a different day
                lastKpm = null;
                // clear out data from the previous day
                await WallclockManager.Instance.GetNewDayCheckerAsync();

                if (td != null)
                {
                    td = null;
                    this.project_null_error = "TimeData should be null as its a new day";
                }
            }

            this.workspace_name             = SoftwareCoUtil.workspace_name;
            this.hostname                   = SoftwareCoUtil.getHostname();
            this.cumulative_session_seconds = 60;
            this.cumulative_editor_seconds  = 60;

            if (td != null)
            {
                this.cumulative_editor_seconds  = td.editor_seconds;
                this.cumulative_session_seconds = td.session_seconds;
            }
            else if (lastKpm != null)
            {
                // no time data found, project null error
                this.project_null_error         = "TimeData not found using " + this.project.directory + " for editor and session seconds";
                this.cumulative_editor_seconds  = lastKpm.cumulative_editor_seconds + 60;
                this.cumulative_session_seconds = lastKpm.cumulative_session_seconds + 60;
            }

            if (this.cumulative_editor_seconds < this.cumulative_session_seconds)
            {
                this.cumulative_editor_seconds = cumulative_session_seconds;
            }
        }
Пример #4
0
        private void InitializeData()
        {
            NowTime nowTime = SoftwareCoUtil.GetNowTime();

            this.timestamp       = nowTime.now;
            this.timestamp_local = nowTime.local_now;
            this.pluginId        = Constants.PluginId;
            this.os       = SoftwareCoPackage.GetOs();
            this.hostname = SoftwareCoUtil.getHostname();
            this.version  = SoftwareCoPackage.GetVersion();
            if (TimeZone.CurrentTimeZone.DaylightName != null &&
                TimeZone.CurrentTimeZone.DaylightName != TimeZone.CurrentTimeZone.StandardName)
            {
                this.timezone = TimeZone.CurrentTimeZone.DaylightName;
            }
            else
            {
                this.timezone = TimeZone.CurrentTimeZone.StandardName;
            }
        }