Projekt

Obecné

Profil

Stáhnout (9.63 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.buildings.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.buildings.Length; k++)
97
                    {
98
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range); 
99
                        loginInfo.Add(dayInfo);
100
                    }
101

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

    
106
                // aggregate data
107
                if (TagInfo.buildingTags.ContainsKey(place))
108
                {
109
                    int index = TagInfo.buildingTags[place];
110
                    //Console.WriteLine(place + " " + index);
111

    
112
                    // to all
113
                    if (index == -1)
114
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
115
                            recordedAmount[l] += list[i].amount;
116
                    else
117
                        recordedAmount[index] += list[i].amount;
118

    
119
                }
120
                else
121
                {
122
                    // TODO uknown code handling -> to file?
123
                    // Console.WriteLine("Unknown code " + list[i].building);
124
                }
125

    
126
            }
127

    
128
            // last day
129
            for (int k = 0; k < TagInfo.buildings.Length; k++)
130
            {
131
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartDay, range);
132
                loginInfo.Add(dayInfo);
133
            }
134

    
135
            return loginInfo;
136
        }
137

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

    
149
            List<ActivityInfo> loignInfo = new List<ActivityInfo>();
150
            List<LogInInstance> list = loader.LoadLoginFile(path);
151

    
152
            // min/max hour taken into account
153
            int[] minmaxHour = new int[] { 7, 18 };
154
            int range = minmaxHour[1] - minmaxHour[0];
155

    
156
            if (interval > range)
157
                return null;
158

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

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

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

    
189
                        // data for each faculty separate
190
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
191
                        {
192
                            ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
193
                            loignInfo.Add(dayInfo);
194
                        }
195

    
196
                        data[k] = new int[TagInfo.buildings.Length];
197
                    }
198
                    lastStartTime = date;
199
                }
200

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

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

    
229
                }
230
                else
231
                {
232
                    // TODO uknown code handling -> write to file?
233
                    // Console.WriteLine("Unknown code " + list[i].building);
234
                }
235

    
236
            }
237

    
238
            for (int k = 0; k < to.Length; k++)
239
            {
240
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
241

    
242
                // data for each faculty separate
243
                for (int l = 0; l < TagInfo.buildings.Length; l++)
244
                {
245
                    ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[l], data[k][l], stTime, interval);
246
                    loignInfo.Add(dayInfo);
247
                }
248
            }
249

    
250
            return loignInfo;
251
        }
252

    
253
    }
254
}
(3-3/5)