예제 #1
0
        public string GetStats(string query, string body, IAppServerServices services)
        {
            var req = JObject.Parse(body);
            string uuid = null, mid = null, iid = null, prod = null;
            int testid = 0;
            try
            {
                uuid = (string) req["uuid"];
                prod = (string) req["product"];
                mid = (string) req["machineGuid"];
                iid = (string) req["installGuid"];
                testid = (int) req["testid"];
            }
            catch (Exception ex)
            {
                services.Logger.LogInfo("STOpsConsole - GetStats - by UUID extract query parameter exception {0}", ex.Message);
            }

            if (testid > 0 && (string.IsNullOrEmpty(uuid) || string.IsNullOrEmpty(mid) || string.IsNullOrEmpty(iid)))
            {
                services.Logger.LogInfo("STOpsConsole - GetStats - by test id {0}", testid);
            }
            else if (string.IsNullOrEmpty(mid) && string.IsNullOrEmpty(iid)) // If request has only uuid specify, try to get the stat of the latest machine id and installation id.
            {
                services.Logger.LogInfo("STOpsConsole - GetStats - by UUID");
                using (var opsConsoleSvc = new OpsConsoleServiceClient(RouterBindings.Local,RouterAddresses.Local.RequestReply))
                {
                    try
                    {
                        var machInstReq = new MachInstInfoRequest()
                        {
                            uuids = new List<string> {uuid},
                            product = prod,
                            filter = true
                        };
                        var response = opsConsoleSvc.GetMachineInstallInfo(machInstReq);
                        var latest = response.Items.FirstOrDefault()
                                        .machInstInfoList.OrderByDescending(y => DateTime.Parse(y.dateLastSeen))
                                        .First();
                        req.Add(new JProperty("installGuid", latest.instGUID));
                        req.Add(new JProperty("machineGuid", latest.machGUID));
                        body = JsonConvert.SerializeObject(req);
                    }
                    catch (Exception ex)
                    {
                        services.Logger.LogError("STOpsConsole - GetStats:GetMachineInstallInfo - Caught exception: " + ex.Message);
                    }
                }
            }
            else if (string.IsNullOrEmpty(mid) || string.IsNullOrEmpty(iid))
            {
                services.Logger.LogWarn(string.IsNullOrEmpty(mid)
                    ? "STOpsConsole - GetStats:GetMachineInstallInfo - machine id is null or empty"
                    : "STOpsConsole - GetStats:GetMachineInstallInfo - installation id is null or empty");
                return "{}";
            }

            var getStatsRequest = JsonConvert.DeserializeObject<GetStatsRequest>(body);
            getStatsRequest.product = "est";
            var product = new List<string> { "est" };
            if (Permission.IsAllowToGetStats(services.UserContext, product) && Permission.IsUserInScope(services.UserContext, getStatsRequest.uuid)){
                using (var opsConsoleSvc = new OpsConsoleServiceClient(RouterBindings.Local, RouterAddresses.Local.RequestReply))
                {
                    try
                    {
                        var response = opsConsoleSvc.GetStats(getStatsRequest);
                        if (response == null || String.IsNullOrEmpty(response.uuid))
                        {
                            return "{}";
                        }
                        response.firstName = services.UserSearch.GetUserByUuid(response.uuid).FirstName;
                        response.lastName = services.UserSearch.GetUserByUuid(response.uuid).LastName;
                        response.email = services.UserSearch.GetUserByUuid(response.uuid).EmailAddress;
                        return JsonConvert.SerializeObject(response);
                    }
                    catch (Exception ex)
                    {
                        services.Logger.LogError("Error while invoking GetStats: {0}", ex.Message);
                        return "{}";
                    }
                }
            }
            else
            {
                return "{}";
            }
        }
예제 #2
0
        public void DownloadStatData(IDictionary<string, object> context)
        {
            string body = (string)context["AS.RequestBody"];
            string encodedJson = body.Replace("json=", "");
            string json = HttpUtility.UrlDecode(encodedJson);

            // Load the incoming request into the request params object (for easy passing to the half dozen functions that need the parameters)
            var req = JsonConvert.DeserializeObject<GetStatsRequest>(json);
            req.product = "est";
            string fileName = string.Format("SystemReport-{0}-{1}.csv", req.getStatHistoryFor, DateTime.UtcNow.ToString("yyyyMMdd-HHmmss'Z'"));
            context["AS.ResponseContentType"] = "application/vnd.ms-excel";
            var headers = (Dictionary<string, string>)context["AS.ResponseHeaders"];
            headers.Add("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
            headers.Add("Content-Type", "application/vnd.ms-excel");

            var user = ((IAaaUser)context["AS.Services.ThomsonReuters.Eikon.Toolkit.Interfaces.IAaaUser"]);
            //var product = string.IsNullOrEmpty(req.product) ? null : new List<string>() { req.product };
            var product = new List<string> { "est" };
            if (Permission.IsAllowToGetStats(user, product) && Permission.IsUserInScope(user, req.uuid))
            {
                GetStatsResponse statResponse = null;
                req.getAllStats = true;
                using (var opsConsoleSvc = new OpsConsoleServiceClient(RouterBindings.Local, RouterAddresses.Local.RequestReply))
                {
                    try
                    {
                        statResponse = opsConsoleSvc.GetStats(req);
                        if (statResponse != null)
                        {
                            StringBuilder data = new StringBuilder();
                            try
                            {
                                StatItem stat = statResponse.stats.Find(s => s.statName == req.getStatHistoryFor);
                                data.AppendFormat("Name,{0}\n", stat.statName);
                                data.AppendFormat("Display Name,{0}\n", stat.statDisplayName);
                                data.AppendFormat("Unit,{0}\n", stat.unit);
                                data.AppendFormat("Discard Incomming data,{0}\n", stat.discard ? "true" : "false");
                                data.AppendFormat("Hidden in UI,{0}\n", stat.hidden ? "true" : "false");
                            }
                            catch (Exception)
                            {
                                data.AppendLine("Name,N/A");
                                data.AppendFormat("Display Name,{0}\n", req.getStatHistoryFor);
                                data.AppendLine("Unit,N/A");
                                data.AppendLine("Discard Incomming data,N/A");
                                data.AppendLine("Hidden in UI,N/A");
                            }
                            data.AppendLine();
                            data.AppendLine("Timestamp,Value");
                            foreach (var row in statResponse.history.rows)
                            {
                                DateTime dt = DateTime.Parse(row.timeStamp);
                                data.AppendFormat("{0},{1}\n", dt.ToString("u"), row.statVal);
                            }
                            context["AS.ResponseBody"] = data.ToString();
                        }
                        else
                        {
                            context["AS.ResponseBody"] = "null";
                        }
                    }
                    catch (Exception)
                    {
                        context["AS.ResponseBody"] = "Error while invoking GetStats";
                    }
                }
            }
            else
            {
                context["AS.ResponseBody"] = "No permission to get stats";
            }
        }