public async Task <IActionResult> WebDataDownloadAsync <T> (string table, string title, string orgId, uint controllerId, DateTimeOffset?from = null, DateTimeOffset?to = null, Sorting sort = Sorting.ByTime, IPredicate <T> filter = null, DataFileFormats format = DataFileFormats.JSON, double timezone = 0.0) where T : EntryBase, IDataFileFormatConverter { IEnumerable <T> data; try { (from, to) = Utils.ProcessDateTimeRange(from, to); using (var db = new ConfigDB()) { using (var ana = new AnalyticsEngine(db)) { data = await ana.GetDataAsync <T>(table, from.Value, to.Value, filter, sort, orgId, controllerId); if (data == null) { return(NotFound()); } } } } catch (ArgumentException ex) { return(BadRequest(ex.ToString())); } // Encode result string[] headers = null; string filename = "data"; if (format != DataFileFormats.JSON) { if (typeof(T) == typeof(EventX)) { headers = EventX.Headers; filename = "events"; } else if (typeof(T) == typeof(AlarmX)) { headers = AlarmX.Headers; filename = "alarms"; } else if (typeof(T) == typeof(AuditTrailX)) { headers = AuditTrailX.Headers; filename = "audit"; } else if (typeof(T) == typeof(CycleDataX)) { headers = CycleDataX.Headers.Concat((data as IEnumerable <CycleDataX>).SelectMany(x => x.Data.Keys).Distinct()).ToArray(); filename = "cycledata"; } else { throw new ApplicationException(); } } switch (format) { case DataFileFormats.JSON: return(Json(data)); case DataFileFormats.CSV: return(Content(DataFileGenerator.BuildCSVFile(headers, data, timezone), "text/csv", Encoding.UTF8)); case DataFileFormats.TSV: return(Content(DataFileGenerator.BuildCSVFile(headers, data, timezone, "\t", false), "text/csv", Encoding.UTF8)); case DataFileFormats.XLS: case DataFileFormats.XLSX: { IWorkbook xls; string mime; string ext; switch (format) { case DataFileFormats.XLS: xls = new HSSFWorkbook(); ext = ".xls"; mime = "application/vnd.ms-excel"; break; case DataFileFormats.XLSX: xls = new XSSFWorkbook(); ext = ".xlsx"; mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; default: throw new ApplicationException(); } using (var stream = new MemoryStream()) { DataFileGenerator.BuildXLSFile(stream, xls, title, headers, data, timezone); var filedata = stream.ToArray(); return(File(filedata, mime, filename + ext)); } } default: throw new ApplicationException(); } }
public async Task <IActionResult> GetCycleDataVariableAsync(uint controllerId, string variable, DateTimeOffset?from = null, DateTimeOffset?to = null, DataFileFormats format = DataFileFormats.JSON, double timezone = 0.0) { if (string.IsNullOrWhiteSpace(variable)) { return(BadRequest($"Invalid variable name: {variable}")); } IEnumerable <CycleDataX> result; try { (from, to) = Utils.ProcessDateTimeRange(from, to); using (var db = new ConfigDB()) { using (var ana = new AnalyticsEngine(db)) { result = await ana.GetDataAsync <CycleDataX>(Storage.CycleDataTable, from.Value, to.Value, null, Sorting.ByTime, HttpContext.GetOrg(), controllerId, variable); if (result == null) { return(NotFound()); } } } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } var data = result.AsParallel().Select(x => new TimeValue <double>(x.Time, x.ContainsKey(variable) ? x[variable] : 0.0)).ToList(); // Encode result switch (format) { case DataFileFormats.JSON: return(Json(data)); case DataFileFormats.CSV: return(Content(DataFileGenerator.BuildCSVFile(TimeValue <double> .Headers, data, timezone), "text/csv", Encoding.UTF8)); case DataFileFormats.TSV: return(Content(DataFileGenerator.BuildCSVFile(TimeValue <double> .Headers, data, timezone, "\t", false), "text/csv", Encoding.UTF8)); case DataFileFormats.XLS: case DataFileFormats.XLSX: { IWorkbook xls; string mime; string ext; switch (format) { case DataFileFormats.XLS: xls = new HSSFWorkbook(); ext = ".xls"; mime = "application/vnd.ms-excel"; break; case DataFileFormats.XLSX: xls = new XSSFWorkbook(); ext = ".xlsx"; mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; default: throw new ApplicationException(); } using (var stream = new MemoryStream()) { DataFileGenerator.BuildXLSFile(stream, xls, variable, TimeValue <double> .Headers, data, timezone); var filedata = stream.ToArray(); return(File(filedata, mime, variable + ext)); } } default: throw new ApplicationException(); } }