Ejemplo n.º 1
0
        /// <summary>
        /// Runs a filter on all events in the specified folder.
        /// </summary>
        /// <param name="filterId">ID of the filter.</param>
        /// <param name="folderId">ID of the folder to run the filter on.</param>
        /// <param name="runIfDisabled">If true, the filter will be run even if the filter is disabled.</param>
        public void RunFilterAgainstFolder(int filterId, int folderId, bool runIfDisabled = false)
        {
            FullFilter full = db.GetFilter(filterId);

            if (full == null)
            {
                throw new Exception("Failed to find filter with ID " + filterId);
            }
            RunFilterAgainstFolder(full, folderId, runIfDisabled);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Runs a filter on all events in the specified folder.
 /// </summary>
 /// <param name="full">Filter to run.</param>
 /// <param name="folderId">ID of the folder to run the filter on.</param>
 /// <param name="runIfDisabled">If true, the filter will be run even if the filter is disabled.</param>
 public void RunFilterAgainstFolder(FullFilter full, int folderId, bool runIfDisabled = false)
 {
     if (!full.filter.Enabled && !runIfDisabled)
     {
         return;                 // Filter is disabled and we aren't being told to run it anyway
     }
     if (!full.actions.Any(a => a.Enabled))
     {
         return;                 // No actions are enabled, so no need to run this filter.
     }
     if (full.filter.ConditionHandling != ConditionHandling.Unconditional && !full.conditions.Any(c => c.Enabled))
     {
         return;                 // No enabled conditions, and this is not an unconditional filter
     }
     foreach (Event e in db.GetEventsWithoutTagsInFolderDeferred(folderId, null))
     {
         DeferredRunFilterAgainstEvent(full, e, false);                 // This method can indicate to stop executing filters against the event, but we are already done either way.
     }
     deferredActions.ExecuteDeferredActions(db);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// <para>Runs the filter against the event (whether the filter is enabled or not).  Returns true if filter execution against this event should cease immediately.  This method logs exceptions and does not rethrow them.</para>
        /// <para>AFTER CALLING THIS METHOD ONE OR MORE TIMES, CALL deferredActions.ExecuteDeferredActions()</para>
        /// </summary>
        /// <param name="full">A filter to run against the event.</param>
        /// <param name="e">An event with the Tags field populated.</param>
        /// <param name="isEventSubmission">Pass true if the current operation is an event submission and this is the automatic filtering run. Additional logging may be performed for debugging purposes.</param>
        /// <returns></returns>
        private bool DeferredRunFilterAgainstEvent(FullFilter full, Event e, bool isEventSubmission)
        {
            try
            {
                // Evaluate Conditions
                bool conditionsMet = false;
                if (full.filter.ConditionHandling == ConditionHandling.Unconditional)
                {
                    conditionsMet = true;
                    if (conditionsMet && isEventSubmission && Settings.data.verboseSubmitLogging)
                    {
                        Util.SubmitLog(ProjectName, "Event " + e.EventId + " Filter " + full.filter.FilterId + " met unconditionally. " + full.actions.Count(a => a.Enabled) + "/" + full.actions.Length + " actions enabled.");
                    }
                }
                else if (full.filter.ConditionHandling == ConditionHandling.All)
                {
                    int metCount   = 0;
                    int triedCount = 0;
                    foreach (FilterCondition condition in full.conditions)
                    {
                        if (condition.Enabled)
                        {
                            triedCount++;
                            if (EvalCondition(condition, e, isEventSubmission))
                            {
                                metCount++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    conditionsMet = metCount > 0 && metCount == triedCount;
                    if (conditionsMet && isEventSubmission && Settings.data.verboseSubmitLogging)
                    {
                        Util.SubmitLog(ProjectName, "Event " + e.EventId + " Filter " + full.filter.FilterId + " met " + metCount + "/" + triedCount + " conditions. " + full.actions.Count(a => a.Enabled) + "/" + full.actions.Length + " actions enabled.");
                    }
                }
                else if (full.filter.ConditionHandling == ConditionHandling.Any)
                {
                    foreach (FilterCondition condition in full.conditions)
                    {
                        if (condition.Enabled)
                        {
                            if (EvalCondition(condition, e, isEventSubmission))
                            {
                                conditionsMet = true;
                                break;
                            }
                        }
                    }
                    if (conditionsMet && isEventSubmission && Settings.data.verboseSubmitLogging)
                    {
                        Util.SubmitLog(ProjectName, "Event " + e.EventId + " Filter " + full.filter.FilterId + " met any one condition. " + full.actions.Count(a => a.Enabled) + "/" + full.actions.Length + " actions enabled.");
                    }
                }
                else
                {
                    throw new Exception("[Filter " + full.filter.FilterId + "] Unsupported filter ConditionHandling: " + full.filter.ConditionHandling);
                }

                // Execute Actions if Conditions were sufficiently met.
                if (conditionsMet)
                {
                    foreach (FilterAction action in full.actions)
                    {
                        try
                        {
                            if (DeferredExecAction(action, e, isEventSubmission))
                            {
                                return(true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Debug(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
            return(false);
        }