private void BuildResponse_SaveAsExcel(HttpContext context, EM_BackEndResponder beResponder) { try { EM_StatisticsBackEndResponder me = beResponder as EM_StatisticsBackEndResponder; if (resultsPackages == null || resultsPackages.Count == 0) { BackEnd.WriteResponseError(context, "No files for analysing defined!"); return; } List <ResultsPackage> allResults = new List <ResultsPackage>(); if (context.Request.Query.ContainsKey("packageKey")) { string packageKey = context.Request.Query["packageKey"].First().ToString(); ResultsPackage resultsPackage = (from rp in resultsPackages.ToList() where rp.packageKey == packageKey select rp).FirstOrDefault(); // .ToList() here is extremely important for thread-safety if (resultsPackage == null) { BackEnd.WriteResponseError(context, "Package not found!"); return; } allResults.Add(resultsPackage); } else { allResults.AddRange(resultsPackages.ToList()); } if (allResults.Count < 1) { BackEnd.WriteResponseError(context, "No Results found!"); return; } bool allReady = true; foreach (ResultsPackage resultsPackage in allResults) { if (resultsPackage.displayResults == null || !resultsPackage.displayResults.calculated) { allReady = false; } } if (allReady) { MemoryStream excelStream; string errMsg = string.Empty; foreach (ResultsPackage resultsPackage in allResults) { errMsg += (errMsg == string.Empty ? "" : Environment.NewLine) + resultsPackage.error; } context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; if (allResults.Count == 1) { context.Response.Headers.Append("Content-Disposition", $"attachment; filename={allResults[0].displayResults.info.button}.xlsx"); ExportHandling.ExportSinglePackage(allResults[0].displayResults, out errMsg, out excelStream); } else { context.Response.Headers.Append("Content-Disposition", $"attachment; filename=EM_Statistics_export.xlsx"); List <DisplayResults> allDisplayResults = new List <DisplayResults>(); foreach (ResultsPackage resultsPackage in allResults) { allDisplayResults.Add(resultsPackage.displayResults); } ExportHandling.ExportMultiPackages(allDisplayResults, out errMsg, out excelStream); } context.Response.Body.WriteAsync(excelStream.ToArray(), 0, (int)excelStream.Length); return; } else { System.Threading.Thread.Sleep(500); BuildResponse_SaveAsExcel(context, beResponder); return; } } catch (Exception exception) { BackEnd.WriteResponseError(context, exception.Message); } }
private void BuildResponse_LoadStatistics(HttpContext context, EM_BackEndResponder beResponder) { try { EM_StatisticsBackEndResponder me = beResponder as EM_StatisticsBackEndResponder; context.Response.ContentType = "text/json"; if (resultsPackages == null || resultsPackages.Count == 0) { BackEnd.WriteResponseError(context, "No files for analysing defined!"); return; } bool hasForm = context.Request.HasFormContentType && context.Request.Form.Count > 0; string packageKey = hasForm && context.Request.Form.ContainsKey("packageKey") ? context.Request.Form["packageKey"].ToString() : resultsPackages[0].packageKey; ResultsPackage resultsPackage = (from rp in resultsPackages.ToList() where rp.packageKey == packageKey select rp).FirstOrDefault(); // .ToList() here is extremely important for thread-safety // package does not yet exist! if (resultsPackage == null) { BackEnd.WriteResponseError(context, "No Results?!? This should never happen!"); return; // FilePackageContent filePackage = (from fp in filePackages where fp.Key == packageKey select fp).FirstOrDefault(); // if (filePackage == null) { BackEnd.WriteResponseError(context, $"Unknown package-key: '{packageKey}'!"); return; } // resultsPackage = CalcPackage(filePackage); resultsPackages.Add(resultsPackage); } // statistics not yet calculated if (!resultsPackage.displayResults.calculated) { DisplayResults results = new DisplayResults() { info = resultsPackage.displayResults.info }; LoadInfo loadInfo = new LoadInfo() { packageKey = packageKey, completed = false, results = results }; context.Response.WriteAsync(JsonConvert.SerializeObject(loadInfo)); return; } if (resultsPackage.displayResults != null) { LoadInfo loadInfo = new LoadInfo() { packageKey = packageKey, completed = true }; string json = ""; lock (resultsPackages) { loadInfo.results = resultsPackage.displayResults; loadInfo.warnings = resultsPackage.error; json = JsonConvert.SerializeObject(loadInfo); } context.Response.WriteAsync(json); } else { BackEnd.WriteResponseError(context, resultsPackage.error); } } catch (Exception exception) { BackEnd.WriteResponseError(context, exception.Message); } }