/
ResampledDataContainer.cs
128 lines (104 loc) · 4.34 KB
/
ResampledDataContainer.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using FileHelpers;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace MongoDBDatasetConverter
{
class ResampledDataContainer : DataContainer
{
private readonly Dictionary<string, List<ResampledData>> _collectionsContent = new Dictionary<string, List<ResampledData>>();
public ResampledDataContainer(string connectionString, string databaseName) : base(connectionString, databaseName)
{
CheckDatabaseName(databaseName);
}
public ResampledDataContainer(string databaseName) : base(databaseName)
{
CheckDatabaseName(databaseName);
}
private void CheckDatabaseName(string databaseName)
{
if (!Regex.IsMatch(databaseName, @"^[a-zA-Z0-9]*$"))
{
throw new NotImplementedException("Invalid String Format");
}
}
public ResampledData[] Content { get; set; }
public override async Task ConvertToCsv(string collectionName, string outputFileName)
{
if (!Regex.IsMatch(collectionName, @"^[a-zA-Z0-9]*$"))
{
throw new NotImplementedException("Invalid String Format");
}
_collectionsContent.Add(collectionName, new List<ResampledData>());
Console.WriteLine(collectionName);
var collection = Database.GetCollection<BsonDocument>(collectionName);
var projection = Builders<BsonDocument>.Projection.Exclude("_id");
var documents = await collection.Find(new BsonDocument()).Project(projection).ToListAsync();
using (StreamWriter file = new StreamWriter(outputFileName))
{
List<List<double?>> matrix = new List<List<double?>>();
foreach (var document in documents)
{
ResampledData localContent = new ResampledData(document);
string s = localContent.ToStringIgnoreTime();
//avoid null enties at begining
if (s.Contains("null") && _collectionsContent[collectionName].Count == 0)
{
Console.WriteLine("line null at begining");
}
else
{
matrix.Add(localContent.getSensorData());
_collectionsContent[collectionName].Add(localContent);
}
}
int nbRows = matrix.Count;
int nbColumns = matrix[0].Count;
for (int i = 0; i < nbColumns; i++)
{
List<double?> temp = new List<double?>();
for (int j = 0; j < nbRows; j++)
{
temp.Add(matrix[j][i]);
}
temp.Smooth();
for (int j = 0; j < nbRows; j++)
{
matrix[j][i] = temp[j];
}
}
for (int index = 0; index < matrix.Count; index++)
{
var row = matrix[index];
_collectionsContent[collectionName][index].SetSensorDatas(row);
file.WriteLine(_collectionsContent[collectionName][index].ToStringIgnoreTime());
}
Console.WriteLine();
}
}
public override async Task ConvertAllCollectionsToCsv(string dir)
{
using (var cursor = await Database.ListCollectionsAsync())
{
var collections = await cursor.ToListAsync();
var query = from e in collections.AsQueryable()
where e["name"].ToString().Contains("_raw") == false
select e;
foreach (var collection in query)
{
var collectionName = collection["name"].ToString();
var localCsvFileName = dir + collectionName + ".csv";
await ConvertToCsv(collectionName, localCsvFileName);
}
}
}
}
}