Exemple #1
0
        internal static FlatReportDto TimeSeriesReport(IEnumerable <Small> devices, IEnumerable <TelemetryData> telemetryDataList)
        {
            var flatReportDto = new FlatReportDto
            {
                Properties = new List <string>(),
                Rows       = new List <FlatRowDto>()
            };

            Small currentDevice = null;

            foreach (var telemetryData in telemetryDataList)
            {
                if (currentDevice == null || currentDevice.Id != telemetryData.DeviceId)
                {
                    currentDevice = new Small
                    {
                        Id   = telemetryData.DeviceId,
                        Name = devices.Single(d => d.Id == telemetryData.DeviceId).Name
                    };
                }
                var fields     = ExtractFields(telemetryData.Payload);
                var flatRowDto = new FlatRowDto
                {
                    DeviceId  = currentDevice.Id,
                    Name      = currentDevice.Name,
                    Timestamp = telemetryData.Time.ToUnixTime(),
                    Fields    = fields
                };
                flatReportDto.Rows.Add(flatRowDto);

                FillProperties(fields, flatReportDto.Properties);
            }

            return(flatReportDto);
        }
Exemple #2
0
        public static IActionResult ToHttpResponseMessage(FlatReportDto flatReport)
        {
            using (var stringWriter = new StringWriter())
            {
                stringWriter.NewLine = "\r\n";
                if (flatReport != null)
                {
                    stringWriter.WriteLine(string.Join <string>(",",
                                                                new[] { "DeviceId", "Name", "Time", }.Union(flatReport.Properties.Select(Escaped))));

                    var properties    = flatReport.Properties;
                    int propertyCount = properties.Count();

                    foreach (var row in flatReport.Rows)
                    {
                        var valueLine = Escaped(row.DeviceId) + "," + Escaped(row.Name) + "," +
                                        Escaped(row.Timestamp.ToString(CultureInfo.InvariantCulture)) +
                                        (propertyCount > 0 ? "," : "");

                        for (var p = 0; p < propertyCount; p++)
                        {
                            var prop = properties[p];

                            var field   = row.Fields.SingleOrDefault(f => f.Key == prop);
                            var escaped = Escaped(field != null ? field.Value : null);
                            valueLine = string.Concat(valueLine, escaped, p < propertyCount - 1 ? "," : "");
                        }

                        stringWriter.WriteLine(valueLine);
                    }
                }
                var fileContentResult = new FileContentResult(Encoding.UTF8.GetBytes(stringWriter.ToString()), "text/csv");
                fileContentResult.FileDownloadName = "telemetry.csv";
                return(fileContentResult);
            }
        }