1+ using App . ControlPanel . Engine ;
2+ using Azure . Messaging . ServiceBus ;
3+ using Common . Entities ;
4+ using Common . Entities . Config ;
5+ using Common . Entities . Redis ;
6+ using Newtonsoft . Json ;
7+ using System . Data . Entity ;
8+ using System . Linq ;
9+ using System . Threading . Tasks ;
10+
11+ namespace Web . AnalyticsWeb . Models
12+ {
13+ public class SystemStatus
14+ {
15+ #region Constructors
16+
17+ public SystemStatus ( string json )
18+ {
19+ if ( ! string . IsNullOrEmpty ( json ) )
20+ {
21+ try
22+ {
23+ this . Config = JsonConvert . DeserializeObject < SolutionInstallConfig > ( json ) ;
24+ }
25+ catch ( JsonReaderException )
26+ {
27+ // Nothing. Show no config object
28+ }
29+ }
30+
31+ this . ConfigJson = json ;
32+ }
33+ protected SystemStatus ( ) { }
34+
35+ #endregion
36+
37+ #region Props
38+
39+ public SolutionInstallConfig Config { get ; set ; }
40+
41+ public string ConfigJson { get ; set ; }
42+
43+ public int HitCount { get ; set ; }
44+ public int ActivityCount { get ; set ; }
45+
46+ public int TeamsCount { get ; set ; }
47+ public int TeamsBeingTrackedCount { get ; set ; }
48+
49+ public string BuildLabel { get ; set ; }
50+
51+ public bool HasValidConfig
52+ {
53+ get { return this . Config != null ; }
54+ }
55+
56+ public string WebAppConfigSQL { get ; set ; }
57+ public string WebAppConfigRedis { get ; set ; }
58+ public string WebAppConfigServiceBus { get ; set ; }
59+ public string WebAppConfigCognitive { get ; set ; }
60+ public bool CognitiveServiceEnabled { get ; set ; }
61+ public string WebAppBaseURL { get ; set ; }
62+ public bool YammerAuth { get ; set ; }
63+
64+ #endregion
65+
66+ internal async static Task < SystemStatus > LoadFrom ( AnalyticsEntitiesContext db , CacheConnectionManager cache )
67+ {
68+ SystemStatus status = null ;
69+
70+
71+ // Load config
72+ var latestConfig = await db . ConfigStates . OrderByDescending ( s => s . DateApplied ) . Take ( 1 ) . ToListAsync ( ) ;
73+ if ( latestConfig . Count == 1 && ! string . IsNullOrEmpty ( latestConfig [ 0 ] . ConfigJson ) )
74+ {
75+ try
76+ {
77+ status = new SystemStatus ( latestConfig [ 0 ] . ConfigJson ) ;
78+ }
79+ catch ( JsonReaderException )
80+ {
81+ status = new UnknownConfigSystemStatus ( ) ;
82+ }
83+ }
84+ else
85+ {
86+ status = new UnknownConfigSystemStatus ( ) ;
87+ }
88+
89+ status . BuildLabel = System . Configuration . ConfigurationManager . AppSettings [ "BuildLabel" ] ;
90+
91+ // DB counts
92+ status . HitCount = await db . hits . CountAsync ( ) ;
93+ status . ActivityCount = await db . AuditEventsCommon . CountAsync ( ) ;
94+ status . TeamsCount = await db . Teams . CountAsync ( ) ;
95+ status . TeamsBeingTrackedCount = await db . Teams . Where ( t => t . HasRefreshToken ) . CountAsync ( ) ;
96+
97+ // Config
98+ var config = new AppConfig ( ) ;
99+ status . WebAppConfigCognitive = config . CognitiveEndpoint ;
100+ status . WebAppConfigRedis = StackExchange . Redis . ConfigurationOptions . Parse ( config . ConnectionStrings . RedisConnectionString ) . SslHost ;
101+ status . WebAppConfigSQL = new System . Data . SqlClient . SqlConnectionStringBuilder ( config . ConnectionStrings . DatabaseConnectionString ) . DataSource ;
102+ status . WebAppConfigServiceBus = ServiceBusConnectionStringProperties . Parse ( config . ConnectionStrings . ServiceBusConnectionString ) . Endpoint . ToString ( ) ;
103+ status . CognitiveServiceEnabled = config . IsValidCognitiveConfig ;
104+ status . WebAppBaseURL = config . WebAppURL ;
105+ return status ;
106+ }
107+ }
108+
109+ public class UnknownConfigSystemStatus : SystemStatus
110+ {
111+ public UnknownConfigSystemStatus ( ) : this ( string . Empty )
112+ {
113+ }
114+ public UnknownConfigSystemStatus ( string json ) : base ( json )
115+ {
116+ base . Config = SolutionInstallConfig . NewConfig ( ) ;
117+ }
118+ }
119+ }
0 commit comments