private static bool UploadMetrics(List <MetricAggregate> metrics)
        {
            try
            {
                if (metrics == null || metrics.Count == 0)
                {
                    return(true);
                }

                //checks are done outside this method before it gets this far to ensure API access is working

                List <SubmitMetricByIDModel> records = new List <SubmitMetricByIDModel>();

                foreach (var metric in metrics)
                {
                    SubmitMetricByIDModel model = new SubmitMetricByIDModel();
                    model.Value         = Math.Round(metric.Value, 2);
                    model.MonitorID     = metric.MonitorID ?? 0;
                    model.OccurredUtc   = metric.OccurredUtc;
                    model.Count         = metric.Count;
                    model.MonitorTypeID = (short)metric.MetricType;

                    if (_httpClient.AppIdentity != null)
                    {
                        model.ClientDeviceID = _httpClient.AppIdentity.DeviceID;
                    }

                    records.Add(model);

                    StackifyAPILogger.Log(string.Format("Uploading metric {0}:{1} Count {2}, Value {3}, ID {4}", metric.Category, metric.Name, metric.Count, metric.Value, metric.MonitorID));
                }



                string jsonData = JsonConvert.SerializeObject(records);

                var response = _httpClient.SendJsonAndGetResponse(
                    (_httpClient.BaseAPIUrl) +
                    "Metrics/SubmitMetricsByID",
                    jsonData);


                if (response.Exception == null && response.StatusCode == HttpStatusCode.OK)
                {
                    return(true);
                }

                if (response.Exception != null)
                {
                    StackifyAPILogger.Log("Error saving metrics " + response.Exception.Message);
                }

                return(false);
            }
            catch (Exception e)
            {
                StackifyAPILogger.Log("Error saving metrics " + e.Message);
                return(false);
            }
        }
Beispiel #2
0
        private static bool UploadMetrics(List <MetricAggregate> metrics)
        {
            try
            {
                if (metrics == null || metrics.Count == 0)
                {
                    return(true);
                }

                //checks are done outside this method before it gets this far to ensure API access is working

                var records = new List <SubmitMetricByIDModel>();

                foreach (var metric in metrics)
                {
                    var model = new SubmitMetricByIDModel
                    {
                        Value         = Math.Round(metric.Value, 2),
                        MonitorID     = metric.MonitorID ?? 0,
                        OccurredUtc   = metric.OccurredUtc,
                        Count         = metric.Count,
                        MonitorTypeID = (short)metric.MetricType
                    };

                    if (HttpClient.AppIdentity != null)
                    {
                        model.ClientDeviceID = HttpClient.AppIdentity.DeviceID;
                    }

                    records.Add(model);

                    StackifyAPILogger.Log(string.Format($"Uploading metric {metric.Category}:{metric.Name} Count {metric.Count}, Value {metric.Value}, ID {metric.MonitorID}"));
                }

                var jsonData = JsonConvert.SerializeObject(records);

                var response = HttpClient.SendJsonAndGetResponse($"{HttpClient.BaseAPIUrl}Metrics/SubmitMetricsByID", jsonData);

                if (response.Exception == null && response.StatusCode == HttpStatusCode.OK)
                {
                    return(true);
                }

                if (response.Exception != null)
                {
                    StackifyAPILogger.Log($"Error saving metrics {response.Exception.Message}");
                }

                return(false);
            }
            catch (Exception e)
            {
                StackifyAPILogger.Log($"Error saving metrics {e}");
                return(false);
            }
        }