Projekt

Obecné

Profil

Stáhnout (10.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 cdf3c217 A-Konig
//
2
// Author: A. Konig
3
//
4
5
using System;
6 5d9a5bd9 A-Konig
using System.Collections.Generic;
7
using System.IO;
8 734533a8 A-Konig
using ServerApp.Parser.InputData;
9
using ServerApp.Parser.OutputInfo;
10 5d9a5bd9 A-Konig
11 734533a8 A-Konig
namespace ServerApp.Parser.Parsers
12 5d9a5bd9 A-Konig
{
13 734533a8 A-Konig
    /// <summary>
14
    /// Class parsing jis files into instances of ActivityInfo divided by given time interval
15
    /// Data parsed from 7am (included) to 18pm (included)
16
    /// </summary>
17 cdf3c217 A-Konig
    /// <author>A. Konig</author>
18 a53b1de8 A-Konig
    public class JisParser
19 5d9a5bd9 A-Konig
    {
20 98b568bc A-Konig
21
        /// <summary> Datafile loader  </summary>
22 cdf3c217 A-Konig
        IDataLoader loader;
23 98b568bc A-Konig
24
        /// <summary>
25
        /// Constructor
26
        /// </summary>
27
        /// <param name="loader">Datafile loader</param>
28 cdf3c217 A-Konig
        public JisParser(IDataLoader loader)
29 98b568bc A-Konig
        {
30
            this.loader = loader;
31
        }
32
33 734533a8 A-Konig
        /// <summary>
34
        /// Parses jis data to ActivityInfo instances
35
        /// Data parsed from 7am (included) to 18pm (included)
36
        /// </summary>
37 cdf3c217 A-Konig
        /// <param name="jisFiles">Paths to files with login data files</param>
38
        /// <param name="endTime">End time of related data</param>
39
        /// <param name="startTime">Start time of related data</param>
40 734533a8 A-Konig
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
41
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
42
        /// <returns></returns>
43 cdf3c217 A-Konig
        public List<ActivityInfo> ParseJisData(List<string> jisFiles, DateTime startTime, DateTime endTime, bool wholeDay = true, int interval = 1)
44 5d9a5bd9 A-Konig
        {
45 734533a8 A-Konig
            List<ActivityInfo> list = new List<ActivityInfo>();
46
47 23e8ae04 A-Konig
            if (jisFiles == null || interval <= 0)
48 cdf3c217 A-Konig
                return list;
49
50 23e8ae04 A-Konig
            string current = "";
51
            try
52 5d9a5bd9 A-Konig
            {
53 23e8ae04 A-Konig
                // parse all files
54
                foreach (string fileName in jisFiles)
55
                {
56
                    current = fileName;
57 5d9a5bd9 A-Konig
58 23e8ae04 A-Konig
                    if (!File.Exists(fileName))
59
                        continue;
60
61
                    // parse as one instance per day
62
                    List<ActivityInfo> loadedData = null;
63
                    if (wholeDay)
64
                        loadedData = ProcessOneJisFileAsDays(fileName, startTime, endTime);
65
                    // parse by interval length
66
                    else
67
                        loadedData = ProcessOneJisFileAsIntervals(fileName, interval, startTime, endTime);
68
69
                    list.AddRange(loadedData);
70
                }
71
            }
72
            catch
73
            {
74
                Console.WriteLine("Incorrect jis input file " + current);
75 5d9a5bd9 A-Konig
            }
76
77
            return list;
78
        }
79
80 734533a8 A-Konig
        /// <summary>
81
        /// Parses data from one data file as one instance per day
82
        /// </summary>
83 cdf3c217 A-Konig
        /// <param name="path">Path to file</param>
84
        /// <param name="endTime">End time of related data</param>
85
        /// <param name="startTime">Start time of related data</param>
86 734533a8 A-Konig
        /// <returns>List with ActivityInfo instances</returns>
87 cdf3c217 A-Konig
        private List<ActivityInfo> ProcessOneJisFileAsDays(string path, DateTime startTime, DateTime endTime)
88 5d9a5bd9 A-Konig
        {
89 cdf3c217 A-Konig
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
90
            
91 98b568bc A-Konig
            List<JisInstance> list =  loader.LoadJisFile(path);
92 3729dcae A-Konig
            if (list == null || list.Count == 0)
93 a53b1de8 A-Konig
                return jisInfo;
94 5d9a5bd9 A-Konig
95 734533a8 A-Konig
            // data for each faculty
96 d39750f3 A-Konig
            int[] recordedAmount = new int[TagInfo.buildings.Length];
97 734533a8 A-Konig
            // min/max hour taken into account
98 0da0ac88 A-Konig
            int[] minmaxHour = new int[] { 7, 18 };
99 734533a8 A-Konig
            // interval length
100 0da0ac88 A-Konig
            int range = minmaxHour[1] - minmaxHour[0];
101 5d9a5bd9 A-Konig
102 734533a8 A-Konig
            // first day
103
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
104 5d9a5bd9 A-Konig
            for (int i = 0; i < list.Count; i++)
105
            {
106 0da0ac88 A-Konig
                int currHour = list[i].dateTime.Hour;
107
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
108
                    continue;
109
110 734533a8 A-Konig
                // start of the day -> make an instance
111 0da0ac88 A-Konig
                string place = list[i].placeTag;
112 734533a8 A-Konig
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
113 0da0ac88 A-Konig
                if (!date.Equals(lastStartTime)) 
114 5d9a5bd9 A-Konig
                {
115 734533a8 A-Konig
                    // data for each faculty separate
116 d39750f3 A-Konig
                    for (int k = 0; k < TagInfo.buildings.Length; k++)
117 5d9a5bd9 A-Konig
                    {
118 d39750f3 A-Konig
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartTime, range);
119 a53b1de8 A-Konig
                        if (recordedAmount[k] != 0)
120
                            jisInfo.Add(dayInfo);
121 5d9a5bd9 A-Konig
                    }
122
123 d39750f3 A-Konig
                    recordedAmount = new int[TagInfo.buildings.Length];
124 0da0ac88 A-Konig
                    lastStartTime = date;
125 5d9a5bd9 A-Konig
                }
126
127 cdf3c217 A-Konig
                // if not in allowed time window -> discard
128
                if (list[i].dateTime < startTime || list[i].dateTime > endTime)
129
                    continue;
130
131 734533a8 A-Konig
                // aggregate data
132 0da0ac88 A-Konig
                if (TagInfo.jisPlaces.ContainsKey(place))
133
                {
134
                    int index = TagInfo.jisPlaces[place];
135
                    if (index == -1)
136 d39750f3 A-Konig
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
137 0da0ac88 A-Konig
                            recordedAmount[l] += list[i].amount;
138
                    else
139
                        recordedAmount[index] += list[i].amount;
140
141
                }
142 5d9a5bd9 A-Konig
                else
143
                {
144 d39750f3 A-Konig
                    // TODO uknown code handling -> to file?
145
                    // Console.WriteLine("Unknown code " + list[i].placeTag);
146 5d9a5bd9 A-Konig
                }
147
148
            }
149
150 734533a8 A-Konig
            // last day
151 d39750f3 A-Konig
            for (int k = 0; k < TagInfo.buildings.Length; k++)
152 0da0ac88 A-Konig
            {
153 d39750f3 A-Konig
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], lastStartTime, range);
154 a53b1de8 A-Konig
                if (recordedAmount[k] != 0)
155
                    jisInfo.Add(dayInfo);
156 0da0ac88 A-Konig
            }
157
158
            return jisInfo;
159
        }
160
161 98b568bc A-Konig
        /// <summary>
162
        /// Parses data from one data file as instance per interval length in a day
163
        /// </summary>
164
        /// <param name="path">Path ti file</param>
165
        /// <param name="interval">Interval length</param>
166 cdf3c217 A-Konig
        /// <param name="endTime">End time of related data</param>
167
        /// <param name="startTime">Start time of related data</param>
168 98b568bc A-Konig
        /// <returns>List with ActivityInfo instances</returns>
169 cdf3c217 A-Konig
        private List<ActivityInfo> ProcessOneJisFileAsIntervals(string path, int interval, DateTime startTime, DateTime endTime)
170 0da0ac88 A-Konig
        {
171 cdf3c217 A-Konig
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
172 260b1217 A-Konig
173 98b568bc A-Konig
            List<JisInstance> list = loader.LoadJisFile(path);
174 3729dcae A-Konig
            if (list == null || list.Count == 0)
175 a53b1de8 A-Konig
                return jisInfo;
176 260b1217 A-Konig
177
            // data for each faculty
178 d39750f3 A-Konig
            int[] recordedAmount = new int[TagInfo.buildings.Length];
179 260b1217 A-Konig
            // 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 ecdce631 A-Konig
            int[] to = new int[indices];
188
            for (int i = 0; i < to.Length; i++) 
189
                to[i] = minmaxHour[0] + interval * (i+1);
190 260b1217 A-Konig
191
            // first day
192
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
193 ecdce631 A-Konig
            int endtime = to[0];
194 cdf3c217 A-Konig
            int startingTime = minmaxHour[0];
195 1db865ce A-Konig
            int index = 0;
196 260b1217 A-Konig
            for (int i = 0; i < list.Count; i++)
197
            {
198
                int currHour = list[i].dateTime.Hour;
199
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
200
                    continue;
201
202
                // start of the day -> make an instance
203
                string place = list[i].placeTag;
204
                bool trigger = false;
205
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
206 1db865ce A-Konig
207 260b1217 A-Konig
                // end of the day
208
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
209 1db865ce A-Konig
                {
210 260b1217 A-Konig
                    trigger = true;
211 1db865ce A-Konig
                    index = 0;
212
                }
213 260b1217 A-Konig
214
                // end of interval period
215 ecdce631 A-Konig
                if ( date.Hour >= endtime || trigger) 
216 260b1217 A-Konig
                {
217 1db865ce A-Konig
                    // start time of last interval
218 cdf3c217 A-Konig
                    DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, startingTime, 0, 0);
219 ecdce631 A-Konig
220 a53b1de8 A-Konig
                    // TODO zjistit kolik se vynechalo a přidat tolik nul?
221
222 ecdce631 A-Konig
                    // find end and start time
223 1db865ce A-Konig
                    if (!trigger)
224 ecdce631 A-Konig
                        index++;
225
                    while (date.Hour >= to[index])
226
                        index++;
227 1db865ce A-Konig
228 ecdce631 A-Konig
                    endtime = to[index];
229
                    if (index == 0)
230 cdf3c217 A-Konig
                        startingTime = minmaxHour[0];
231 ecdce631 A-Konig
                    else
232 cdf3c217 A-Konig
                        startingTime = to[index - 1];
233 1db865ce A-Konig
234 260b1217 A-Konig
                    // data for each faculty separate
235 d39750f3 A-Konig
                    for (int k = 0; k < TagInfo.buildings.Length; k++)
236 260b1217 A-Konig
                    {
237 d39750f3 A-Konig
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], stTime, interval);
238 a53b1de8 A-Konig
                        if (recordedAmount[k] != 0)
239
                            jisInfo.Add(dayInfo);
240 260b1217 A-Konig
                    }
241
242 d39750f3 A-Konig
                    recordedAmount = new int[TagInfo.buildings.Length];
243 260b1217 A-Konig
                    lastStartTime = date;
244
                }
245
246 cdf3c217 A-Konig
                // if not in allowed time window -> discard
247
                if (list[i].dateTime < startTime || list[i].dateTime > endTime)
248
                    continue;
249
250 260b1217 A-Konig
                // aggregate data
251
                if (TagInfo.jisPlaces.ContainsKey(place))
252
                {
253 1db865ce A-Konig
                    int faculty = TagInfo.jisPlaces[place];
254
                    if (faculty == -1)
255 d39750f3 A-Konig
                        for (int l = 0; l < TagInfo.buildings.Length; l++)
256 260b1217 A-Konig
                            recordedAmount[l] += list[i].amount;
257
                    else
258 1db865ce A-Konig
                        recordedAmount[faculty] += list[i].amount;
259 260b1217 A-Konig
260
                }
261
                else
262
                {
263 d39750f3 A-Konig
                    // TODO uknown code handling -> to file?
264
                    // Console.WriteLine("Unknown code " + list[i].placeTag);
265 260b1217 A-Konig
                }
266
267
            }
268
269
            // last day
270 a53b1de8 A-Konig
            DateTime startT = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, startingTime, 0, 0);
271 d39750f3 A-Konig
            for (int k = 0; k < TagInfo.buildings.Length; k++)
272 260b1217 A-Konig
            {
273 a53b1de8 A-Konig
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.buildings[k], recordedAmount[k], startT, interval);
274
                if (recordedAmount[k] != 0)
275
                    jisInfo.Add(dayInfo);
276 260b1217 A-Konig
            }
277
278
            return jisInfo;
279 5d9a5bd9 A-Konig
        }
280
    }
281
}