Пример #1
0
        private async Task <bool> CheckSentimentAlert(SentimentAlert alert, DateTime now)
        {
            Subject     subject     = alert.Subject;
            FeedManager feedManager = new FeedManager();
            List <Feed> feeds;

            DateTime end   = now;
            DateTime start = end.AddDays(-7);
            Dictionary <string, double> valuePairs = new Dictionary <string, double>();

            if (subject.GetType() == typeof(Person))
            {
                feeds = await feedManager.GetPersonFeedsSinceAsync(subject.Name, start);
            }
            else if (subject.GetType() == typeof(Organisation))
            {
                feeds = await feedManager.GetOrganisationFeedsSinceAsync(subject.Name, start);
            }
            else
            {
                feeds = await feedManager.GetWordFeedsSinceAsync(subject.Name, start);
            }

            int feedCount   = feeds.Count();
            int posNegFeeds = 0;
            int result;

            if (alert.SubjectProperty.Equals(SubjectProperty.pos))
            {
                foreach (Feed f in feeds)
                {
                    string[] sentiment = f.Sentiment.Split(',');
                    float    posneg    = float.Parse(sentiment[0], System.Globalization.CultureInfo.InvariantCulture);
                    if (posneg > 0)
                    {
                        posNegFeeds++;
                    }
                }
                valuePairs.Add("Positive", posNegFeeds);
                valuePairs.Add("Negative", feedCount - posNegFeeds);
                result = posNegFeeds / feedCount;
            }
            else
            {
                foreach (Feed f in feeds)
                {
                    string[] sentiment = f.Sentiment.Split(',');
                    float    posneg    = float.Parse(sentiment[0], System.Globalization.CultureInfo.InvariantCulture);
                    if (posneg < 0)
                    {
                        posNegFeeds++;
                    }
                    valuePairs.Add("Positive", feedCount - posNegFeeds);
                    valuePairs.Add("Negative", posNegFeeds);
                }
                result = (posNegFeeds / feedCount) * 100;
            }

            alert.Graph.EndDate   = end;
            alert.Graph.StartDate = start;
            alert.JsonValues      = JsonConvert.SerializeObject(valuePairs);

            switch (alert.Operator)
            {
            case Operator.GT:
                if (result >= alert.Value)
                {
                    return(true);
                }
                break;

            case Operator.LT:
                if (result <= alert.Value)
                {
                    return(true);
                }
                break;
            }

            return(false);
        }
Пример #2
0
        private async Task <bool> CheckTrendAlert(TrendAlert alert, DateTime now)
        {
            Subject subject = alert.Subject;

            int    days      = 6;
            int    fcToday   = 0;
            int    fcHistory = 0;
            float  avarage   = 0;
            double std       = 0;
            double zScore    = 0;

            FeedManager        feedManager = new FeedManager();
            IEnumerable <Feed> feeds;

            DateTime end   = now;
            DateTime start = end.AddDays(-7);
            Dictionary <string, List <double> > valuePairs = new Dictionary <string, List <double> >();
            List <double> values = new List <double>();

            if (subject.GetType() == typeof(Person))
            {
                feeds = await feedManager.GetPersonFeedsSinceAsync(subject.Name, start);
            }
            else if (subject.GetType() == typeof(Organisation))
            {
                feeds = await feedManager.GetOrganisationFeedsSinceAsync(subject.Name, start);
            }
            else
            {
                feeds = await feedManager.GetWordFeedsSinceAsync(subject.Name, start);
            }

            foreach (Feed f in feeds)
            {
                if (f.Date.Ticks >= end.AddDays(-1).Ticks)
                {
                    fcToday++;
                }
            }

            for (int i = 6; i > 0; i--)
            {
                foreach (Feed f in feeds)
                {
                    if (start.AddDays(6 - i).Ticks <= f.Date.Ticks && f.Date.Ticks < end.AddDays(-i).Ticks)
                    {
                        fcHistory++;
                    }
                }
                avarage += fcHistory;
                values.Add(fcHistory);
                fcHistory = 0;
            }

            valuePairs.Add(subject.Name, values);

            avarage = (float)avarage / days;

            foreach (float item in values)
            {
                std += Math.Pow(item - avarage, 2);
            }

            std = Math.Sqrt(std / days);

            zScore = ((float)fcToday - avarage) / std;

            alert.Graph.EndDate   = end;
            alert.Graph.StartDate = start;
            alert.JsonValues      = JsonConvert.SerializeObject(valuePairs);

            if (zScore > 2)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        private async Task <bool> CheckCheckAlert(CheckAlert alert, DateTime now)
        {
            Subject     subject     = alert.Subject;
            FeedManager feedManager = new FeedManager();
            List <Feed> feeds;

            DateTime end   = now;
            DateTime start = end.AddDays(-7);
            Dictionary <string, List <double> > valuePairs = new Dictionary <string, List <double> >();
            List <double> values = new List <double>();

            if (subject.GetType() == typeof(Person))
            {
                feeds = await feedManager.GetPersonFeedsSinceAsync(subject.Name, start);
            }
            else if (subject.GetType() == typeof(Organisation))
            {
                feeds = await feedManager.GetOrganisationFeedsSinceAsync(subject.Name, start);
            }
            else
            {
                feeds = await feedManager.GetWordFeedsSinceAsync(subject.Name, start);
            }

            int fCPast = 0;

            int fc = 0;

            for (int i = 6; i > 0; i--)
            {
                foreach (Feed f in feeds)
                {
                    if (start.AddDays(6 - i).Ticks <= f.Date.Ticks && f.Date.Ticks < end.AddDays(-i).Ticks)
                    {
                        fc++;
                    }
                }
                fCPast += fc;
                values.Add(fc);
                fc = 0;
            }

            fCPast = fCPast / 6;

            fCPast = (fCPast == 0) ? 1 : fCPast;

            int fCNow = 0;

            foreach (Feed f in feeds)
            {
                if (f.Date.Ticks >= end.AddDays(-1).Ticks)
                {
                    fCNow++;
                }
            }
            values.Add(fCNow);
            valuePairs.Add(subject.Name, values);

            int result;

            if (alert.SubjectProperty.Equals(SubjectProperty.relativeCount))
            {
                result = (fCNow / fCPast - 1) * 100;
            }
            else
            {
                result = fCNow - fCPast;
            }

            alert.Graph.EndDate   = end;
            alert.Graph.StartDate = start;
            alert.JsonValues      = JsonConvert.SerializeObject(valuePairs);

            switch (alert.Operator)
            {
            case Operator.GT:
                if (result >= alert.Value)
                {
                    return(true);
                }
                break;

            case Operator.LT:
                if (result <= alert.Value)
                {
                    return(true);
                }
                break;
            }

            return(false);
        }
Пример #4
0
        private async Task <bool> CheckCompareAlert(CompareAlert alert, DateTime now)
        {
            Subject subjectA = alert.SubjectA;
            Subject subjectB = alert.SubjectB;

            int fcA = 0;
            int fcB = 0;

            FeedManager feedManager = new FeedManager();
            List <Feed> feedsA;
            List <Feed> feedsB;

            DateTime end   = now;
            DateTime start = end.AddDays(-7);
            Dictionary <string, double> valuePairs = new Dictionary <string, double>();

            if (subjectA.GetType() == typeof(Person))
            {
                feedsA = await feedManager.GetPersonFeedsSinceAsync(subjectA.Name, start);

                feedsB = await feedManager.GetPersonFeedsSinceAsync(subjectB.Name, start);
            }
            else if (subjectA.GetType() == typeof(Organisation))
            {
                feedsA = await feedManager.GetOrganisationFeedsSinceAsync(subjectA.Name, start);

                feedsB = await feedManager.GetOrganisationFeedsSinceAsync(subjectB.Name, start);
            }
            else
            {
                feedsA = await feedManager.GetWordFeedsSinceAsync(subjectA.Name, start);

                feedsB = await feedManager.GetWordFeedsSinceAsync(subjectB.Name, start);
            }

            fcA = feedsA.Count();
            valuePairs.Add(subjectA.Name, fcA);
            fcB = feedsB.Count();
            valuePairs.Add(subjectB.Name, fcB);

            alert.Graph.EndDate   = end;
            alert.Graph.StartDate = start;
            alert.JsonValues      = JsonConvert.SerializeObject(valuePairs);

            switch (alert.Operator)
            {
            case Operator.GT:
                if (fcA > fcB)
                {
                    return(true);
                }
                break;

            case Operator.LT:
                if (fcA < fcB)
                {
                    return(true);
                }
                break;
            }
            return(false);
        }
Пример #5
0
        public async Task WeeklyReview(DateTime now)
        {
            FeedManager    feedManager = new FeedManager();
            List <Subject> subjects    = await repo.ReadSubjectsAsync();

            foreach (Subject subject in subjects)
            {
                int    days      = 6;
                int    fcToday   = 0;
                int    fcHistory = 0;
                float  avarage   = 0;
                double std       = 0;
                double zScore    = 0;

                List <Feed> feeds;

                DateTime      end    = now;
                DateTime      start  = end.AddDays(-7);
                List <double> values = new List <double>();

                if (subject.GetType().Equals(typeof(Person)))
                {
                    feeds = await feedManager.GetPersonFeedsSinceAsync(subject.Name, now.AddDays(-7));

                    subject.FeedCount = feeds.Count();
                }
                else if (subject.GetType().Equals(typeof(Organisation)))
                {
                    feeds = await feedManager.GetOrganisationFeedsSinceAsync(subject.Name, now.AddDays(-7));

                    subject.FeedCount = feeds.Count();
                }
                else
                {
                    feeds = await feedManager.GetWordFeedsSinceAsync(subject.Name, now.AddDays(-7));

                    subject.FeedCount = feeds.Count();
                }

                foreach (Feed f in feeds)
                {
                    if (f.Date.Ticks >= end.AddDays(-1).Ticks)
                    {
                        fcToday++;
                    }
                }

                for (int i = 6; i > 0; i--)
                {
                    foreach (Feed f in feeds)
                    {
                        if (start.AddDays(6 - i).Ticks <= f.Date.Ticks && f.Date.Ticks < end.AddDays(-i).Ticks)
                        {
                            fcHistory++;
                        }
                    }
                    avarage += fcHistory;
                    values.Add(fcHistory);
                    fcHistory = 0;
                }

                avarage = (float)avarage / days;

                foreach (float item in values)
                {
                    std += Math.Pow(item - avarage, 2);
                }

                std = Math.Sqrt(std / days);

                zScore = ((float)fcToday - avarage) / std;

                if (zScore > 2)
                {
                    subject.Trending = true;
                }
                else
                {
                    subject.Trending = false;
                }
            }

            await repo.UpdateSubjects(subjects);
        }