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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
using LiteDB;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Enumerations;
namespace Tango.Insights
{
public class InsightsManager : IDisposable
{
private const string INSIGHTS_COLLECTION = "Insights";
private const string STATUSES_COLLECTION = "Statuses";
private const string APPLICATION_EXCEPTIONS_COLLECTION = "ApplicationExceptions";
private bool _disposed;
private static InsightsManager _instance;
public static InsightsManager Default
{
get
{
if (_instance == null)
{
_instance = new InsightsManager(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Insights", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".insights"));
}
return _instance;
}
}
private LiteDatabase _database;
public String DatabasePath { get; private set; }
private InsightsManager(String databasePath)
{
DatabasePath = databasePath;
Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
_database = new LiteDatabase($"Filename={DatabasePath}");
_database.Pragma("TIMEOUT", 10); //Read Timeout
_database.Pragma("UTC_DATE", true); //Keep time as UTC when getting data
_database.Commit();
}
public virtual void Dispose()
{
if (_database != null)
{
try
{
_disposed = true;
_database.Dispose();
_database = null;
}
catch { }
}
}
~InsightsManager()
{
Dispose();
}
private ILiteCollection<InsightsFrame> GetInsightsCollection()
{
return _database.GetCollection<InsightsFrame>(INSIGHTS_COLLECTION);
}
private ILiteCollection<InsightsStatus> GetStatusesCollection()
{
return _database.GetCollection<InsightsStatus>(STATUSES_COLLECTION);
}
private ILiteCollection<InsightsApplicationException> GetApplicationExceptionsCollection()
{
return _database.GetCollection<InsightsApplicationException>(APPLICATION_EXCEPTIONS_COLLECTION);
}
public virtual void InsertFrame(InsightsFrame frame)
{
if (_disposed) return;
var collection = GetInsightsCollection();
collection.Insert(frame);
}
public virtual void InsertStatus(InsightsStatus status)
{
if (_disposed) return;
var collection = GetStatusesCollection();
collection.Insert(status);
}
public virtual void InsertApplicationException(InsightsApplicationException appException)
{
if (_disposed) return;
var collection = GetApplicationExceptionsCollection();
appException.Time = appException.Time.Subtract(TimeSpan.FromSeconds(30));
collection.Insert(appException);
}
public virtual List<InsightsFrame> GetFrames(DateTime startUTC, DateTime endUTC)
{
var collection = GetInsightsCollection();
return collection.Find(x => x.Time >= startUTC && x.Time <= endUTC).ToList().OrderBy(x => x.Time).ToList();
}
public virtual List<InsightsStatus> GetStatuses(DateTime startUTC, DateTime endUTC)
{
var collection = GetStatusesCollection();
return collection.Find(x => x.Time >= startUTC && x.Time <= endUTC).ToList().OrderBy(x => x.Time).ToList();
}
public virtual List<InsightsApplicationException> GetApplicationExceptions(DateTime startUTC, DateTime endUTC)
{
var collection = GetApplicationExceptionsCollection();
return collection.Find(x => x.Time >= startUTC && x.Time <= endUTC).ToList().OrderBy(x => x.Time).ToList();
}
public virtual int DeleteFrames(DateTime maxDateUTC)
{
if (_disposed) return 0;
var collection = GetInsightsCollection();
return collection.DeleteMany(x => x.Time < maxDateUTC);
}
public virtual int DeleteStatuses(DateTime maxDateUTC)
{
if (_disposed) return 0;
var collection = GetStatusesCollection();
return collection.DeleteMany(x => x.Time < maxDateUTC);
}
public virtual int DeleteApplicationExceptions(DateTime maxDateUTC)
{
if (_disposed) return 0;
var collection = GetApplicationExceptionsCollection();
return collection.DeleteMany(x => x.Time < maxDateUTC);
}
public DateTime? GetFramesMinDate()
{
if (_disposed) return null;
var collection = GetInsightsCollection();
if (collection.Count() > 0)
{
return collection.Min(x => x.Time);
}
else
{
return null;
}
}
public virtual List<InsightsEvent> GetEvents(DateTime startUTC, DateTime endUTC, bool limitToMinFramesTime = true)
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
List<InsightsEvent> events = db.MachinesEvents
.Where(x => x.DateTime >= startUTC && x.DateTime <= endUTC)
.Include(x => x.EventType)
.Select(x => new InsightsEvent()
{
Time = x.DateTime,
EventCode = x.EventType.Code,
Description = x.Description
})
.ToList()
.Where(x => (EventTypes)x.EventCode != EventTypes.APPLICATION_EXCEPTION)
.Select(x => new InsightsEvent()
{
Time = x.Time,
EventCode = x.EventCode,
Description = ((EventTypes)x.EventCode == EventTypes.JOB_FAILED ? x.Description : null)
})
.OrderBy(x => x.Time)
.ToList();
if (limitToMinFramesTime)
{
var collection = GetInsightsCollection();
if (collection.Count() > 0)
{
var minTime = collection.Min(x => x.Time);
events.RemoveAll(x => x.Time < minTime);
}
}
return events;
}
}
}
}
|