Ejemplo n.º 1
0
        public List <PlatformActivityLog> GetPlatformLogByCategory(string categoryType, int maxRecords, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(null);
            }

            return(PlatformLogManager.GetPlatformLogByCategory((CategoryType)Enum.Parse(typeof(CategoryType), categoryType), maxRecords));
        }
        public static InfrastructureSnapshot GetInfrastructureSnapshot()
        {
            var infrastructureSnapshot = new InfrastructureSnapshot();

            #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 cachedInfrastructureSnapshot = null;

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

                if (((RedisValue)cachedInfrastructureSnapshot).HasValue)
                {
                    infrastructureSnapshot = JsonConvert.DeserializeObject <InfrastructureSnapshot>((RedisValue)cachedInfrastructureSnapshot);
                }
            }
            catch
            {
            }


            #endregion


            #region Generate Snapshot

            if (((RedisValue)cachedInfrastructureSnapshot).IsNullOrEmpty)
            {
                #region Generate Infrastructure Snapshot

                //Latest error data:
                infrastructureSnapshot.Errors_Log       = PlatformLogManager.GetPlatformLogByCategory(Logging.PlatformLogs.Types.CategoryType.Error, 30);
                infrastructureSnapshot.Errors_Last7Days = false;

                foreach (var log in infrastructureSnapshot.Errors_Log)
                {
                    if (log.Timestamp >= DateTime.UtcNow.AddDays(-1))
                    {
                        infrastructureSnapshot.Errors_Last24Hours = true;
                    }
                    if (log.Timestamp >= DateTime.UtcNow.AddDays(-3))
                    {
                        infrastructureSnapshot.Errors_Last3Days = true;
                    }
                    if (log.Timestamp >= DateTime.UtcNow.AddDays(-7))
                    {
                        infrastructureSnapshot.Errors_Last7Days = true;
                    }
                    if (log.Timestamp >= DateTime.UtcNow.AddDays(-30))
                    {
                        infrastructureSnapshot.Errors_Last30Days = true;
                    }
                }

                //Latest billing issue data:

                /*
                 * billingSnapshotIssues = new List<PlatformSnapshotBillingIssue>();
                 * var billingIssuesLogs = PlatformLogManager.GetPlatformLogByActivity(Logging.PlatformLogs.Types.ActivityType.Billing_Issue, 10);
                 *
                 * foreach (var log in billingIssuesLogs)
                 * {
                 *  billingSnapshotIssues.Add(
                 *      new PlatformSnapshotBillingIssue
                 *      {
                 *          Decription = log.Description,
                 *          Details = log.Details,
                 *          Timestamp = log.Timestamp.DateTime
                 *      });
                 * }*/


                //Custodian info:
                infrastructureSnapshot.Custodian = new CustodianSnapshot();
                var custodianWorkerLogs = PlatformLogManager.GetPlatformLogByCategory(Logging.PlatformLogs.Types.CategoryType.Custodian, 1);

                try
                {
                    if (custodianWorkerLogs[0].Activity == Sahara.Core.Logging.PlatformLogs.Types.ActivityType.Custodian_Sleeping.ToString())
                    {
                        infrastructureSnapshot.Custodian.IsSleeping = true;
                        infrastructureSnapshot.Custodian.IsRunning  = false;

                        infrastructureSnapshot.Custodian.LastRun = custodianWorkerLogs[0].Timestamp.UtcDateTime;
                        infrastructureSnapshot.Custodian.NextRun = custodianWorkerLogs[0].Timestamp.UtcDateTime.AddMilliseconds(Sahara.Core.Settings.Platform.Custodian.Frequency.Length);
                    }
                    else
                    {
                        infrastructureSnapshot.Custodian.IsSleeping = false;
                        infrastructureSnapshot.Custodian.IsRunning  = true;

                        infrastructureSnapshot.Custodian.LastRun = DateTime.UtcNow;
                        infrastructureSnapshot.Custodian.NextRun = DateTime.UtcNow.AddMilliseconds(Sahara.Core.Settings.Platform.Custodian.Frequency.Length);
                    }
                }
                catch
                {
                }


                infrastructureSnapshot.Custodian.FrequencyMilliseconds = Sahara.Core.Settings.Platform.Custodian.Frequency.Length;
                infrastructureSnapshot.Custodian.FrequencyDescription  = Sahara.Core.Settings.Platform.Custodian.Frequency.Description;


                #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.Infrastructure,
                        JsonConvert.SerializeObject(infrastructureSnapshot),
                        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(infrastructureSnapshot);
        }