Exemple #1
0
        public async Task <IActionResult> GetLogFileLines(string date, CancellationToken ct, string level = null, [Bind(Prefix = "class")] string classname = null, DataFileFormats format = DataFileFormats.JSON)
        {
            if (!string.IsNullOrWhiteSpace(classname))
            {
                classname = classname.Trim();
            }
            var fullclassnames = RemoveClassPrefixList.Select(prefix => prefix + classname).ToList();

            IEnumerable <LogEntry> data = null;

            if (UseAzureStorage)
            {
                var cloud = ConnectToStorage();

                var startdate = DateTime.Parse(date);
                var enddate   = startdate.AddDays(1);

                var maxdatetick = (DateTime.MaxValue.Ticks - startdate.Ticks + 1).ToString("D19");
                var mindatetick = (DateTime.MaxValue.Ticks - enddate.Ticks + 1).ToString("D19");

                /*
                 * var query = cloud.CreateQuery<LogEntity>().Where(log => log.PartitionKey.CompareTo(mindatetick) >= 0 && log.PartitionKey.CompareTo(maxdatetick) <= 0);
                 * if (!string.IsNullOrWhiteSpace(level)) query = query.Where(log => log.Level == level);
                 *
                 * // WARNING - Change this query if RemoveClassPrefixList is modified
                 * if (!string.IsNullOrWhiteSpace(classname)) query = query.Where(log => log.LoggerName == classname || log.LoggerName == fullclassnames[0] || log.LoggerName == fullclassnames[1]);
                 *
                 * data = query.AsEnumerable().Select(log => new LogEntry(log)).ToList();
                 */

                var filter = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition(nameof(LogEntity.PartitionKey), QueryComparisons.GreaterThan, mindatetick),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition(nameof(LogEntity.PartitionKey), QueryComparisons.LessThanOrEqual, maxdatetick)
                    );

                if (!string.IsNullOrWhiteSpace(level))
                {
                    filter = TableQuery.CombineFilters(
                        filter,
                        TableOperators.And,
                        TableQuery.GenerateFilterCondition(nameof(LogEntity.Level), QueryComparisons.Equal, level)
                        );
                }
                else
                {
                    filter = TableQuery.CombineFilters(
                        filter,
                        TableOperators.And,
                        TableQuery.GenerateFilterCondition(nameof(LogEntity.Level), QueryComparisons.NotEqual, "DEBUG")
                        );
                }

                if (!string.IsNullOrWhiteSpace(classname))
                {
                    var cfilter = TableQuery.GenerateFilterCondition(nameof(LogEntity.LoggerName), QueryComparisons.Equal, classname);

                    foreach (var fullclassname in fullclassnames)
                    {
                        cfilter = TableQuery.CombineFilters(
                            cfilter,
                            TableOperators.Or,
                            TableQuery.GenerateFilterCondition(nameof(LogEntity.LoggerName), QueryComparisons.Equal, fullclassname)
                            );
                    }

                    filter = TableQuery.CombineFilters(filter, TableOperators.And, cfilter);
                }

                var tquery = new TableQuery <LogEntity>().Where(filter);

                var list = await cloud.ExecuteQueryAsync <LogEntity>(tquery).ConfigureAwait(false);

                data = list.AsEnumerable().Select(log => new LogEntry(log)).ToList();
            }

            if (UseLogFiles)
            {
                var(_, lines) = await ReadLogFileAsync(date, ct).ConfigureAwait(false);

                if (lines == null)
                {
                    data = new LogEntry[0];
                }
                else
                {
                    var query = lines.Select(line => LogLineRegex.Match(line)).Where(match => match.Success);

                    // If line match unsuccessful, then it is an error dump
                    if (!string.IsNullOrWhiteSpace(level))
                    {
                        level = level.Trim();
                        query = query.Where(match => match.Groups["level"].Value.Equals(level, StringComparison.OrdinalIgnoreCase));
                    }
                    else
                    {
                        // If level is null, then get all except DEBUG
                        query = query.Where(match => !match.Groups["level"].Value.Equals("DEBUG", StringComparison.OrdinalIgnoreCase));
                    }

                    if (!string.IsNullOrWhiteSpace(classname))
                    {
                        query = query.Where(match =>
                                            match.Groups["class"].Value == classname ||
                                            fullclassnames.Contains(match.Groups["class"].Value));
                    }

                    data = query.Select(match => {
                        var classtext = match.Groups["class"].Value.Trim();
                        classtext     = classtext.Substring(RemoveClassPrefixList.FirstOrDefault(prefix => classtext.StartsWith(prefix))?.Length ?? 0);

                        return(new LogEntry()
                        {
                            Time = DateTimeOffset.Parse($"{match.Groups["date"].Value}T{match.Groups["time"].Value.Replace(",", ".")}", null, DateTimeStyles.AssumeLocal),
                            Level = match.Groups["level"].Value.Trim(),
                            Class = classtext,
                            Thread = match.Groups["thread"].Value.Trim(),
                            Message = match.Groups["message"].Value
                        });
                    }).ToList();
                }
            }

            if (data == null)
            {
                throw new ApplicationException();
            }

            // Download log entries

            switch (format)
            {
            case DataFileFormats.JSON: return(Json(data.OrderByDescending(log => log.Time)));

            case DataFileFormats.CSV: return(Content(BuildCSVFile(LogEntry.HeaderNames, data), "text/csv", Encoding.UTF8));

            case DataFileFormats.TSV: return(Content(BuildCSVFile(LogEntry.HeaderNames, data, "\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()) {
                    BuildXLSFile(stream, xls, "Logs", LogEntry.HeaderNames, data);
                    var filedata = stream.ToArray();
                    return(File(filedata, mime, "logs" + ext));
                }
            }

            default: throw new ApplicationException();
            }
        }
Exemple #2
0
		public async Task<IActionResult> GetAlarmsAsync (uint controllerId, string alarm, DateTimeOffset? from = null, DateTimeOffset? to = null, string sort = "time", DataFileFormats format = DataFileFormats.JSON, double timezone = 0.0)
			=> await WebDataDownloadAsync<AlarmX>(Storage.AlarmsTable, "Alarms", HttpContext.GetOrg(), controllerId, from, to, Utils.GetSorting(sort), new KeyEqualsPredicate<AlarmX>(alarm, AlarmX.DatabaseKeyField), format, timezone);
        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();
            }
        }
Exemple #4
0
 public async Task <IActionResult> GetAuditTrailAsync(uint controllerId, DateTimeOffset?from = null, DateTimeOffset?to = null, string sort = "time", DataFileFormats format = DataFileFormats.JSON, double timezone = 0.0)
 => await WebDataDownloadAsync <AuditTrailX>(Storage.AuditTrailTable, "AuditTrail", HttpContext.GetOrg(), controllerId, from, to, Utils.GetSorting(sort), null, format, timezone);
Exemple #5
0
 public async Task <IActionResult> GetAllEventsAsync(DateTimeOffset?from = null, DateTimeOffset?to = null, string sort = "time", DataFileFormats format = DataFileFormats.JSON, double timezone = 0.0)
 => await GetEventsAsync(0, from, to, sort, format, timezone);
Exemple #6
0
        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();
            }
        }