Exemplo n.º 1
0
        public SendMetricResponse SendMetric(Guid accountId, SendMetricRequestData data)
        {
            var request = GetRequest <SendMetricRequest>(accountId);

            request.Data = data;
            var dispatcher = DispatcherHelper.GetDispatcherService();

            return(dispatcher.SendMetric(request));
        }
Exemplo n.º 2
0
        public JsonResult SetActualValue(SetActualValueModel model)
        {
            try
            {
                var metric = GetMetricById(model.Id);
                var client = GetDispatcherClient();

                // парсим число
                double?value = null;
                if (model.Value != null)
                {
                    try
                    {
                        value = ParseHelper.ParseDouble(model.Value);
                    }
                    catch (Exception)
                    {
                        throw new UserFriendlyException("Не удалось получить число из строки: " + model.Value);
                    }
                }

                var data = new SendMetricRequestData()
                {
                    ComponentId = metric.ComponentId,
                    Name        = metric.MetricType.SystemName,
                    Value       = value
                };
                if (model.ActualTime.HasValue)
                {
                    data.ActualIntervalSecs = model.ActualTime.Value.TotalSeconds;
                }
                client.SendMetric(CurrentUser.AccountId, data).Check();
                return(GetSuccessJsonResponse());
            }
            catch (Exception exception)
            {
                MvcApplication.HandleException(exception);
                return(GetErrorJsonResponse(exception));
            }
        }
Exemplo n.º 3
0
        public IMetricCacheReadObject SaveMetric(Guid accountId, SendMetricRequestData data)
        {
            if (data.Value.HasValue && (double.IsNaN(data.Value.Value) || double.IsInfinity(data.Value.Value)))
            {
                throw new ParameterErrorException("Metric value can't be Nan or Infinity");
            }

            var processDate      = DateTime.Now;
            var accountDbContext = Context.GetAccountDbContext(accountId);

            // Проверим лимиты
            var limitChecker = AccountLimitsCheckerManager.GetCheckerForAccount(accountId);

            limitChecker.CheckMetricsRequestsPerDay(accountDbContext);

            // Проверим лимит размера хранилища
            var size = data.GetSize();

            limitChecker.CheckStorageSize(accountDbContext, size);

            // получим тип метрики
            var metricType = GetOrCreateType(accountId, data.Name);

            // получим метрику
            var componentId = data.ComponentId.Value;
            var metricInfo  = FindMetric(accountId, componentId, metricType.Id);

            if (metricInfo == null)
            {
                var createMetricLock = LockObject.ForComponent(componentId);
                lock (createMetricLock)
                {
                    metricInfo = FindMetric(accountId, componentId, metricType.Id);
                    if (metricInfo == null)
                    {
                        CreateMetric(accountId, componentId, metricType.Id);
                        metricInfo = FindMetric(accountId, componentId, metricType.Id);
                        if (metricInfo == null)
                        {
                            throw new Exception("Не удалось создать метрику");
                        }
                    }
                }
            }
            using (var metric = AllCaches.Metrics.Write(metricInfo))
            {
                GetActualMetricInternal(metric, processDate);

                // если метрика выключена
                if (metric.CanProcess == false)
                {
                    throw new ResponseCodeException(Zidium.Api.ResponseCode.ObjectDisabled, "Метрика выключена");
                }

                // Рассчитаем цвет
                ObjectColor color;
                string      errorMessage = null;
                try
                {
                    color = GetColor(metricType, metric, data.Value);
                }
                catch (Exception exception)
                {
                    color        = ObjectColor.Red;
                    errorMessage = "Ошибка вычисления цвета метрики:" + exception.Message;
                }

                // Запишем метрику в хранилище
                var status = MonitoringStatusHelper.Get(color);

                // Время актуальности
                var actualTime =
                    metric.ActualTime
                    ?? metricType.ActualTime
                    ?? TimeSpanHelper.FromSeconds(data.ActualIntervalSecs)
                    ?? TimeSpan.FromHours(1);

                var actualDate = processDate + actualTime;
                var statusData = SetMetricValue(metricType, metric, data.Value, processDate, actualDate, status, errorMessage, true);

                limitChecker.AddMetricsRequestsPerDay(accountDbContext);
                limitChecker.AddMetricsSizePerDay(accountDbContext, size);

                metric.BeginSave();
                return(metric);
            }
        }