Пример #1
0
        public async Task Dispatch(DashboardContext context)
        {
            var requestedMetrics = await context.Request.GetFormValuesAsync("metrics[]").ConfigureAwait(false);

            var page = new StubPage();

            page.Assign(context);

            var metrics = DashboardMetrics.GetMetrics().Where(x => requestedMetrics.Contains(x.Name));
            var result  = new Dictionary <string, Metric>();

            foreach (var metric in metrics)
            {
                var value = metric.Func(page);
                result.Add(metric.Name, value);
            }

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Converters       = new JsonConverter[] { new StringEnumConverter {
                                                             CamelCaseText = true
                                                         } }
            };
            var serialized = JsonConvert.SerializeObject(result, settings);

            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(serialized).ConfigureAwait(false);
        }
Пример #2
0
        public async Task Dispatch(RequestDispatcherContext context)
        {
            var owinContext = new OwinContext(context.OwinEnvironment);
            var form        = await owinContext.ReadFormSafeAsync();

            var requestedMetrics = new HashSet <string>(form.GetValues("metrics[]") ?? new string[0]);

            var page = new StubPage();

            page.Assign(context);

            var metrics = DashboardMetrics.GetMetrics().Where(x => requestedMetrics.Contains(x.Name));
            var result  = new Dictionary <string, Metric>();

            foreach (var metric in metrics)
            {
                var value = metric.Func(page);
                result.Add(metric.Name, value);
            }

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Converters       = new JsonConverter[] { new StringEnumConverter {
                                                             CamelCaseText = true
                                                         } }
            };
            var serialized = JsonConvert.SerializeObject(result, settings);

            owinContext.Response.ContentType = "application/json";
            await owinContext.Response.WriteAsync(serialized);
        }