Пример #1
0
        public async Task <IActionResult> GetDashletData([FromBody] DashletViewModel dashlet)
        {
            var response = await dashboardService.GetDataFromApiPost(dashlet);

            var data = await dashboardService.DataRetrievalResponse(response, dashlet);

            return(Json(data));
        }
Пример #2
0
        public async Task <string> DataRetrievalResponse(string response, DashletViewModel dashlet)
        {
            string root = String.Empty;

            if (dashlet.PathRoot == "")
            {
                response = $"{{'result' : {response} }}";
                root     = "result";
            }
            else
            {
                root = dashlet.PathRoot;
            }

            JObject obj   = JObject.Parse(response);
            JArray  array = new JArray();

            foreach (JObject item in obj[root])
            {
                bool hasValue    = false;
                var  curentValue = item.SelectToken(dashlet.PathValue);

                foreach (var ar in array)
                {
                    var tempValue = string.IsNullOrWhiteSpace((string)curentValue) ? "no info" : curentValue;
                    if ((string)ar[dashlet.DataY] == (string)tempValue)
                    {
                        var tempCount = ar.Value <int>(dashlet.DataX);
                        tempCount++;
                        ar[dashlet.DataX] = tempCount;
                        hasValue          = true;
                        continue;
                    }
                }
                if (hasValue)
                {
                    continue;
                }
                else
                {
                    JToken token = JToken.Parse(string.Format("{{\"{0}\":\"{1}\",\"{2}\":\"{3}\"}}", dashlet.DataY, string.IsNullOrWhiteSpace((string)curentValue) ? "no info" : curentValue, dashlet.DataX, 0));
                    array.Add(token);
                }
            }

            string chartData = JsonConvert.SerializeObject(array);

            return(chartData);
        }
Пример #3
0
        public async Task <string> GetDataFromApiPost(DashletViewModel dashlet)
        {
            string responseContent = null;

            switch (dashlet.RequestType)
            {
            case "POST":
            {
                string requestBody = dashlet.Parametars;

                var httpContent = new StringContent(requestBody, Encoding.UTF8, "application/json");
                using (var httpClient = new HttpClient())
                {
                    // Do the actual request and await the response
                    var httpResponse = await httpClient.PostAsync(dashlet.DataSrc, httpContent);

                    // If the response contains content we want to read it!
                    if (httpResponse.Content != null)
                    {
                        responseContent = await httpResponse.Content.ReadAsStringAsync();
                    }
                }
            }
            break;

            case "GET":
            {
                string requestBody = dashlet.Parametars;

                var httpContent = new StringContent(requestBody, Encoding.UTF8, "application/json");
                using (var httpClient = new HttpClient())
                {
                    // Do the actual request and await the response
                    var httpResponse = await httpClient.GetAsync(dashlet.DataSrc);

                    // If the response contains content we want to read it!
                    if (httpResponse.Content != null)
                    {
                        responseContent = await httpResponse.Content.ReadAsStringAsync();
                    }
                }
            }
            break;
            }

            return(responseContent);
        }
Пример #4
0
        public async Task <DashletViewModel> GetDashboardDashletData(string dashboardId, string dashletId)
        {
            string           path       = "./dashboards.json";
            DashboardsJson   dashboards = new DashboardsJson();
            DashletViewModel dashlet    = new DashletViewModel();

            using (StreamReader reader = new StreamReader(path))
            {
                var dashboardJson = await reader.ReadToEndAsync();

                dashboards = JsonConvert.DeserializeObject <DashboardsJson>(dashboardJson);

                dashlet = dashboards?.dashboards?.Dashboard?
                          .FirstOrDefault(x => x.Id == dashboardId)
                          ?.Dashlets?.FirstOrDefault(x => x.Id == dashletId);
            }

            return(dashlet);
        }