Пример #1
0
        public ApplicationGridModule(GlobalConfig globals, IAppConfigurationManager appconf, ILogStore logStore)
        {
            if (globals.IsAuthenticationEnabled())
            {
                this.RequiresAuthentication();
            }

            Get["/", true] = async(x, ct) => {
                ApplicationGridModel model = null;
                if (DefaultCacheTimeInSeconds != 0)
                {
                    model = cache["appstats"] as ApplicationGridModel;
                }

                if (model == null)
                {
                    var applicationStatuses = await logStore.GetApplicationStatusesAsync(DateTime.UtcNow.AddMinutes(-15));

                    var allApplications = await appconf.GetAppsAsync();

                    var activeServers             = new SortedSet <string>();
                    var activeApplications        = new Dictionary <string, Application>();
                    var activeApplicationStatuses = new SortedDictionary <string, IDictionary <string, LastApplicationStatus> >();
                    foreach (var appStatus in applicationStatuses)
                    {
                        activeServers.Add(appStatus.Server);

                        var app = allApplications.FirstOrDefault(a => string.Equals(a.Path, appStatus.ApplicationPath,
                                                                                    StringComparison.InvariantCultureIgnoreCase));
                        if (app != null && !app.IsExcluded)
                        {
                            if (!activeApplications.ContainsKey(app.Path))
                            {
                                activeApplications.Add(app.Path, app);
                            }
                            IDictionary <string, LastApplicationStatus> applicationStatusPerServer;
                            if (!activeApplicationStatuses.TryGetValue(app.Path, out applicationStatusPerServer))
                            {
                                applicationStatusPerServer = new Dictionary <string, LastApplicationStatus>(StringComparer.OrdinalIgnoreCase);
                                activeApplicationStatuses.Add(app.Path, applicationStatusPerServer);
                            }
                            applicationStatusPerServer.Add(appStatus.Server, appStatus);
                        }
                    }
                    // for the rest of the applications
                    foreach (var app in allApplications)
                    {
                        if (!app.IsExcluded)
                        {
                            if (!activeApplications.ContainsKey(app.Path))
                            {
                                activeApplications.Add(app.Path, app);
                            }
                            if (!activeApplicationStatuses.ContainsKey(app.Path))
                            {
                                activeApplicationStatuses.Add(app.Path, new Dictionary <string, LastApplicationStatus>(StringComparer.OrdinalIgnoreCase));
                            }
                        }
                    }

                    model = new ApplicationGridModel {
                        LastUpdateTime      = DateTime.Now,
                        Servers             = activeServers.ToArray(),
                        Applications        = activeApplications,
                        ApplicationStatuses = activeApplicationStatuses
                    };
                    if (DefaultCacheTimeInSeconds != 0)
                    {
                        cache.Set("appstats", model, new CacheItemPolicy()
                        {
                            AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(DefaultCacheTimeInSeconds)
                        });
                    }
                }
                return(View["ApplicationGrid.cshtml", model]);
            };

            Get["/apps", true] = async(x, ct) => {
                // gets applications for which we have received the logs
                return(View["Applications", (await appconf.GetAppsAsync()).Where(app => !app.IsHidden).Select(
                                app => {
                    app.DaysToKeepLogs = app.DaysToKeepLogs ?? AppSettings.DefaultNoOfDaysToKeepLogs;
                    return app;
                })]);
            };
        }