Projekt

Obecné

Profil

Stáhnout (11.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
//
2
// Author: A. Konig
3
//
4

    
5
using ServerApp.Parser.InputData;
6
using ServerApp.Parser.OutputInfo;
7
using System;
8
using System.Collections.Generic;
9
using System.IO;
10

    
11
namespace ServerApp.Parser.Parsers
12
{
13
    /// <summary>
14
    /// Class parsing login files into instances of ActivityInfo divided by given time interval
15
    /// Data parsed from 7am (included) to 18pm (included)
16
    /// </summary>
17
    /// <author>A. Konig</author>
18
    public class LogInParser
19
    {
20
        /// <summary> Datafile loader  </summary>
21
        IDataLoader loader;
22

    
23
        /// <summary>
24
        /// Constructor
25
        /// </summary>
26
        /// <param name="loader">Datafile loader</param>
27
        public LogInParser(IDataLoader loader)
28
        {
29
            this.loader = loader;
30
        }
31

    
32
        /// <summary>
33
        /// Parses login data to ActivityInfo instances
34
        /// Data parsed from 7am (included) to 18pm (included)
35
        /// </summary>
36
        /// <param name="loginFiles">Paths to files with login data files</param>
37
        /// <param name="endTime">End time of related data</param>
38
        /// <param name="startTime">Start time of related data</param>
39
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
40
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
41
        /// <returns></returns>
42
        public List<ActivityInfo> ParseLogInData(List<string> loginFiles, DateTime startTime, DateTime endTime, bool wholeDay = true, int interval = 1)
43
        {
44
            List<ActivityInfo> list = new List<ActivityInfo>();
45

    
46
            if (loginFiles == null || interval <= 0)
47
                return list;
48

    
49
            string current = "";
50
            try
51
            {
52
                // for all files in folder
53
                foreach (string fileName in loginFiles)
54
                {
55
                    current = fileName;
56

    
57
                    if (!File.Exists(fileName))
58
                        continue;
59

    
60
                    // parse as one instance per day
61
                    List<ActivityInfo> loadedData = null;
62
                    if (wholeDay)
63
                        loadedData = ProcessOneLogInFileAsDays(fileName, startTime, endTime);
64
                    // parse by interval length
65
                    else
66
                        loadedData = ProcessOneLoginFileAsIntervals(fileName, interval, startTime, endTime);
67

    
68
                    list.AddRange(loadedData);
69
                }
70
            } catch
71
            {
72
                Console.WriteLine("Incorrect Login input file " + current);
73
            }
74

    
75
            return list;
76
        }
77

    
78
        /// <summary>
79
        /// Parses data from one data file as one instance per day
80
        /// </summary>
81
        /// <param name="path">Path ti file</param>
82
        /// <param name="endTime">End time of related data</param>
83
        /// <param name="startTime">Start time of related data</param>
84
        /// <returns>List with ActivityInfo instances</returns>
85
        private List<ActivityInfo> ProcessOneLogInFileAsDays(string path, DateTime startTime, DateTime endTime)
86
        {
87
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
88

    
89
            List<LogInInstance> list = loader.LoadLoginFile(path);
90
            if (list == null || list.Count == 0)
91
                return loginInfo;
92

    
93
            // data for each faculty
94
            int[] recordedAmount = new int[TagInfo.buildings.Length];
95
            // min/max hour taken into account
96
            int[] minmaxHour = new int[] { 7, 18 };
97
            // interval length
98
            int range = minmaxHour[1] - minmaxHour[0];
99

    
100
            // first day
101
            DateTime lastStartDay = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
102
            for (int i = 0; i < list.Count; i++)
103
            {
104
                int currHour = list[i].lessonStart.Hour;
105
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
106
                    continue;
107

    
108
                // start of the day -> make an instance
109
                string place = list[i].building;
110
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, minmaxHour[0], 0, 0);
111
                DateTime dateOfLessonStart = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, list[i].lessonStart.Hour, list[i].lessonStart.Minute, list[i].lessonStart.Second);
112
                if (!date.Equals(lastStartDay))
113
                {
114
                    // data for each faculty separate
115
                    for (int k = 0; k < TagInfo.buildings.Length; k++)
116
                    {
117
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range); 
118
                        if (recordedAmount[k] != 0)
119
                            loginInfo.Add(dayInfo);
120
                    }
121

    
122
                    recordedAmount = new int[TagInfo.buildings.Length];
123
                    lastStartDay = date;
124
                }
125

    
126
                // if not in allowed time window -> discard
127
                if (dateOfLessonStart < startTime || dateOfLessonStart > endTime)
128
                    continue;
129

    
130
                // aggregate data
131
                if (TagInfo.buildingTags.ContainsKey(place))
132
                {
133
                    int index = TagInfo.buildingTags[place];
134
                    //Console.WriteLine(place + " " + index);
135

    
136
                    // to all
137
                    if (index == -1)
138
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
139
                            recordedAmount[l] += list[i].amount;
140
                    else
141
                        recordedAmount[index] += list[i].amount;
142

    
143
                }
144
                else
145
                {
146
                    // TODO uknown code handling -> to file?
147
                    // Console.WriteLine("Unknown code " + list[i].building);
148
                }
149

    
150
            }
151

    
152
            // last day
153
            for (int k = 0; k < TagInfo.buildings.Length; k++)
154
            {
155
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range);
156
                if (recordedAmount[k] != 0)
157
                    loginInfo.Add(dayInfo);
158
            }
159

    
160
            return loginInfo;
161
        }
162

    
163
        /// <summary>
164
        /// Parses data from one data file as one instance per interval length
165
        /// </summary>
166
        /// <param name="path">Path to file</param>
167
        /// <param name="interval">Interval length</param>
168
        /// <param name="endTime">End time of related data</param>
169
        /// <param name="startTime">Start time of related data</param>
170
        /// <returns>List with ActivityInfo instances</returns>
171
        private List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval, DateTime startTime, DateTime endTime)
172
        {
173
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
174
            
175
            List<LogInInstance> list = loader.LoadLoginFile(path);
176
            if (list == null || list.Count == 0)
177
                return loginInfo;
178

    
179
            // min/max hour taken into account
180
            int[] minmaxHour = new int[] { 7, 18 };
181
            int range = minmaxHour[1] - minmaxHour[0];
182

    
183
            if (interval > range)
184
                return null;
185

    
186
            int indices = (int)Math.Ceiling(range / (double)interval);
187
            int[] to = new int[indices];
188
            int[][] data = new int[indices][];
189
            for (int i = 0; i < to.Length; i++)
190
            {
191
                to[i] = minmaxHour[0] + interval * (i + 1);
192
                data[i] = new int[TagInfo.buildings.Length];
193
            }
194
            
195
            // first day
196
            DateTime lastStartTime = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
197
            int index = 0;
198
            for (int i = 0; i < list.Count; i++)
199
            {
200
                int currHour = list[i].lessonStart.Hour;
201
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
202
                    continue;
203

    
204
                // start of the day -> make an instance
205
                string place = list[i].building;
206
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, list[i].lessonStart.Hour, 0, 0);
207
                DateTime dateOfLessonStart = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, list[i].lessonStart.Hour, list[i].lessonStart.Minute, list[i].lessonStart.Second);
208

    
209
                // end of the day
210
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
211
                {
212
                    // note down all aggregated data
213
                    for (int k = 0; k < to.Length; k++)
214
                    {
215
                        DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
216

    
217
                        // data for each faculty separate
218
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
219
                        {
220
                            ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
221
                            if (data[k][l] != 0)
222
                                loginInfo.Add(dayInfo);
223
                        }
224

    
225
                        data[k] = new int[TagInfo.buildings.Length];
226
                    }
227
                    lastStartTime = date;
228
                }
229

    
230
                // if not in allowed time window -> discard
231
                if (dateOfLessonStart < startTime || dateOfLessonStart > endTime)
232
                    continue;
233

    
234
                // find index for current instance
235
                index = 0;
236
                for (int k = 1; k < to.Length; k++)
237
                {
238
                    if (to[k] > list[i].lessonStart.Hour && to[k - 1] <= list[i].lessonStart.Hour)
239
                    {
240
                        index = k;
241
                        break;
242
                    }
243
                }
244

    
245
                // aggregate data
246
                if (TagInfo.buildingTags.ContainsKey(place))
247
                {
248
                    int faculty = TagInfo.buildingTags[place];
249
                    // to all
250
                    if (faculty == -1)
251
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
252
                            data[index][l] += list[i].amount;
253
                    // to first two
254
                    else if (faculty == -2)
255
                    {
256
                        data[index][0] += list[i].amount;
257
                        data[index][1] += list[i].amount;
258
                    }
259
                    else
260
                        data[index][faculty] += list[i].amount;
261

    
262
                }
263
                else
264
                {
265
                    // TODO uknown code handling -> write to file?
266
                    // Console.WriteLine("Unknown code " + list[i].building);
267
                }
268

    
269
            }
270

    
271
            for (int k = 0; k < to.Length; k++)
272
            {
273
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
274

    
275
                // data for each faculty separate
276
                for (int l = 0; l < TagInfo.buildings.Length; l++)
277
                {
278
                    ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
279
                    if (data[k][l] != 0)
280
                        loginInfo.Add(dayInfo);
281
                }
282
            }
283

    
284
            return loginInfo;
285
        }
286

    
287
    }
288
}
(4-4/6)