blob: 2ceb952989d56f65cf442cc794a271d4efafaea6 (
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
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
|
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Telemetry
{
public class TelemetryLiteDBStorageManager : ITelemetryStorageManager
{
private bool _disposed;
private LiteDatabase _database;
private static Object _lock = new object();
public String DatabasePath { get; private set; }
public TelemetryLiteDBStorageManager()
{
DatabasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Telemetry", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".telemetry");
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 && !_disposed)
{
try
{
_disposed = true;
_database.Dispose();
_database = null;
}
catch { }
}
}
~TelemetryLiteDBStorageManager()
{
Dispose();
}
private ILiteCollection<PendingTelemetry> GetPendingTelemetriesCollection()
{
return _database.GetCollection<PendingTelemetry>("PendingTelemetries");
}
private ILiteCollection<TelemetryHistorySourceCheckPoint> GetSourcesCheckpointCollection()
{
return _database.GetCollection<TelemetryHistorySourceCheckPoint>("SourcesCheckPoints");
}
public void UpsertPendingTelemetry(PendingTelemetry pendingTelemetry)
{
lock (_lock)
{
var collection = GetPendingTelemetriesCollection();
collection.Upsert(pendingTelemetry);
}
}
public void DeletePendingTelemetry(PendingTelemetry pendingTelemetry)
{
lock (_lock)
{
var collection = GetPendingTelemetriesCollection();
collection.Delete(pendingTelemetry.Id);
}
}
public List<PendingTelemetry> GetPendingTelemetries(int maxCount)
{
lock (_lock)
{
var collection = GetPendingTelemetriesCollection();
return collection.FindAll().OrderBy(x => x.TelemetryObject.Time).Take(Math.Max(maxCount, 1)).ToList();
}
}
public TelemetryHistorySourceCheckPoint GetHistorySourceCheckPoint(ITelemetryHistorySource source)
{
lock (_lock)
{
var collection = GetSourcesCheckpointCollection();
return collection.FindOne(x => x.SourceName == source.Name);
}
}
public void SetHistorySourceCheckPoint(ITelemetryHistorySource source, DateTime time, int totalCount)
{
lock (_lock)
{
var collection = GetSourcesCheckpointCollection();
collection.Upsert(new TelemetryHistorySourceCheckPoint() { SourceName = source.Name, Time = time, TotalCount = totalCount });
}
}
public int GetPendingTelemetriesCount()
{
lock (_lock)
{
var collection = GetPendingTelemetriesCollection();
return collection.Count();
}
}
}
}
|