public static AccountsSnapshot GetAccountsSnapshot()
        {
            var accountsSnapshotLoclCacheKey = "accountsSnapshotcachekey";

            var accountsSnapshot = new AccountsSnapshot();

            if (System.Web.HttpRuntime.Cache.Get(accountsSnapshotLoclCacheKey) == null)
            {
                //if local cache is empty

                var platformManagementServiceClient = new PlatformManagementService.PlatformManagementServiceClient();

                try
                {
                    platformManagementServiceClient.Open();

                    accountsSnapshot = platformManagementServiceClient.GetAccountsShapshot(Common.SharedClientKey);

                    //Close the connection
                    WCFManager.CloseConnection(platformManagementServiceClient);

                    //Store snapshot in local cache for 5 seconds (for charting components to use):
                    System.Web.HttpRuntime.Cache.Insert(accountsSnapshotLoclCacheKey, accountsSnapshot, null, DateTime.Now.AddSeconds(5), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
                }
                catch (Exception e)
                {
                    #region Manage Exception

                    string exceptionMessage = e.Message.ToString();

                    var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                    string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                    // Abort the connection & manage the exception
                    WCFManager.CloseConnection(platformManagementServiceClient, exceptionMessage, currentMethodString);

                    #endregion
                }
            }
            else
            {
                // Get snapshot from cache
                accountsSnapshot = (AccountsSnapshot)System.Web.HttpRuntime.Cache.Get(accountsSnapshotLoclCacheKey);
            }


            return(accountsSnapshot);
        }
        public static AccountsSnapshot GetAccountsSnapshot()
        {
            var accountsSnapshot = new AccountsSnapshot();

            #region Get from Redis Cache

            //IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.PlatformManager_Multiplexer.GetDatabase();
            IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetDatabase();

            Object cachedAccountsSnapshot = null;

            try
            {
                cachedAccountsSnapshot = cache.HashGet(
                    Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Key,
                    Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Fields.Accounts
                    );

                if (((RedisValue)cachedAccountsSnapshot).HasValue)
                {
                    accountsSnapshot = JsonConvert.DeserializeObject <AccountsSnapshot>((RedisValue)cachedAccountsSnapshot);
                }
            }
            catch
            {
            }


            #endregion


            #region Generate Snapshot

            if (((RedisValue)cachedAccountsSnapshot).IsNullOrEmpty)
            {
                #region Generate Account Snapshot

                accountsSnapshot.PastDue = new List <Accounts.Models.Account>();
                accountsSnapshot.Unpaid  = new List <Accounts.Models.Account>();


                //Assign Counts
                accountsSnapshot.Counts = new PlatformSnapshotAccountCounts();


                //Account #'s
                accountsSnapshot.Counts.Total = AccountManager.GetAccountCount();

                accountsSnapshot.Counts.Unprovisioned = AccountManager.GetAccountCount("PaymentPlanName", "Unprovisioned");
                accountsSnapshot.Counts.Inactive      = AccountManager.GetAccountCount("Active", "0");

                accountsSnapshot.Counts.Delinquent = AccountManager.GetAccountCount("Delinquent", "1");
                if (accountsSnapshot.Counts.Delinquent > 0)
                {
                    var delinquentAccounts = AccountManager.GetAllAccountsByFilter("Delinquent", "1", 0, 0, "Delinquent Desc");
                    //var unpaidAccounts = AccountManager.GetAllAccountsByFilter("Delinquent", "1", 0, 0, "Delinquent Desc");

                    foreach (Account account in delinquentAccounts)
                    {
                        if (account.Active == false)
                        {
                            //Account is Delinquent AND no longer active
                            accountsSnapshot.Counts.Unpaid++;
                            accountsSnapshot.Unpaid.Add(account);
                        }
                        else
                        {
                            //Account is just Delinquent
                            accountsSnapshot.Counts.PastDue++;
                            accountsSnapshot.PastDue.Add(account);
                        }
                    }
                }

                accountsSnapshot.Counts.Subscribed = accountsSnapshot.Counts.Total - accountsSnapshot.Counts.Unprovisioned;

                //remove delinquent (unpaid and past_due) accounts from subscribed number to get the PaidUP amount:
                accountsSnapshot.Counts.PaidUp = accountsSnapshot.Counts.Subscribed - accountsSnapshot.Counts.Delinquent;


                //Signups since counts ------------------------------------------------------------------
                accountsSnapshot.Counts.Signups_Last24Hours = AccountManager.GetAccountCountCreatedSince(DateTime.UtcNow.AddDays(-1));
                accountsSnapshot.Counts.Signups_Last3Days   = AccountManager.GetAccountCountCreatedSince(DateTime.UtcNow.AddDays(-3));
                accountsSnapshot.Counts.Signups_Last7Days   = AccountManager.GetAccountCountCreatedSince(DateTime.UtcNow.AddDays(-7));
                accountsSnapshot.Counts.Signups_Last30Days  = AccountManager.GetAccountCountCreatedSince(DateTime.UtcNow.AddDays(-30));

                //User #'s -----------------------------------------------------------------------
                //accountsSnapshot.Counts.GlobalUsersCount = AccountUserManager.GetUserCount();


                //Accounts scheduled for closure and/or requiring closure approval
                var accountsPendingClosure = AccountManager.GetAccountsPendingClosure();

                accountsSnapshot.RequiresClosureApproval = new List <Account>();
                accountsSnapshot.ScheduledForClosure     = new List <Account>();
                foreach (var account in accountsPendingClosure)
                {
                    if (!account.ClosureApproved)
                    {
                        accountsSnapshot.RequiresClosureApproval.Add(account);
                    }
                    else
                    {
                        accountsSnapshot.ScheduledForClosure.Add(account);
                    }
                }

                //Latest registrations
                accountsSnapshot.LatestRegistered = new List <Account>();
                var registrationLogs = PlatformLogManager.GetPlatformLogByActivity(Logging.PlatformLogs.Types.ActivityType.Account_Registered, 10);

                foreach (var log in registrationLogs)
                {
                    var account = AccountManager.GetAccount(log.AccountID);

                    if (account == null)
                    {
                        //If the account no longer exists we create an account object with the name along with NULL accountId to indiicate that the account is likely purged, deprovisioned or closed.
                        account             = new Account();
                        account.AccountName = log.AccountName;
                        account.CreatedDate = log.Timestamp.DateTime;
                    }

                    accountsSnapshot.LatestRegistered.Add(account);
                }


                //Account closures
                accountsSnapshot.LatestClosures = new List <AccountClosure>();
                var closureLogs = PlatformLogManager.GetPlatformLogByActivity(Logging.PlatformLogs.Types.ActivityType.Account_Closed, 10);

                foreach (var log in closureLogs)
                {
                    var account = AccountManager.GetAccount(log.AccountID);

                    if (account == null)
                    {
                        //If the account no longer exists we create an account object with the name along with NULL accountId to indiicate that the account is likely purged, deprovisioned or closed.
                        account = new Account();
                        account.AccountStatus = AccountStatus.Closed;
                        account.AccountName   = log.AccountName;
                        account.CreatedDate   = log.Timestamp.DateTime;
                    }

                    accountsSnapshot.LatestClosures.Add(new AccountClosure {
                        Account = account, Decription = log.Description, Timestamp = log.Timestamp.DateTime
                    });
                }



                #region Store in Redis

                try
                {
                    //Store a copy in the Redis cache
                    cache.HashSet(
                        Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Key,
                        Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Fields.Accounts,
                        JsonConvert.SerializeObject(accountsSnapshot),
                        When.Always,
                        CommandFlags.FireAndForget
                        );

                    //Expire cache after set time
                    cache.KeyExpire(
                        Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Key,
                        Sahara.Core.Common.Redis.PlatformManagerServer.Hashes.SnapshotsHash.Expiration,
                        CommandFlags.FireAndForget
                        );
                }
                catch
                {
                }

                #endregion


                #endregion
            }


            #endregion

            return(accountsSnapshot);
        }