public ActionResult WebpagePublicStats(int id, string path, DateTime?start, DateTime?end)
        {
            if (!start.HasValue)
            {
                start = new DateTime(DateTime.UtcNow.AddDays(-15).Year, DateTime.UtcNow.AddDays(-15).Month, DateTime.UtcNow.AddDays(-15).Day);
            }
            if (!end.HasValue)
            {
                end = DateTime.UtcNow;
            }
            WebpageDisplayPublic wdp = new WebpageDisplayPublic();

            wdp.Path      = path;
            wdp.WebsiteId = id;
            wdp.Start     = start.Value;
            wdp.End       = new DateTime(end.Value.Year, end.Value.Month, end.Value.Day, 23, 59, 59);
            if (wdp.Start.Year == wdp.End.Year && wdp.Start.Month == wdp.End.Month && wdp.Start.Day == wdp.End.Day)
            {
                wdp.Range = ReportDateRangeCustomType.Day;
            }
            else if (wdp.End.Subtract(wdp.Start).TotalDays < 60)
            {
                wdp.Range = ReportDateRangeCustomType.Days;
            }
            else if (wdp.End.Subtract(wdp.Start).TotalDays > 60)
            {
                wdp.Range = ReportDateRangeCustomType.Months;
            }
            wdp.VisitChartDescription = string.Format("Visits from {0} till {1}", wdp.Start.ToString("d/MMM/yy"), end);
            Website w = websiteRepository.GetWebsiteByID(id);

            if (w != null)
            {
                wdp.WebsiteName = w.Name;
                Uri     u  = new Uri(path);
                Webpage wp = webpageRepository.GetWebpage(w.ID, u.AbsolutePath.Trim(), u.Query.Trim());
                if (wp != null)
                {
                    var visits   = visitRepository.GetVisitsByWebpage(wp.ID, start, end);
                    var visitact = visitRepository.GetVisitPageActivities(visits.Select(t => t.ID).ToList(), wp.ID);


                    wdp.BrowserData.AddRange(GetBrowserUsage(visits.ToList()));
                    wdp.VisitCount = visits.ToList().Count;
                    DateTime current = DateTime.UtcNow;
                    Dictionary <string, int> browsers = new Dictionary <string, int>();
                    foreach (var item in visits)
                    {
                        string refstr = "";

                        if (!string.IsNullOrEmpty(item.Referer))
                        {
                            Uri referurl = new Uri(HttpUtility.UrlDecode(item.Referer));
                            refstr = referurl.Host;
                        }



                        RefererData rd = wdp.RefererList.SingleOrDefault(t => t.Referer == refstr);
                        if (rd != null)
                        {
                            rd.Count++;
                        }
                        else
                        {
                            wdp.RefererList.Add(new RefererData()
                            {
                                Count = 1, Referer = refstr
                            });
                        }
                    }
                    DateTime daystart = wdp.Start;
                    switch (wdp.Range)
                    {
                    case ReportDateRangeCustomType.Day:
                        while (daystart <= wdp.End)
                        {
                            VisitCountChartPoint p = new VisitCountChartPoint();
                            p.X = daystart.ToString("h:m tt");
                            p.Y = visits.Where(t => t.DateCreated >= daystart &&
                                               t.DateCreated <= daystart.AddHours(1)).Count();
                            wdp.VisitsData.Add(p);

                            VisitCountChartPoint np = new VisitCountChartPoint();
                            np.X = daystart.ToString("h:m tt");
                            np.Y = visits.Where(t => t.DateCreated >= daystart &&
                                                t.DateCreated <= daystart.AddHours(1) && t.LastVisitID.HasValue == false).Count();
                            wdp.NewVisitsData.Add(np);

                            VisitCountChartPoint rp = new VisitCountChartPoint();
                            rp.X = daystart.ToString("h:m tt");
                            rp.Y = visits.Where(t => t.DateCreated >= daystart &&
                                                t.DateCreated <= daystart.AddHours(1) && t.LastVisitID.HasValue == true).Count();
                            wdp.ReturnVisitsData.Add(rp);

                            daystart = daystart.AddHours(1);
                        }


                        break;


                    case ReportDateRangeCustomType.Days:

                        while (daystart <= wdp.End)
                        {
                            VisitCountChartPoint p = new VisitCountChartPoint();
                            p.X = daystart.ToString("MM/d");
                            p.Y = visits.Where(t => t.DateCreated.Year == daystart.Year && t.DateCreated.Month == daystart.Month && t.DateCreated.Day == daystart.Day).Count();
                            wdp.VisitsData.Add(p);

                            VisitCountChartPoint np = new VisitCountChartPoint();
                            np.X = daystart.ToString("MM/d");
                            np.Y = visits.Where(t => t.DateCreated.Year == daystart.Year && t.DateCreated.Month == daystart.Month && t.DateCreated.Day == daystart.Day && t.LastVisitID.HasValue == false).Count();
                            wdp.NewVisitsData.Add(np);

                            VisitCountChartPoint rp = new VisitCountChartPoint();
                            rp.X = daystart.ToString("MM/d");
                            rp.Y = visits.Where(t => t.DateCreated.Year == daystart.Year && t.DateCreated.Month == daystart.Month && t.DateCreated.Day == daystart.Day && t.LastVisitID.HasValue == true).Count();
                            wdp.ReturnVisitsData.Add(rp);

                            daystart = daystart.AddDays(1);
                        }


                        break;

                    case ReportDateRangeCustomType.Months:


                        while (daystart <= wdp.End)
                        {
                            VisitCountChartPoint p = new VisitCountChartPoint();
                            p.X = daystart.AddDays(15).ToString("y-M-d");
                            p.Y = visits.Where(t => t.DateCreated >= daystart && t.DateCreated < daystart.AddDays(15)).Count();
                            wdp.VisitsData.Add(p);

                            VisitCountChartPoint np = new VisitCountChartPoint();
                            np.X = daystart.AddDays(15).ToString("y-M-d");
                            np.Y = visits.Where(t => t.DateCreated >= daystart && t.DateCreated < daystart.AddDays(15) && t.LastVisitID.HasValue == false).Count();
                            wdp.NewVisitsData.Add(np);

                            VisitCountChartPoint rp = new VisitCountChartPoint();
                            rp.X = daystart.AddDays(15).ToString("y-M-d");
                            rp.Y = visits.Where(t => t.DateCreated >= daystart && t.DateCreated < daystart.AddDays(15) && t.LastVisitID.HasValue == true).Count();
                            wdp.ReturnVisitsData.Add(rp);

                            daystart = daystart.AddDays(15);
                        }

                        break;
                    }
                    HeatMapGenerator hmg = new HeatMapGenerator(Server.MapPath("~/Content/img/palette.bmp"));
                    hmg.ws         = w;
                    hmg.wp         = wp;
                    hmg.Activities = visitact.Where(t => t.Activity == ActivityName.Click).ToList();
                    hmg.GenerateMap();
                    wdp.HeatMapPath = hmg.HeatMapPath;
                }
            }


            return(View(wdp));
        }