Projekt

Obecné

Profil

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

    
7
namespace ServerApp.Parser.Parsers
8
{
9
    /// <summary>
10
    /// Class parsing jis 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 JisParser
15
    {
16

    
17
        /// <summary> Datafile loader  </summary>
18
        DataLoader loader;
19

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

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

    
42
            List<ActivityInfo> list = new List<ActivityInfo>();
43

    
44
            // parse all files
45
            string[] fileEntries = Directory.GetFiles(folder);
46
            foreach (string fileName in fileEntries)
47
            {
48
                List<ActivityInfo> loadedData = null;
49

    
50
                // parse as one instance per day
51
                if (wholeDay)
52
                    loadedData = ProcessOneJisFileAsDays(fileName);
53
                // parse by interval length
54
                else
55
                    loadedData = ProcessOneJisFileAsIntervals(fileName, interval);
56

    
57
                list.AddRange(loadedData);
58
            }
59

    
60
            return list;
61
        }
62

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

    
73
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
74
            List<JisInstance> list =  loader.LoadJisFile(path);
75

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

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

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

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

    
107
                // aggregate data
108
                if (TagInfo.jisPlaces.ContainsKey(place))
109
                {
110
                    int index = TagInfo.jisPlaces[place];
111
                    if (index == -1)
112
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
113
                            recordedAmount[l] += list[i].amount;
114
                    else
115
                        recordedAmount[index] += list[i].amount;
116

    
117
                }
118
                else
119
                {
120
                    // TODO uknown code handling
121
                    Console.WriteLine("Unknown code " + list[i].placeTag);
122
                }
123

    
124
            }
125

    
126
            // last day
127
            for (int k = 0; k < TagInfo.faculties.Length; k++)
128
            {
129
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
130
                jisInfo.Add(dayInfo);
131
            }
132

    
133
            return jisInfo;
134
        }
135

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

    
147
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
148
            List<JisInstance> list = loader.LoadJisFile(path);
149

    
150
            // data for each faculty
151
            int[] recordedAmount = new int[TagInfo.faculties.Length];
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
            for (int i = 0; i < to.Length; i++) 
162
                to[i] = minmaxHour[0] + interval * (i+1);
163

    
164
            // first day
165
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
166
            int endtime = to[0];
167
            int startTime = minmaxHour[0];
168
            int index = 0;
169
            for (int i = 0; i < list.Count; i++)
170
            {
171
                int currHour = list[i].dateTime.Hour;
172
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
173
                    continue;
174

    
175
                // start of the day -> make an instance
176
                string place = list[i].placeTag;
177
                bool trigger = false;
178
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
179

    
180
                // end of the day
181
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
182
                {
183
                    trigger = true;
184
                    index = 0;
185
                }
186

    
187
                // end of interval period
188
                if ( date.Hour >= endtime || trigger) 
189
                {
190
                    // start time of last interval
191
                    DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, startTime, 0, 0);
192

    
193
                    // find end and start time
194
                    if (!trigger)
195
                        index++;
196
                    while (date.Hour >= to[index])
197
                        index++;
198

    
199
                    endtime = to[index];
200
                    if (index == 0)
201
                        startTime = minmaxHour[0];
202
                    else
203
                        startTime = to[index - 1];
204

    
205
                    // data for each faculty separate
206
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
207
                    {
208
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], stTime, interval);
209
                        jisInfo.Add(dayInfo);
210
                    }
211

    
212
                    recordedAmount = new int[TagInfo.faculties.Length];
213
                    lastStartTime = date;
214
                }
215

    
216
                // aggregate data
217
                if (TagInfo.jisPlaces.ContainsKey(place))
218
                {
219
                    int faculty = TagInfo.jisPlaces[place];
220
                    if (faculty == -1)
221
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
222
                            recordedAmount[l] += list[i].amount;
223
                    else
224
                        recordedAmount[faculty] += list[i].amount;
225

    
226
                }
227
                else
228
                {
229
                    // TODO uknown code handling
230
                    Console.WriteLine("Unknown code " + list[i].placeTag);
231
                }
232

    
233
            }
234

    
235
            // last day
236
            for (int k = 0; k < TagInfo.faculties.Length; k++)
237
            {
238
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, interval);
239
                jisInfo.Add(dayInfo);
240
            }
241

    
242
            return jisInfo;
243
        }
244
    }
245
}
(3-3/6)