Пример #1
0
        public object Get(GetUsageStats activity)
        {
            AuthorizationInfo user_info = _ac.GetAuthorizationInfo(Request);
            UserPolicy        policy    = _userManager.GetUserPolicy(user_info.User);

            if (!policy.IsAdministrator)
            {
                return(new List <Dictionary <string, object> >());
            }

            string[] filter_tokens = new string[0];
            if (activity.filter != null)
            {
                filter_tokens = activity.filter.Split(',');
            }

            DateTime end_date;

            if (string.IsNullOrEmpty(activity.end_date))
            {
                end_date = DateTime.Now;
            }
            else
            {
                _logger.Info("End_Date: " + activity.end_date);
                end_date = DateTime.ParseExact(activity.end_date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            }

            Dictionary <String, Dictionary <string, int> > results = repository.GetUsageForDays(activity.days, end_date, filter_tokens, activity.data_type);

            // add empty user for labels
            results.Add("labels_user", new Dictionary <string, int>());

            List <Dictionary <string, object> > user_usage_data = new List <Dictionary <string, object> >();

            foreach (string user_id in results.Keys)
            {
                Dictionary <string, int> user_usage = results[user_id];

                // fill in missing dates for time period
                SortedDictionary <string, int> userUsageByDate = new SortedDictionary <string, int>();
                DateTime from_date = end_date.AddDays((activity.days * -1) + 1);
                while (from_date <= end_date)
                {
                    string date_string = from_date.ToString("yyyy-MM-dd");
                    if (user_usage.ContainsKey(date_string) == false)
                    {
                        userUsageByDate.Add(date_string, 0);
                    }
                    else
                    {
                        userUsageByDate.Add(date_string, user_usage[date_string]);
                    }

                    from_date = from_date.AddDays(1);
                }

                string user_name = "Not Known";
                if (user_id == "labels_user")
                {
                    user_name = "labels_user";
                }
                else
                {
                    User user = null;
                    try
                    {
                        Guid user_guid = new Guid(user_id);
                        user = _userManager.GetUserById(user_guid);
                    }
                    catch (Exception e)
                    {
                        _logger.ErrorException("Error parsing user GUID : (" + user_id + ")", e);
                    }

                    if (user != null)
                    {
                        user_name = user.Name;
                    }
                    else
                    {
                        // if we could not get the user just use the user ID
                        user_name = user_id;
                    }
                }

                Dictionary <string, object> user_data = new Dictionary <string, object>();
                user_data.Add("user_id", user_id);
                user_data.Add("user_name", user_name);
                user_data.Add("user_usage", userUsageByDate);

                user_usage_data.Add(user_data);
            }

            var sorted_data = user_usage_data.OrderBy(dict => (dict["user_name"] as string).ToLower());

            return(sorted_data);
        }
Пример #2
0
        public object Get(GetUsageStats activity)
        {
            string[] filter_tokens = new string[0];
            if (activity.filter != null)
            {
                filter_tokens = activity.filter.Split(',');
            }

            DateTime end_date;

            if (string.IsNullOrEmpty(activity.end_date))
            {
                end_date = DateTime.Now;
            }
            else
            {
                _logger.Info("End_Date: " + activity.end_date);
                end_date = DateTime.ParseExact(activity.end_date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            }

            Dictionary <String, Dictionary <string, int> > results = Repository.GetUsageForDays(activity.days, end_date, filter_tokens, activity.data_type);

            // add empty user for labels
            results.Add("labels_user", new Dictionary <string, int>());

            List <Dictionary <string, object> > user_usage_data = new List <Dictionary <string, object> >();

            foreach (string user_id in results.Keys)
            {
                Dictionary <string, int> user_usage = results[user_id];

                // fill in missing dates for time period
                SortedDictionary <string, int> userUsageByDate = new SortedDictionary <string, int>();
                DateTime from_date = end_date.AddDays((activity.days * -1) + 1);
                while (from_date <= end_date)
                {
                    string date_string = from_date.ToString("yyyy-MM-dd");
                    if (user_usage.ContainsKey(date_string) == false)
                    {
                        userUsageByDate.Add(date_string, 0);
                    }
                    else
                    {
                        userUsageByDate.Add(date_string, user_usage[date_string]);
                    }

                    from_date = from_date.AddDays(1);
                }

                string user_name = "Not Known";
                if (user_id == "labels_user")
                {
                    user_name = "labels_user";
                }
                else
                {
                    Guid user_guid = new Guid(user_id);
                    MediaBrowser.Controller.Entities.User user = _userManager.GetUserById(user_guid);
                    if (user != null)
                    {
                        user_name = user.Name;
                    }
                }

                Dictionary <string, object> user_data = new Dictionary <string, object>();
                user_data.Add("user_id", user_id);
                user_data.Add("user_name", user_name);
                user_data.Add("user_usage", userUsageByDate);

                user_usage_data.Add(user_data);
            }

            var sorted_data = user_usage_data.OrderBy(dict => (dict["user_name"] as string).ToLower());

            return(sorted_data);
        }