List GenerateSequences()

in Source/Rules/PollingDetectionRule.cs [139:174]


        List<Sequence> GenerateSequences(List<CallDelta> deltas, double tolerancePercentage)
        {
            // Tolerance is how much we allow the delta to vary to call it a poll sequence
            double upperPercent = 1 + tolerancePercentage;
            double lowerPercent = 1 - tolerancePercentage;

            // For eaiser look up make a dicionary of delta grouped on the id of the first call in the pair
            var grouped = deltas.GroupBy(d => d.m_first.m_id).ToDictionary(g => g.Key, g => g.ToList());

            foreach (var delta in deltas)
            {
                if (grouped.ContainsKey(delta.m_second.m_id))
                {
                    // Look through all of the deltas that could potentially be the enxt one in the sequence
                    foreach (var potential in grouped[delta.m_second.m_id].Where(d => d.m_previous == null))
                    {
                        // If the delta fits within the range, then hook the 2 deltas together
                        if (potential.Delta >= delta.Delta * lowerPercent && potential.Delta <= delta.Delta * upperPercent)
                        {
                            delta.m_next = potential;
                            potential.m_previous = delta;
                        }
                    }
                }
            }

            // Walk though all of the deltas that are the first in the sequence and build them up.
            List<Sequence> completed = new List<Sequence>();
            foreach (var delta in deltas.Where(d => d.m_previous == null))
            {
                completed.Add(Sequence.Create(delta));
            }

            // Remove the sequences that are subsets of other sets or are shorter that minimum size
            return RemoveSubsets(completed.Where(s => s.Length >= m_minSequenceSize).ToList());
        }