Beispiel #1
0
        public ActionResult GetAnalyticsData()
        {
            // retrieve account analytic data
            string acctName    = RouteData.Values["account"] as string;
            var    analytics   = new List <object>();
            var    period      = String.Empty;
            var    totalVisits = 0;
            var    culture     = new CultureInfo("pt-BR");
            var    gotTotals   = false;

            if (acctName != null)
            {
                foreach (var module in ModRepository.GetAccountModules(acctName))
                {
                    if (!module.UsageCounters.IsLoaded)
                    {
                        module.UsageCounters.Load();
                    }

                    // get all module counters through last 30 days timespan
                    var validCounters = module
                                        .UsageCounters
                                        .Where(uc => IsValidCounter(uc))
                                        .Select(uc => new {
                        Date  = new DateTime(uc.Year, uc.Month, uc.Day),
                        Count = uc.RequestCount
                    });

                    if (!gotTotals && validCounters.Count() > 0)
                    {
                        period = String.Format("{0} a {1}.",
                                               validCounters.Select(vc => vc.Date).Min().ToString("dd/MM/yyyy", culture),
                                               validCounters.Select(vc => vc.Date).Max().ToString("dd/MM/yyyy", culture));

                        totalVisits += validCounters.Sum(uc => uc.Count);
                    }

                    analytics.Add(new {
                        label = module.DisplayName,
                        data  = validCounters.Select(uc => new long[] {
                            uc.Date.ToJavaScriptTimestamp(),
                            uc.Count
                        })
                    });
                }
            }
            else
            {
                var accounts = ModRepository.GetUserAccounts().OrderBy(a => a.Name);

                foreach (var account in accounts)
                {
                    if (!account.Modules.IsLoaded)
                    {
                        account.Modules.Load();
                    }

                    var groups = ModRepository.
                                 GetCountersByAccountId(account.Id)
                                 .Where(uc => IsValidCounter(uc))
                                 .Select(uc => new {
                        Date  = new DateTime(uc.Year, uc.Month, uc.Day),
                        Count = uc.RequestCount
                    }).GroupBy(uc => uc.Date);

                    var validCounters = groups.Select(g => new {
                        Date  = g.Key,
                        Count = g.Sum(gr => gr.Count)
                    });

                    if (!gotTotals && validCounters.Count() > 0)
                    {
                        period = String.Format("{0} a {1}",
                                               validCounters.Select(vc => vc.Date).Min().ToString("dd/MM/yyyy", culture),
                                               validCounters.Select(vc => vc.Date).Max().ToString("dd/MM/yyyy", culture));

                        totalVisits += validCounters.Sum(uc => uc.Count);
                    }

                    analytics.Add(new {
                        label = account.Name,
                        data  = validCounters.Select(uc => new long[] {
                            uc.Date.ToJavaScriptTimestamp(),
                            uc.Count
                        })
                    });
                }
            }

            var data = new {
                period      = period,
                totalVisits = totalVisits.ToString("0,0", culture),
                plotArray   = analytics
            };

            return(Json(data, JsonRequestBehavior.AllowGet));
        }