Permalink
Browse files

GZIP!

IIS won't do it, save yourselves! I don't care what the client asks for here.
  • Loading branch information...
Nick Craver
Nick Craver committed Oct 31, 2018
1 parent 048bf4f commit 8a5f9509de02469710e3a23b656fe6294bfa5e25
Showing with 18 additions and 3 deletions.
  1. +18 −3 Opserver/Controllers/GraphController.Spark.cs
@@ -2,9 +2,12 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using StackExchange.Opserver.Data.Dashboard;
using StackExchange.Opserver.Data.SQL;
@@ -157,8 +160,11 @@ public ActionResult SQLCPUSpark(string node)
return SparkSVG(points, 100, p => p.ProcessUtilization, start);
}
public static async Task<ActionResult> SparkSvgAll<T>(string cacheKey, TimeSpan cacheDuration, Func<Node, Task<List<T>>> getPoints, Func<Node, List<T>, long> getMax, Func<T, double> getVal) where T : IGraphPoint
private async Task<ActionResult> SparkSvgAll<T>(string cacheKey, TimeSpan cacheDuration, Func<Node, Task<List<T>>> getPoints, Func<Node, List<T>, long> getMax, Func<T, double> getVal) where T : IGraphPoint
{
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");
var cached = Current.LocalCache.Get<byte[]>(cacheKey);
if (cached != null)
{
@@ -233,8 +239,17 @@ public ActionResult SQLCPUSpark(string node)
sb.Append("</svg>");
var bytes = Encoding.UTF8.GetBytes(sb.ToStringRecycle());
Current.LocalCache.Set(cacheKey, bytes, cacheDuration);
return new FileContentResult(bytes, "image/svg+xml");
using (var outputStream = new MemoryStream())
{
using (var gZipStream = new GZipStream(outputStream, CompressionMode.Compress))
{
gZipStream.Write(bytes, 0, bytes.Length);
}
var zippedBytes = outputStream.ToArray();
Current.LocalCache.Set(cacheKey, zippedBytes, cacheDuration);
return new FileContentResult(zippedBytes, "image/svg+xml");
}
}
private static FileResult SparkSVG<T>(IEnumerable<T> points, long max, Func<T, double> getVal, DateTime? start = null) where T : IGraphPoint

0 comments on commit 8a5f950

Please sign in to comment.