Exemple #1
0
        private async Task UpdateTriggerActionsAsync(List <NotificationTrigger> triggers, CancellationToken token)
        {
            var actions = ResponseParser.GroupTriggerActions(triggers);

            var parameters = new NotificationActionParameters(actions.Select(a => a.Key).ToArray());

            var tasks = actions.Select(g => GetNotificationActionPropertiesAsync(g.Key, token));

            var normal = await ObjectEngine.GetObjectsXmlAsync(parameters, token : token).ConfigureAwait(false);

            //All the properties of all desired notifications
            var results = await Task.WhenAll(tasks).ConfigureAwait(false);

            //For each different notification action
            for (int i = 0; i < actions.Count; i++)
            {
                var xDoc = RequestParser.ExtractActionXml(normal, results[i], actions[i].Key);

                //Foreach notification action with the same ID
                foreach (var action in actions[i])
                {
                    action.LazyXml = new Lazy <XDocument>(() => xDoc);
                }
            }

            var list = ResponseParser.GroupActionSchedules(actions.SelectMany(g => g).ToList()).ToList();

            List <Schedule> schedules = new List <Schedule>();

            if (list.Count > 0)
            {
                schedules = await GetSchedulesAsync(Property.Id, list.Select(l => l.Key).ToArray(), token).ConfigureAwait(false);
            }

            foreach (var group in actions)
            {
                foreach (var action in group)
                {
                    if (action.lazyScheduleStr != null)
                    {
                        var id = PrtgObject.GetId(action.lazyScheduleStr);

                        if (id != -1)
                        {
                            action.schedule = new Lazy <Schedule>(() => schedules.First(s => s.Id == id));
                        }
                        else
                        {
                            action.schedule = new Lazy <Schedule>(() => new Schedule(action.lazyScheduleStr));
                        }
                    }
                }
            }
        }
        //######################################
        // GetNotificationActionsInternal
        //######################################

        internal List <NotificationAction> GetNotificationActionsInternal(NotificationActionParameters parameters)
        {
            var response = requestEngine.ExecuteRequest(XmlFunction.TableData, parameters);

            var items = response.Descendants("item").ToList();

            foreach (var item in items)
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = GetNotificationActionProperties(id);

                item.Add(properties.Nodes());
            }

            return(XmlDeserializer <NotificationAction> .DeserializeList(response).Items);
        }
Exemple #3
0
        private void UpdateTriggerActions(List <NotificationTrigger> triggers, CancellationToken token)
        {
            //Group all actions from all triggers together based on their object ID
            var actions = ResponseParser.GroupTriggerActions(triggers);

            //Retrieve the XML required to construct "proper" notification actions for all unique actions
            //specified in the triggers
            var actionParameters = new NotificationActionParameters(actions.Select(a => a.Key).ToArray());
            var normalActions    = new Lazy <XDocument>(() => ObjectEngine.GetObjectsXml(actionParameters, token: token), LazyThreadSafetyMode.PublicationOnly);

            foreach (var group in actions)
            {
                //As soon as a notification with a specified ID is accessed on any one of the triggers, retrieve
                //the "supported" properties of ALL of the notification actions, and then retrieve the "unsupported"
                //properties of JUST the notification action object ID that was accessed.
                var lazyAction = new Lazy <XDocument>(
                    () => RequestParser.ExtractActionXml(normalActions.Value, GetNotificationActionProperties(group.Key, token), @group.Key),
                    LazyThreadSafetyMode.PublicationOnly
                    );

                Logger.Log("Setting lazy action to retrieve notification actions");

                foreach (var action in group)
                {
                    action.LazyXml = lazyAction;

                    Logger.Log("Setting lazy action to retrieve notification schedule");

                    action.schedule = new Lazy <Schedule>(
                        () =>
                    {
                        if (action.lazyScheduleStr != null && PrtgObject.GetId(action.lazyScheduleStr) != -1)
                        {
                            Logger.Log($"Resolving schedule {action.lazyScheduleStr} to schedule");

                            return(GetSchedule(new Schedule(action.lazyScheduleStr).Id, token));
                        }

                        return(action.lazyScheduleStr == null ? null : new Schedule(action.lazyScheduleStr));
                    }, LazyThreadSafetyMode.PublicationOnly);
                }
            }
        }
        //######################################
        // GetNotificationActionsInternal
        //######################################

        internal List <NotificationAction> GetNotificationActionsInternal(NotificationActionParameters parameters)
        {
            var response = RequestEngine.ExecuteRequest(parameters);

            var items = response.Descendants("item").ToList();

            foreach (var item in items)
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = GetNotificationActionProperties(id);

                item.Add(properties.Nodes());
            }

            var actions = XmlDeserializer <NotificationAction> .DeserializeList(response).Items;

            var actionsWithSchedules = ResponseParser.GroupActionSchedules(actions).ToList();

            UpdateActionSchedules(actionsWithSchedules);

            return(actions);
        }
        internal async Task <List <NotificationAction> > GetNotificationActionsInternalAsync(NotificationActionParameters parameters)
        {
            var response = await RequestEngine.ExecuteRequestAsync(parameters).ConfigureAwait(false);

            var items = response.Descendants("item").ToList();

            await Task.WhenAll(items.Select(async item =>
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = await GetNotificationActionPropertiesAsync(id).ConfigureAwait(false);

                item.Add(properties.Nodes());
            })).ConfigureAwait(false);

            var actions = XmlDeserializer <NotificationAction> .DeserializeList(response).Items;

            var actionsWithSchedules = ResponseParser.GroupActionSchedules(actions).ToList();

            await UpdateActionSchedulesAsync(actionsWithSchedules).ConfigureAwait(false);

            return(actions);
        }