1
|
using System;
|
2
|
using System.Collections.Generic;
|
3
|
using System.IO;
|
4
|
using ServerApp.Parser.InputData;
|
5
|
using ServerApp.Parser.OutputInfo;
|
6
|
|
7
|
namespace ServerApp.Parser.Parsers
|
8
|
{
|
9
|
/// <summary>
|
10
|
/// Class parsing jis files into instances of ActivityInfo divided by given time interval
|
11
|
/// Data parsed from 7am (included) to 18pm (included)
|
12
|
/// </summary>
|
13
|
class JisParser
|
14
|
{
|
15
|
|
16
|
/// <summary>
|
17
|
/// Parses jis data to ActivityInfo instances
|
18
|
/// Data parsed from 7am (included) to 18pm (included)
|
19
|
/// </summary>
|
20
|
/// <param name="folder">Folder with login data files</param>
|
21
|
/// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
|
22
|
/// <param name="interval">Time interval to divide days by, minimum is 1h</param>
|
23
|
/// <returns></returns>
|
24
|
public List<ActivityInfo> ParseJisData(string folder, bool wholeDay = true, int interval = 1)
|
25
|
{
|
26
|
if (!Directory.Exists(folder))
|
27
|
return null;
|
28
|
|
29
|
List<ActivityInfo> list = new List<ActivityInfo>();
|
30
|
|
31
|
// parse all files
|
32
|
string[] fileEntries = Directory.GetFiles(folder);
|
33
|
foreach (string fileName in fileEntries)
|
34
|
{
|
35
|
List<ActivityInfo> loadedData = null;
|
36
|
|
37
|
// parse as one instance per day
|
38
|
if (wholeDay)
|
39
|
loadedData = ProcessOneJisFileAsDays(fileName);
|
40
|
// parse by interval length
|
41
|
else
|
42
|
loadedData = ProcessOneJisFileAsIntervals(fileName, interval);
|
43
|
|
44
|
list.AddRange(loadedData);
|
45
|
}
|
46
|
|
47
|
return list;
|
48
|
}
|
49
|
|
50
|
/// <summary>
|
51
|
/// Parses data from one data file as one instance per day
|
52
|
/// </summary>
|
53
|
/// <param name="path">Path ti file</param>
|
54
|
/// <returns>List with ActivityInfo instances</returns>
|
55
|
private static List<ActivityInfo> ProcessOneJisFileAsDays(string path)
|
56
|
{
|
57
|
if (!File.Exists(path))
|
58
|
return null;
|
59
|
|
60
|
List<ActivityInfo> jisInfo = new List<ActivityInfo>();
|
61
|
List<JisInstance> list = CsvDataLoader.LoadJisFile(path);
|
62
|
|
63
|
// data for each faculty
|
64
|
int[] recordedAmount = new int[TagInfo.faculties.Length];
|
65
|
// min/max hour taken into account
|
66
|
int[] minmaxHour = new int[] { 7, 18 };
|
67
|
// interval length
|
68
|
int range = minmaxHour[1] - minmaxHour[0];
|
69
|
|
70
|
// first day
|
71
|
DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
|
72
|
for (int i = 0; i < list.Count; i++)
|
73
|
{
|
74
|
int currHour = list[i].dateTime.Hour;
|
75
|
if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
|
76
|
continue;
|
77
|
|
78
|
// start of the day -> make an instance
|
79
|
string place = list[i].placeTag;
|
80
|
DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
|
81
|
if (!date.Equals(lastStartTime))
|
82
|
{
|
83
|
// data for each faculty separate
|
84
|
for (int k = 0; k < TagInfo.faculties.Length; k++)
|
85
|
{
|
86
|
ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
|
87
|
jisInfo.Add(dayInfo);
|
88
|
}
|
89
|
|
90
|
recordedAmount = new int[TagInfo.faculties.Length];
|
91
|
lastStartTime = date;
|
92
|
}
|
93
|
|
94
|
// aggregate data
|
95
|
if (TagInfo.jisPlaces.ContainsKey(place))
|
96
|
{
|
97
|
int index = TagInfo.jisPlaces[place];
|
98
|
if (index == -1)
|
99
|
for (int l = 0; l < TagInfo.faculties.Length; l++)
|
100
|
recordedAmount[l] += list[i].amount;
|
101
|
else
|
102
|
recordedAmount[index] += list[i].amount;
|
103
|
|
104
|
}
|
105
|
else
|
106
|
{
|
107
|
// TODO uknown code handling
|
108
|
Console.WriteLine("Unknown code " + list[i].placeTag);
|
109
|
}
|
110
|
|
111
|
}
|
112
|
|
113
|
// last day
|
114
|
for (int k = 0; k < TagInfo.faculties.Length; k++)
|
115
|
{
|
116
|
ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
|
117
|
jisInfo.Add(dayInfo);
|
118
|
}
|
119
|
|
120
|
return jisInfo;
|
121
|
}
|
122
|
|
123
|
private static List<ActivityInfo> ProcessOneJisFileAsIntervals(string path, int interval)
|
124
|
{
|
125
|
if (!File.Exists(path))
|
126
|
return null;
|
127
|
|
128
|
List<ActivityInfo> jisInfo = new List<ActivityInfo>();
|
129
|
List<JisInstance> list = CsvDataLoader.LoadJisFile(path);
|
130
|
|
131
|
// data for each faculty
|
132
|
int[] recordedAmount = new int[TagInfo.faculties.Length];
|
133
|
// min/max hour taken into account
|
134
|
int[] minmaxHour = new int[] { 7, 18 };
|
135
|
int range = minmaxHour[1] - minmaxHour[0];
|
136
|
|
137
|
if (interval > range)
|
138
|
return null;
|
139
|
|
140
|
int indices = (int) Math.Ceiling(range / (double)interval);
|
141
|
int[] from = new int[indices];
|
142
|
for (int i = 0; i < from.Length; i++) {
|
143
|
from[i] = minmaxHour[0] + interval * (i+1);
|
144
|
Console.Write(from[i] + " ");
|
145
|
}
|
146
|
Console.WriteLine();
|
147
|
|
148
|
// first day
|
149
|
DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
|
150
|
int endtime = from[0];
|
151
|
int startTime = minmaxHour[0];
|
152
|
int index = 0;
|
153
|
for (int i = 0; i < list.Count; i++)
|
154
|
{
|
155
|
int currHour = list[i].dateTime.Hour;
|
156
|
if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
|
157
|
continue;
|
158
|
|
159
|
// start of the day -> make an instance
|
160
|
string place = list[i].placeTag;
|
161
|
bool trigger = false;
|
162
|
DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
|
163
|
|
164
|
// end of the day
|
165
|
if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
|
166
|
{
|
167
|
endtime = from[0];
|
168
|
startTime = minmaxHour[0];
|
169
|
trigger = true;
|
170
|
index = 0;
|
171
|
}
|
172
|
|
173
|
// end of interval period
|
174
|
if ( date.Hour >= endtime || trigger) // date.Hour > endTime (date.Hour - lastStartTime.Hour) >= interval
|
175
|
{
|
176
|
// start time of last interval
|
177
|
DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, startTime, 0, 0);
|
178
|
|
179
|
// end of interval, not end of day -> need to find appropriate start and end times
|
180
|
if (!trigger)
|
181
|
{
|
182
|
index += 1;
|
183
|
while (from[index] < date.Hour)
|
184
|
index++;
|
185
|
|
186
|
endtime = from[index];
|
187
|
startTime = from[index - 1];
|
188
|
}
|
189
|
|
190
|
// data for each faculty separate
|
191
|
for (int k = 0; k < TagInfo.faculties.Length; k++)
|
192
|
{
|
193
|
ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], stTime, interval);
|
194
|
jisInfo.Add(dayInfo);
|
195
|
}
|
196
|
|
197
|
recordedAmount = new int[TagInfo.faculties.Length];
|
198
|
lastStartTime = date;
|
199
|
}
|
200
|
|
201
|
// aggregate data
|
202
|
if (TagInfo.jisPlaces.ContainsKey(place))
|
203
|
{
|
204
|
int faculty = TagInfo.jisPlaces[place];
|
205
|
if (faculty == -1)
|
206
|
for (int l = 0; l < TagInfo.faculties.Length; l++)
|
207
|
recordedAmount[l] += list[i].amount;
|
208
|
else
|
209
|
recordedAmount[faculty] += list[i].amount;
|
210
|
|
211
|
}
|
212
|
else
|
213
|
{
|
214
|
// TODO uknown code handling
|
215
|
Console.WriteLine("Unknown code " + list[i].placeTag);
|
216
|
}
|
217
|
|
218
|
}
|
219
|
|
220
|
// last day
|
221
|
for (int k = 0; k < TagInfo.faculties.Length; k++)
|
222
|
{
|
223
|
ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, interval);
|
224
|
jisInfo.Add(dayInfo);
|
225
|
}
|
226
|
|
227
|
return jisInfo;
|
228
|
}
|
229
|
}
|
230
|
}
|