aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Insights/InsightsFrame.cs
blob: 5212b5471a238105969edb4486423f7af8e50f96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Diagnostics;
using Tango.PMR.Insights;
using Tango.Transport.Compression;

namespace Tango.Insights
{
    public class InsightsFrame
    {
        [BsonId(true)]
        public int Id { get; set; }
        public DateTime Time { get; set; }
        public bool IsEmpty { get; set; }
        public byte[] MonitorsData { get; set; }

        public static InsightsFrame CreateEmpty(DateTime timeUtc)
        {
            InsightsFrame frame = new InsightsFrame();
            frame.Time = timeUtc;
            frame.IsEmpty = true;
            frame.MonitorsData = null;
            return frame;
        }

        public static InsightsFrame FromDiagnosticsMonitors(DiagnosticsMonitors diagnosticsMonitors, DateTime timeUtc)
        {
            InsightsMonitors insightsMonitors = InsightsHelper.MapMonitors(diagnosticsMonitors);
            return FromInsightsMonitors(insightsMonitors, timeUtc);
        }

        public static InsightsFrame FromInsightsMonitors(InsightsMonitors insightsMonitors, DateTime timeUtc)
        {
            InsightsFrame frame = new InsightsFrame();
            frame.Time = timeUtc;

            byte[] data = insightsMonitors.ToBytes();
            byte[] compressedData = GZipHelper.Compress(data);

            frame.MonitorsData = compressedData;

            return frame;
        }

        public InsightsMonitors ToInsightsMonitors()
        {
            if (IsEmpty)
            {
                return InsightsHelper.CreateEmptyGap();
            }
            else
            {
                var uncompressed = GZipHelper.Decompress(MonitorsData);
                var insightsMonitors = InsightsMonitors.Parser.ParseFrom(uncompressed);
                return insightsMonitors;
            }
        }
    }
}