/// <summary> /// /// </summary> public static AlertRule ExtractAlert(DataContext dc, ModelDashboard db, int panelId) { var rule = ExtractAlerts(dc, db) .FirstOrDefault(x => x.PanelId == panelId); if (null == rule) { AlertValidationException.ThrowBadPanel(); } return(rule); }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <returns></returns> public AlertRules ExtractAlerts() { var data = JObject.Parse(Dashboard.Data); var panels = data [PROP_PANELS]; var rules = new List <Rule>(); if (null == panels) { return(rules); } foreach (var p in panels) { var panelId = p [PROP_ID].Value <int>(); var jsonAlert = p [PROP_WIDGET]? [PROP_ALERT]; if (null == jsonAlert) { continue; } var jsonMetrics = p [PROP_WIDGET]? [PROP_METRICS]; var dataSource = GetDataSource(jsonMetrics); var queries = dataSource .QueryBuilder .Parse(jsonMetrics); try { var rule = jsonAlert .ToObject <Rule>() .Validate(refId => queries.WithRefId(dataSource.Id, refId)); rule.PanelId = panelId; rules.Add(rule); } catch (JsonSerializationException e) { // Hide deserialization details. AlertValidationException.ThrowBadDeserialization(e?.InnerException?.Message); } } return(rules); }
/// <summary> /// /// </summary> /// <param name="jsonMetrics"></param> /// <returns></returns> private ModelDataSource GetDataSource(JToken jsonMetrics) { var id = jsonMetrics? [PROP_DATA_SOURCE]?.Value <int>(); if (null == id) { AlertValidationException.ThrowBadDataSource(); } var dsRes = DataContext.GetRepo <DataSourceRepository>() [id.Value]; if (dsRes.HasError) { AlertValidationException.ThrowBadDataSource(); } return(dsRes.Value); }