Example #1
0
        public async Task <bool> PopulateDownloadAnalyticObject(DownloadAnalytic analytic)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://freegeoip.net/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // New code:
                HttpResponseMessage response = await client.GetAsync("json/" + analytic.IPAddress);

                if (response.IsSuccessStatusCode)
                {
                    IpAddressObject ips = new IpAddressObject();
                    try
                    {
                        ips = await response.Content.ReadAsAsync <IpAddressObject>();
                    }
                    catch
                    {
                        return(false);;
                    }

                    analytic.Latitude        = ips.latitude;
                    analytic.Longitude       = ips.longitude;
                    analytic.City            = ips.city;
                    analytic.CountryCode     = ips.country_code;
                    analytic.MetroCode       = ips.metro_code;
                    analytic.RegionCode      = ips.region_code;
                    analytic.RegionName      = ips.region_name;
                    analytic.TimeZone        = ips.time_zone;
                    analytic.ZipCode         = ips.zip_code;
                    db.Entry(analytic).State = EntityState.Modified;
                    int records = db.SaveChanges();
                    if (records > 0)
                    {
                        return(true);
                    }
                    // Console.WriteLine("{0}\t${1}\t{2}", ips.Name, product.Price, product.Category);
                }
            }
            return(false);
        }
        // GET: Analytics
        // id is for service
        public async Task <ActionResult> Index(int?id)
        {
            //if (!User.IsInRole("Admin"))
            //{
            //    return View("NotFound");
            //}
            List <FuneralHome> homes     = new List <FuneralHome>();
            List <Analytic>    Analytics = new List <Analytic>();
            AnalyticViewModel  AVM       = new AnalyticViewModel();

            if (id == null)
            {
                if (User.IsInRole("FuneralHome"))
                {
                    var         userId = User.Identity.GetUserId();
                    FuneralHome fh     = db.FuneralHomes.Where(u => u.UserId == userId).FirstOrDefault();
                    Analytics.AddRange(db.Analytics.Where(a => a.Video.Service.FuneralHome.Id == fh.Id).ToList());
                    homes.Add(fh);
                    CalculateAnalytics.PageViews(ref AVM, Analytics, homes);
                }
                else
                {
                    //homes.AddRange(db.FuneralHomes.Where(f => f.DevHome == false).ToList());
                    //Analytics.AddRange(db.Analytics.ToList());
                }
            }
            else
            {
                Service serv = db.Services.Where(s => s.Id == id).FirstOrDefault();
                if (!Authorize(serv))
                {
                    return(View("NotFound"));
                }

                AVM.FirstName = serv.FirstName;
                AVM.LastName  = serv.LastName;
                homes.Add(db.FuneralHomes.Where(f => f.Id == serv.FuneralHome.Id).FirstOrDefault());
                Analytics.AddRange(db.Analytics.Where(a => a.Video.Service.Id == serv.Id).ToList());
                CalculateAnalytics.PageViewsForService(ref AVM, Analytics);
                AVM.TotalVideoViews = Analytics.Count();
            }



            List <IpAddressObject> addresses = new List <IpAddressObject>();

            foreach (var an in Analytics)
            {
                if (an.Video != null)
                {
                    if (an.Latitude != 0 && an.Longitude != 0)
                    {
                        //Database got mucked up with a bunch of hard coded values
                        //So lets exclude those IP addresses from the heat map
                        if (an.IPAddress != "174.17.56.90")
                        {
                            IpAddressObject add = new IpAddressObject();
                            add.IpAdd     = an.IPAddress;
                            add.latitude  = an.Latitude;
                            add.longitude = an.Longitude;
                            if (an.Video != null && an.Video.Service != null && an.Video.Service.VideoTitle == null)
                            {
                                add.VideoTitle = an.Video.Service.FirstName + " " + an.Video.Service.LastName + "'s Service";
                            }
                            else
                            {
                                add.VideoTitle = an.Video.Service.VideoTitle;
                            }
                            add.ViewingDate = an.CreateDate.ToShortDateString();
                            add.Name        = an.Video.Service.FirstName + " " + an.Video.Service.LastName;
                            int age = an.Video.Service.DeathDay.Year - an.Video.Service.Birthday.Year;
                            add.Age              = age.ToString();
                            add.FuneralHomeName  = an.Video.Service.FuneralHome.Name;
                            add.ServiceDate      = an.Video.Service.ServiceDate.ToShortDateString();
                            add.DaysAfterService = (an.CreateDate - an.Video.Service.ServiceDate).Days.ToString();
                            add.city             = an.City;
                            if (an.Stop < 280)
                            {
                                add.AmountWatched = "NA";
                            }
                            else
                            {
                                add.AmountWatched = Math.Round(((double)an.Stop / 60), 0).ToString();
                            }


                            addresses.Add(add);
                        }
                    }
                }
            }

            AVM.Details = addresses;

            CalculateAnalytics.AverageTime(ref AVM, Analytics);
            CalculateAnalytics.linechart(ref AVM, Analytics, false);
            return(View(AVM));
        }
        public ActionResult Dashboard()
        {
            if (!User.IsInRole("Admin"))
            {
                return(View("NotFound"));
            }
            //TODO: write logic to derive the owner id based on user that is logged in
            int ownerId = 2;
            List <FuneralHome>         Homes     = db.FuneralHomes.Where(f => f.DevHome == false && f.OwnerId == ownerId).ToList();
            List <Analytic>            Analytics = new List <Analytic>();
            AnalyticViewModel          AVM       = new AnalyticViewModel();
            AnalyticDashboardViewModel ADVM      = new AnalyticDashboardViewModel();
            List <Service>             Services  = new List <Service>();

            Analytics = db.Analytics.Where(a => a.Video.Service.FuneralHome.DevHome == false && a.Video.Service.FuneralHome.OwnerId == ownerId).ToList();
            Services  = db.Services.Where(s => s.FuneralHome.DevHome == false && s.FuneralHome.OwnerId == ownerId).ToList();

            List <IpAddressObject> addresses = new List <IpAddressObject>();

            foreach (var an in Analytics)
            {
                if (an.Latitude != 0 && an.Longitude != 0)
                {
                    //Database got mucked up with a bunch of hard coded values
                    //So lets exclude those IP addresses from the heat map
                    if (an.IPAddress != "174.17.56.90")
                    {
                        IpAddressObject add = new IpAddressObject();
                        add.IpAdd     = an.IPAddress;
                        add.latitude  = an.Latitude;
                        add.longitude = an.Longitude;
                        addresses.Add(add);
                    }
                }
            }

            DateTime        LastWeek          = DateTime.Now.AddDays(-7);
            List <Analytic> thisWeekAnalytics = Analytics.Where(a => a.CreateDate > LastWeek).ToList();
            string          mostViewedHome    = "";
            int             HighestVIews      = 0;
            int             popularhomeId     = 0;

            foreach (var home in Homes)
            {
                int homeViews = thisWeekAnalytics.Where(a => a.Video.Service.FuneralHome.Id == home.Id).Count();
                if (homeViews > HighestVIews)
                {
                    mostViewedHome = home.Name;
                    HighestVIews   = homeViews;
                    popularhomeId  = home.Id;
                }
            }
            CRMFuneralHome popFH = db.CRMFuneralHome.Where(f => f.FuneralHomeId == popularhomeId).FirstOrDefault();
            CRMContact     PopularHomePicCont = null;

            if (popFH != null)
            {
                PopularHomePicCont = popFH.PrimaryCRMContact;
            }

            if (PopularHomePicCont != null)
            {
                ADVM.PopularHomeOfTheWeekImage = PopularHomePicCont.PictureFileName;
            }
            ADVM.PopularHomeOfTheWeek  = mostViewedHome;
            ADVM.PopularHomeNumOfViews = HighestVIews;
            ADVM.ServicesBarChartArray = Admin.GetServicesBarChart(Services, DateTime.Now.AddMonths(-3), DateTime.Now);
            AVM.Details = addresses;
            CalculateAnalytics.PageViews(ref ADVM, Analytics, Homes);
            CalculateAnalytics.AverageTime(ref AVM, Analytics);
            Admin.dashboardlinechart(ref ADVM, Analytics);
            CalculateAnalytics.linechart(ref AVM, Analytics);
            ADVM.TotalFuneralsWithVideos   = Admin.GetTotalServicesWithVideo(Services);
            ADVM.MonthlyFuneralsWithVideos = Admin.GetServicesThisMonthWithVideo(Services);
            ADVM.LowUsageHomes             = CalculateAnalytics.LowUsageHomes(Homes);

            return(View(ADVM));
        }