Projekt

Obecné

Profil

Stáhnout (9.77 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ServerApp.Parser.InputData;
2
using ServerApp.Parser.OutputInfo;
3
using System;
4
using System.Collections.Generic;
5
using System.IO;
6

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

    
19
        /// <summary>
20
        /// Constructor
21
        /// </summary>
22
        /// <param name="loader">Datafile loader</param>
23
        public LogInParser(DataLoader loader)
24
        {
25
            this.loader = loader;
26
        }
27

    
28
        /// <summary>
29
        /// Parses login data to ActivityInfo instances
30
        /// Data parsed from 7am (included) to 18pm (included)
31
        /// </summary>
32
        /// <param name="folder">Folder with login data files</param>
33
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
34
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
35
        /// <returns></returns>
36
        public List<ActivityInfo> ParseLogInData(string folder, bool wholeDay = true, int interval = 1)
37
        {
38
            if (!Directory.Exists(folder))
39
                return null;
40
            
41
            List<ActivityInfo> list = new List<ActivityInfo>();
42

    
43
            // get all files in folder
44
            string[] fileEntries = Directory.GetFiles(folder);
45
            foreach (string fileName in fileEntries)
46
            {
47
                List<ActivityInfo> loadedData = null;
48
                
49
                // parse as one instance per day
50
                if (wholeDay)
51
                    loadedData = ProcessOneLogInFileAsDays(fileName);
52
                // parse by interval length
53
                else
54
                    loadedData = ProcessOneLoginFileAsIntervals(fileName, interval);
55

    
56
                list.AddRange(loadedData);
57
            }
58

    
59
            return list;
60
        }
61

    
62
        /// <summary>
63
        /// Parses data from one data file as one instance per day
64
        /// </summary>
65
        /// <param name="path">Path ti file</param>
66
        /// <returns>List with ActivityInfo instances</returns>
67
        private List<ActivityInfo> ProcessOneLogInFileAsDays(string path)
68
        {
69
            if (!File.Exists(path))
70
                return null;
71

    
72
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
73
            List<LogInInstance> list = loader.LoadLoginFile(path);
74

    
75
            // data for each faculty
76
            int[] recordedAmount = new int[TagInfo.faculties.Length];
77
            // min/max hour taken into account
78
            int[] minmaxHour = new int[] { 7, 18 };
79
            // interval length
80
            int range = minmaxHour[1] - minmaxHour[0];
81

    
82
            // first day
83
            DateTime lastStartDay = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
84
            for (int i = 0; i < list.Count; i++)
85
            {
86
                int currHour = list[i].lessonStart.Hour;
87
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
88
                    continue;
89

    
90
                // start of the day -> make an instance
91
                string place = list[i].building;
92
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, minmaxHour[0], 0, 0);
93
                if (!date.Equals(lastStartDay))
94
                {
95
                    // data for each faculty separate
96
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
97
                    {
98
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range); 
99
                        loginInfo.Add(dayInfo);
100
                    }
101

    
102
                    recordedAmount = new int[TagInfo.faculties.Length];
103
                    lastStartDay = date;
104
                }
105

    
106
                // aggregate data
107
                if (TagInfo.buildingTags.ContainsKey(place))
108
                {
109
                    int index = TagInfo.buildingTags[place];
110
                    // to all
111
                    if (index == -1)
112
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
113
                            recordedAmount[l] += list[i].amount;
114
                    // to first two
115
                    else if (index == -2)
116
                    {
117
                        recordedAmount[0] += list[i].amount;
118
                        recordedAmount[1] += list[i].amount;
119
                    }
120
                    else
121
                        recordedAmount[index] += list[i].amount;
122

    
123
                }
124
                else
125
                {
126
                    // TODO uknown code handling
127
                    Console.WriteLine("Unknown code " + list[i].building);
128
                }
129

    
130
            }
131

    
132
            // last day
133
            for (int k = 0; k < TagInfo.faculties.Length; k++)
134
            {
135
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range);
136
                loginInfo.Add(dayInfo);
137
            }
138

    
139
            return loginInfo;
140
        }
141

    
142
        /// <summary>
143
        /// Parses data from one data file as one instance per interval length
144
        /// </summary>
145
        /// <param name="path">Path to file</param>
146
        /// <param name="interval">Interval length</param>
147
        /// <returns>List with ActivityInfo instances</returns>
148
        private List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval)
149
        {
150
            if (!File.Exists(path))
151
                return null;
152

    
153
            List<ActivityInfo> loignInfo = new List<ActivityInfo>();
154
            List<LogInInstance> list = loader.LoadLoginFile(path);
155

    
156
            // min/max hour taken into account
157
            int[] minmaxHour = new int[] { 7, 18 };
158
            int range = minmaxHour[1] - minmaxHour[0];
159

    
160
            if (interval > range)
161
                return null;
162

    
163
            int indices = (int)Math.Ceiling(range / (double)interval);
164
            int[] to = new int[indices];
165
            int[][] data = new int[indices][];
166
            for (int i = 0; i < to.Length; i++)
167
            {
168
                to[i] = minmaxHour[0] + interval * (i + 1);
169
                data[i] = new int[TagInfo.faculties.Length];
170
            }
171
            
172
            // first day
173
            DateTime lastStartTime = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
174
            int index = 0;
175
            for (int i = 0; i < list.Count; i++)
176
            {
177
                int currHour = list[i].lessonStart.Hour;
178
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
179
                    continue;
180

    
181
                // start of the day -> make an instance
182
                string place = list[i].building;
183
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, list[i].lessonStart.Hour, 0, 0);
184

    
185
                // end of the day
186
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
187
                {
188
                    // note down all aggregated data
189
                    for (int k = 0; k < to.Length; k++)
190
                    {
191
                        DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
192

    
193
                        // data for each faculty separate
194
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
195
                        {
196
                            ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[l], data[k][l], stTime, interval);
197
                            loignInfo.Add(dayInfo);
198
                        }
199

    
200
                        data[k] = new int[TagInfo.faculties.Length];
201
                    }
202
                    lastStartTime = date;
203
                }
204

    
205
                // find index for current instance
206
                index = 0;
207
                for (int k = 1; k < to.Length; k++)
208
                {
209
                    if (to[k] > list[i].lessonStart.Hour && to[k - 1] <= list[i].lessonStart.Hour)
210
                    {
211
                        index = k;
212
                        break;
213
                    }
214
                }
215

    
216
                // aggregate data
217
                if (TagInfo.buildingTags.ContainsKey(place))
218
                {
219
                    int faculty = TagInfo.buildingTags[place];
220
                    // to all
221
                    if (faculty == -1)
222
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
223
                            data[index][l] += list[i].amount;
224
                    // to first two
225
                    else if (faculty == -2)
226
                    {
227
                        data[index][0] += list[i].amount;
228
                        data[index][1] += list[i].amount;
229
                    }
230
                    else
231
                        data[index][faculty] += list[i].amount;
232

    
233
                }
234
                else
235
                {
236
                    // TODO uknown code handling
237
                    Console.WriteLine("Unknown code " + list[i].building);
238
                }
239

    
240
            }
241

    
242
            for (int k = 0; k < to.Length; k++)
243
            {
244
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
245

    
246
                // data for each faculty separate
247
                for (int l = 0; l < TagInfo.faculties.Length; l++)
248
                {
249
                    ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[l], data[k][l], stTime, interval);
250
                    loignInfo.Add(dayInfo);
251
                }
252
            }
253

    
254
            return loignInfo;
255
        }
256

    
257
    }
258
}
(4-4/6)