/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
var App = require('app');
/**
* Mapper for App.AlertGroup
* Save general information
* Use App.cache['previousAlertGroupsMap']
to store map alertDefinitions-alertGroups. This map is used
* in the App.AlertDefinitionsMapper
to correctly link alertDefinitions and alertGroups
*/
App.alertGroupsMapper = App.QuickDataMapper.create({
model: App.AlertGroup,
config: {
id: 'AlertGroup.id',
name: 'AlertGroup.name',
default: 'AlertGroup.default',
targets_key: 'AlertGroup.targets',
targets_type: 'array',
targets: {
item: 'id'
}
},
/**
* Map for alertGroup's alertDefinitions
* Store alertDefinitions to alertGroup-properties basing on alertDefinitionType
* Format: key - alertDefinitionType, value - alertGroup-property where alertDefinition should be saved
* @type {object}
*/
typesMap: {
PORT: 'port_alert_definitions',
METRIC: 'metrics_alert_definitions',
WEB: 'web_alert_definitions',
AGGREGATE: 'aggregate_alert_definitions',
SCRIPT: 'script_alert_definitions'
},
map: function (json) {
if (!Em.isNone(json, 'items')) {
var alertGroups = [],
self = this,
groupsToDelete = App.AlertGroup.find().mapProperty('id'),
typesMap = this.get('typesMap'),
/**
* AlertGroups-map for App.AlertDefinitionsMappers
* Format:
*
* {
* alert_definition1_id: [alert_group1_id, alert_group2_id],
* alert_definition2_id: [alert_group3_id, alert_group1_id],
* ...
* }
*
* @type {object}
*/
alertDefinitionsGroupsMap = {},
alertNotificationsGroupsMap = {};
json.items.forEach(function(item) {
var group = self.parseIt(item, self.get('config'));
groupsToDelete = groupsToDelete.without(group.id);
Em.keys(typesMap).forEach(function(k) {
group[typesMap[k]] = [];
});
group.targets = [];
if (item.AlertGroup.definitions) {
item.AlertGroup.definitions.forEach(function(definition) {
var type = typesMap[definition.source_type];
if (!group[type].contains(definition.id)) {
group[type].push(definition.id);
}
if (Em.isNone(alertDefinitionsGroupsMap[definition.id])) {
alertDefinitionsGroupsMap[definition.id] = [];
}
alertDefinitionsGroupsMap[definition.id].push(group.id);
});
}
if (item.AlertGroup.targets) {
item.AlertGroup.targets.forEach(function(target) {
if (!group.targets.contains(target.id)) {
group.targets.push(target.id);
}
if (Em.isNone(alertNotificationsGroupsMap[target.id])) {
alertNotificationsGroupsMap[target.id] = [];
}
alertNotificationsGroupsMap[target.id].push(group.id);
});
}
alertGroups.push(group);
}, this);
groupsToDelete.forEach(function(groupId) {
self.deleteRecord(App.AlertGroup.find(groupId));
});
App.cache['previousAlertGroupsMap'] = alertDefinitionsGroupsMap;
App.cache['alertNotificationsGroupsMap'] = alertNotificationsGroupsMap;
App.store.loadMany(this.get('model'), alertGroups);
App.store.commit();
}
}
});