private async Task SendEmailMoreThanAverage(ActivityCount ac)
 {
     try
     {
         await _mailService.SendToManyActivitiesEmailAsync(ac);
     }
     catch (Exception ex)
     {
     }
 }
        private async Task SendEmail(ActivityCount ac)
        {
            MailRequest request = new MailRequest();

            request.ToEmail = "*****@*****.**";
            request.Subject = ac.Address;
            request.Body    = $"{ac.Address} har aktiverats mer än genomsnittet. {ac.CountActivity} gånger denna timme {ac.Hourly}\n\r Mvh Löva";


            await _mailService.SendEmailAsync(request);
        }
Exemple #3
0
        public async Task <IActionResult> SendWelcomeMail([FromForm] ActivityCount request)
        {
            try
            {
                //await mailService.SendToManyActivitiesEmailAsync(request);
                await mailService.SendNoActivitiesEmailAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemple #4
0
        public List <ActivityCount> FetchActivityCount()
        {
            var q1 = from e in DbUtil.Db.CheckInTimes
                     join c in DbUtil.Db.CheckInActivities on e.Id equals c.Id
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     group c by c.Activity into grouped
                     select new ActivityCount()
            {
                name = grouped.Key, count = grouped.Count()
            };

            var q2 = from e in DbUtil.Db.CheckInTimes
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     group e by e.PeopleId into grouped
                     select new { key = grouped.Key };

            var q3 = from e in DbUtil.Db.CheckInTimes
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     select e;

            var p = new ActivityCount()
            {
                name = "Total Guests", count = q3.Count()
            };
            var q = new ActivityCount()
            {
                name = "Unique People", count = q2.Count()
            };


            var lq = q1.ToList();

            lq.Insert(0, q);
            lq.Insert(0, p);

            return(lq);
        }
Exemple #5
0
        public async Task SendToManyActivitiesEmailAsync(ActivityCount request)
        {
            string FilePath = contentRootPath +
                              Path.DirectorySeparatorChar.ToString()
                              + "Templates"
                              + Path.DirectorySeparatorChar.ToString()
                              + "HighActivities.txt";

            // FilePath = "https://lottingelundfiles.blob.core.windows.net/emailtemplates/HighActivities.html";

            string MailText = string.Empty;

            using (StreamReader str = new StreamReader(FilePath))
            {
                MailText = str.ReadToEnd();
            };


            MailText = MailText.Replace("[adress]", request.Address)
                       .Replace("[antalAktiveringar]", request.CountActivity.ToString())
                       .Replace("[timme]", request.Hourly.ToString());
            var email = new MimeMessage();

            email.Sender = MailboxAddress.Parse(_mailSettings.Mail);
            email.To.Add(MailboxAddress.Parse("*****@*****.**"));
            email.Subject = $"Intagsenhet {request.Address}";
            var builder = new BodyBuilder();

            builder.HtmlBody = MailText;
            email.Body       = builder.ToMessageBody();
            using var smtp   = new SmtpClient();
            smtp.Connect(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls);
            smtp.Authenticate(_mailSettings.Mail, _mailSettings.Password);
            await smtp.SendAsync(email);

            smtp.Disconnect(true);
        }
        private async Task SaveToTableStorage(ActivityViewModel drainPatrolViewModel)
        {
            // Create reference to an existing table
            CloudTable table = await TableStorageCommon.CreateTableAsync("Drains");

            DrainTableStorageEntity drainExistingRow = new DrainTableStorageEntity();

            // Get existing data for a specific master_node and address
            drainExistingRow = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address);


            // Create a new/update record for Azure Table Storage
            DrainTableStorageEntity drain = new DrainTableStorageEntity(drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address);

            // Check if address is actice
            if (drainPatrolViewModel.Active)
            {
                // Store data in Azure nosql table if Active == true
                drain.TimeUp          = drainPatrolViewModel.Time.ToLocalTime();
                drain.TimeDown        = drainExistingRow.TimeDown.ToLocalTime();
                drain.IsActive        = drainPatrolViewModel.Active;
                drain.AverageActivity = drainExistingRow.AverageActivity;

                var diff = (drainPatrolViewModel.Time - drainExistingRow.TimeDown).TotalSeconds;
                if (drainExistingRow.AverageRest == 0)
                {
                    drain.AverageRest = (int)diff;
                }
                else
                {
                    drain.AverageRest = (int)((drainExistingRow.AverageRest + diff) / 2);
                }

                // Add hourly counter if within same hour otherwise save count to Azure SQL table AcitvityCounts
                if (DateExtensions.NewHour(drainPatrolViewModel.Time.ToLocalTime(), drainExistingRow.TimeUp.ToLocalTime()))
                {
                    // New hour reset counter to one
                    drain.HourlyCount = 1;

                    if (DateExtensions.IsNewDay(drainPatrolViewModel.Time.ToLocalTime(), drainExistingRow.TimeUp.ToLocalTime()))
                    {
                        drain.DailyCount = 1;
                    }
                    else
                    {
                        drain.DailyCount = drain.DailyCount + 1;
                    }

                    var averageCount = drainExistingRow.AverageActivity;

                    // Save counter
                    ActivityCount ac = new ActivityCount
                    {
                        Address       = drainExistingRow.RowKey,
                        CountActivity = drainExistingRow.HourlyCount,
                        Hourly        = DateExtensions.RemoveMinutesAndSeconds(convertToLocalTimeZone(drainExistingRow.TimeUp)),
                        AverageCount  = averageCount
                    };


                    drain.AverageActivity = (averageCount + drainExistingRow.HourlyCount) / 2;

                    await TableStorageUtils.InsertOrMergeEntityAsync(table, drain);

                    _context.ActivityCounts.Add(ac);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    // withing the same hour add one to existing sum.
                    drain.HourlyCount = drainExistingRow.HourlyCount + 1;
                    drain.DailyCount  = drainExistingRow.DailyCount + 1;



                    // Save updated to the Azure nosql table
                    await TableStorageUtils.InsertOrMergeEntityAsync(table, drain);
                }
            }
            else
            {
                // Get data from Azure table and store data in one row on ActivityPerRow

                // drain = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address);
                drain.TimeUp      = drainExistingRow.TimeUp.ToLocalTime();
                drain.TimeDown    = drainPatrolViewModel.Time.ToLocalTime();
                drain.IsActive    = drainPatrolViewModel.Active;
                drain.HourlyCount = drainExistingRow.HourlyCount;
                drain.AverageRest = drainExistingRow.AverageRest;

                var diff = (drain.TimeDown - drain.TimeUp).TotalSeconds;
                if (drainExistingRow.AverageActivity == 0)
                {
                    drain.AverageActivity = (int)diff;
                }
                else
                {
                    drain.AverageActivity = (int)((drainExistingRow.AverageActivity + diff) / 2);
                }



                await TableStorageUtils.InsertOrMergeEntityAsync(table, drain);

                bool isGroup = false;

                if (drain.RowKey.Contains("7") || drain.RowKey.Contains("8"))
                {
                    isGroup = true;
                }



                var perRowData = new ActivityPerRow
                {
                    Address  = drain.RowKey,
                    TimeUp   = drain.TimeUp,
                    TimeDown = drain.TimeDown,
                    TimeDiff = (drain.TimeDown - drain.TimeUp).TotalMilliseconds,
                    //(drain.TimeDown - drain.TimeUp.AddHours(1)).TotalMilliseconds,
                    IsGroupAddress = isGroup
                };


                _context.ActivityPerRows.Add(perRowData);
                await _context.SaveChangesAsync();
            }
        }
Exemple #7
0
        protected internal virtual void addTableBody(TabularResultSet tabularResultSet, IList <string> watchActivities, PerfTestResult passResult)
        {
            // get first and last timestamp
            DateTime firstStartTime = null;
            DateTime lastEndTime    = null;

            foreach (IList <ActivityPerfTestResult> activityResults in passResult.ActivityResults.Values)
            {
                foreach (ActivityPerfTestResult activityResult in activityResults)
                {
                    if (firstStartTime == null || activityResult.StartTime < firstStartTime)
                    {
                        firstStartTime = activityResult.StartTime;
                    }

                    if (lastEndTime == null || activityResult.EndTime > lastEndTime)
                    {
                        lastEndTime = activityResult.EndTime;
                    }
                }
            }

            long firstTimestamp = firstStartTime.Ticks;
            long lastTimestamp  = lastEndTime.Ticks;
            IList <IDictionary <string, ActivityCount> > resultTable = new List <IDictionary <string, ActivityCount> >();

            for (long t = firstTimestamp; t <= lastTimestamp + INTERVAL; t += INTERVAL)
            {
                IDictionary <string, ActivityCount> activitiesMap = new Dictionary <string, ActivityCount>();
                foreach (string activity in watchActivities)
                {
                    activitiesMap[activity] = new ActivityCount(this);
                }
                resultTable.Add(activitiesMap);
            }


            foreach (IList <ActivityPerfTestResult> activityResults in passResult.ActivityResults.Values)
            {
                foreach (ActivityPerfTestResult activityResult in activityResults)
                {
                    string activityId = activityResult.ActivityId;
                    int    startSlot  = calculateTimeSlot(activityResult.StartTime, firstTimestamp);
                    int    endSlot    = calculateTimeSlot(activityResult.EndTime, firstTimestamp);
                    resultTable[startSlot][activityId].incrementStarted();
                    resultTable[endSlot][activityId].incrementEnded();
                    resultTable[endSlot][activityId].addDuration(activityResult.Duration);
                }
            }

            List <object> row = null;
            IDictionary <string, ActivityCount> sumMap = new Dictionary <string, ActivityCount>();

            foreach (string activity in watchActivities)
            {
                sumMap[activity] = new ActivityCount(this);
            }

            long sumActivitiesEnded = 0;

            for (int i = 0; i < resultTable.Count; i++)
            {
                row = new List <object>();
                row.Add(i * INTERVAL / TIME_UNIT);
                long currentActivitiesEnded = 0;
                foreach (string activity in watchActivities)
                {
                    sumMap[activity].addStarted(resultTable[i][activity].Started);
                    sumMap[activity].addEnded(resultTable[i][activity].Ended);
                    sumMap[activity].addDuration(resultTable[i][activity].Duration);
                    currentActivitiesEnded += resultTable[i][activity].Ended;
                }
                foreach (string activity in watchActivities)
                {
                    long   started       = resultTable[i][activity].Started;
                    long   ended         = resultTable[i][activity].Ended;
                    double endedFraction = 0;
                    long   avgDuration   = 0;

                    if (sumMap[activity].Ended > 0)
                    {
                        avgDuration = sumMap[activity].Duration / sumMap[activity].Ended;
                    }

                    if (currentActivitiesEnded > 0)
                    {
                        endedFraction = ended * 100.0 / currentActivitiesEnded;
                    }

                    row.Add(started);
                    row.Add(sumMap[activity].Started);
                    row.Add(string.Format("{0:D} ({1:F1}%)", ended, endedFraction));
                    row.Add(sumMap[activity].Ended);
                    row.Add(avgDuration + " ms");
                }
                sumActivitiesEnded += currentActivitiesEnded;
                row.Add(currentActivitiesEnded / INTERVAL_SECONDS);
                row.Add(sumActivitiesEnded / ((i + 1) * INTERVAL_SECONDS));
                tabularResultSet.addResultRow(row);
            }
        }
Exemple #8
0
        public List<ActivityCount> FetchActivityCount()
        {
            var q1 = from e in DbUtil.Db.CheckInTimes
                     join c in DbUtil.Db.CheckInActivities on e.Id equals c.Id
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     group c by c.Activity into grouped
                     select new ActivityCount() { name = grouped.Key, count = grouped.Count() };

            var q2 = from e in DbUtil.Db.CheckInTimes
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     group e by e.PeopleId into grouped
                     select new { key = grouped.Key };

            var q3 = from e in DbUtil.Db.CheckInTimes
                     where e.Location == location
                     where e.CheckInTimeX >= dateStart || dateStart == null
                     where e.CheckInTimeX < dateEnd || dateEnd == null
                     select e;

            var p = new ActivityCount() { name = "Total Guests", count = q3.Count() };
            var q = new ActivityCount() { name = "Unique People", count = q2.Count() };


            var lq = q1.ToList();
            lq.Insert(0, q);
            lq.Insert(0, p);

            return lq;
        }