Projekt

Obecné

Profil

Stáhnout (11.2 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 || startTime == null || endTime == null || interval <= 0)
47
                return list;
48

    
49
            // for all files in folder
50
            foreach (string fileName in loginFiles)
51
            {
52
                if (!File.Exists(fileName))
53
                    continue;
54
                
55
                // parse as one instance per day
56
                List<ActivityInfo> loadedData = null;
57
                if (wholeDay)
58
                    loadedData = ProcessOneLogInFileAsDays(fileName, startTime, endTime);
59
                // parse by interval length
60
                else
61
                    loadedData = ProcessOneLoginFileAsIntervals(fileName, interval, startTime, endTime);
62

    
63
                list.AddRange(loadedData);
64
            }
65

    
66
            return list;
67
        }
68

    
69
        /// <summary>
70
        /// Parses data from one data file as one instance per day
71
        /// </summary>
72
        /// <param name="path">Path ti file</param>
73
        /// <param name="endTime">End time of related data</param>
74
        /// <param name="startTime">Start time of related data</param>
75
        /// <returns>List with ActivityInfo instances</returns>
76
        private List<ActivityInfo> ProcessOneLogInFileAsDays(string path, DateTime startTime, DateTime endTime)
77
        {
78
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
79

    
80
            List<LogInInstance> list = loader.LoadLoginFile(path);
81
            if (list == null)
82
                return loginInfo;
83

    
84
            // data for each faculty
85
            int[] recordedAmount = new int[TagInfo.buildings.Length];
86
            // min/max hour taken into account
87
            int[] minmaxHour = new int[] { 7, 18 };
88
            // interval length
89
            int range = minmaxHour[1] - minmaxHour[0];
90

    
91
            // first day
92
            DateTime lastStartDay = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
93
            for (int i = 0; i < list.Count; i++)
94
            {
95
                int currHour = list[i].lessonStart.Hour;
96
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
97
                    continue;
98

    
99
                // start of the day -> make an instance
100
                string place = list[i].building;
101
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, minmaxHour[0], 0, 0);
102
                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);
103
                if (!date.Equals(lastStartDay))
104
                {
105
                    // data for each faculty separate
106
                    for (int k = 0; k < TagInfo.buildings.Length; k++)
107
                    {
108
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range); 
109
                        if (recordedAmount[k] != 0)
110
                            loginInfo.Add(dayInfo);
111
                    }
112

    
113
                    recordedAmount = new int[TagInfo.buildings.Length];
114
                    lastStartDay = date;
115
                }
116

    
117
                // if not in allowed time window -> discard
118
                if (dateOfLessonStart < startTime || dateOfLessonStart > endTime)
119
                    continue;
120

    
121
                // aggregate data
122
                if (TagInfo.buildingTags.ContainsKey(place))
123
                {
124
                    int index = TagInfo.buildingTags[place];
125
                    //Console.WriteLine(place + " " + index);
126

    
127
                    // to all
128
                    if (index == -1)
129
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
130
                            recordedAmount[l] += list[i].amount;
131
                    else
132
                        recordedAmount[index] += list[i].amount;
133

    
134
                }
135
                else
136
                {
137
                    // TODO uknown code handling -> to file?
138
                    // Console.WriteLine("Unknown code " + list[i].building);
139
                }
140

    
141
            }
142

    
143
            // last day
144
            for (int k = 0; k < TagInfo.buildings.Length; k++)
145
            {
146
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range);
147
                if (recordedAmount[k] != 0)
148
                    loginInfo.Add(dayInfo);
149
            }
150

    
151
            return loginInfo;
152
        }
153

    
154
        /// <summary>
155
        /// Parses data from one data file as one instance per interval length
156
        /// </summary>
157
        /// <param name="path">Path to file</param>
158
        /// <param name="interval">Interval length</param>
159
        /// <param name="endTime">End time of related data</param>
160
        /// <param name="startTime">Start time of related data</param>
161
        /// <returns>List with ActivityInfo instances</returns>
162
        private List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval, DateTime startTime, DateTime endTime)
163
        {
164
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
165
            
166
            List<LogInInstance> list = loader.LoadLoginFile(path);
167
            if (list == null)
168
                return loginInfo;
169

    
170
            // min/max hour taken into account
171
            int[] minmaxHour = new int[] { 7, 18 };
172
            int range = minmaxHour[1] - minmaxHour[0];
173

    
174
            if (interval > range)
175
                return null;
176

    
177
            int indices = (int)Math.Ceiling(range / (double)interval);
178
            int[] to = new int[indices];
179
            int[][] data = new int[indices][];
180
            for (int i = 0; i < to.Length; i++)
181
            {
182
                to[i] = minmaxHour[0] + interval * (i + 1);
183
                data[i] = new int[TagInfo.buildings.Length];
184
            }
185
            
186
            // first day
187
            DateTime lastStartTime = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
188
            int index = 0;
189
            for (int i = 0; i < list.Count; i++)
190
            {
191
                int currHour = list[i].lessonStart.Hour;
192
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
193
                    continue;
194

    
195
                // start of the day -> make an instance
196
                string place = list[i].building;
197
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, list[i].lessonStart.Hour, 0, 0);
198
                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);
199

    
200
                // end of the day
201
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
202
                {
203
                    // note down all aggregated data
204
                    for (int k = 0; k < to.Length; k++)
205
                    {
206
                        DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
207

    
208
                        // data for each faculty separate
209
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
210
                        {
211
                            ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
212
                            if (data[k][l] != 0)
213
                                loginInfo.Add(dayInfo);
214
                        }
215

    
216
                        data[k] = new int[TagInfo.buildings.Length];
217
                    }
218
                    lastStartTime = date;
219
                }
220

    
221
                // if not in allowed time window -> discard
222
                if (dateOfLessonStart < startTime || dateOfLessonStart > endTime)
223
                    continue;
224

    
225
                // find index for current instance
226
                index = 0;
227
                for (int k = 1; k < to.Length; k++)
228
                {
229
                    if (to[k] > list[i].lessonStart.Hour && to[k - 1] <= list[i].lessonStart.Hour)
230
                    {
231
                        index = k;
232
                        break;
233
                    }
234
                }
235

    
236
                // aggregate data
237
                if (TagInfo.buildingTags.ContainsKey(place))
238
                {
239
                    int faculty = TagInfo.buildingTags[place];
240
                    // to all
241
                    if (faculty == -1)
242
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
243
                            data[index][l] += list[i].amount;
244
                    // to first two
245
                    else if (faculty == -2)
246
                    {
247
                        data[index][0] += list[i].amount;
248
                        data[index][1] += list[i].amount;
249
                    }
250
                    else
251
                        data[index][faculty] += list[i].amount;
252

    
253
                }
254
                else
255
                {
256
                    // TODO uknown code handling -> write to file?
257
                    // Console.WriteLine("Unknown code " + list[i].building);
258
                }
259

    
260
            }
261

    
262
            for (int k = 0; k < to.Length; k++)
263
            {
264
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
265

    
266
                // data for each faculty separate
267
                for (int l = 0; l < TagInfo.buildings.Length; l++)
268
                {
269
                    ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
270
                    if (data[k][l] != 0)
271
                        loginInfo.Add(dayInfo);
272
                }
273
            }
274

    
275
            return loginInfo;
276
        }
277

    
278
    }
279
}
(4-4/6)