Projekt

Obecné

Profil

Stáhnout (8.55 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
    class JisParser
14
    {
15
        
16
        /// <summary>
17
        /// Parses jis data to ActivityInfo instances
18
        /// Data parsed from 7am (included) to 18pm (included)
19
        /// </summary>
20
        /// <param name="folder">Folder with login data files</param>
21
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
22
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
23
        /// <returns></returns>
24
        public List<ActivityInfo> ParseJisData(string folder, bool wholeDay = true, int interval = 1)
25
        {
26
            if (!Directory.Exists(folder))
27
                return null;
28

    
29
            List<ActivityInfo> list = new List<ActivityInfo>();
30

    
31
            // parse all files
32
            string[] fileEntries = Directory.GetFiles(folder);
33
            foreach (string fileName in fileEntries)
34
            {
35
                List<ActivityInfo> loadedData = null;
36

    
37
                // parse as one instance per day
38
                if (wholeDay)
39
                    loadedData = ProcessOneJisFileAsDays(fileName);
40
                // parse by interval length
41
                else
42
                    loadedData = ProcessOneJisFileAsIntervals(fileName, interval);
43

    
44
                list.AddRange(loadedData);
45
            }
46

    
47
            return list;
48
        }
49

    
50
        /// <summary>
51
        /// Parses data from one data file as one instance per day
52
        /// </summary>
53
        /// <param name="path">Path ti file</param>
54
        /// <returns>List with ActivityInfo instances</returns>
55
        private static List<ActivityInfo> ProcessOneJisFileAsDays(string path)
56
        {
57
            if (!File.Exists(path))
58
                return null;
59

    
60
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
61
            List<JisInstance> list =  CsvDataLoader.LoadJisFile(path);
62

    
63
            // data for each faculty
64
            int[] recordedAmount = new int[TagInfo.faculties.Length];
65
            // min/max hour taken into account
66
            int[] minmaxHour = new int[] { 7, 18 };
67
            // interval length
68
            int range = minmaxHour[1] - minmaxHour[0];
69

    
70
            // first day
71
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
72
            for (int i = 0; i < list.Count; i++)
73
            {
74
                int currHour = list[i].dateTime.Hour;
75
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
76
                    continue;
77

    
78
                // start of the day -> make an instance
79
                string place = list[i].placeTag;
80
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
81
                if (!date.Equals(lastStartTime)) 
82
                {
83
                    // data for each faculty separate
84
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
85
                    {
86
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
87
                        jisInfo.Add(dayInfo);
88
                    }
89

    
90
                    recordedAmount = new int[TagInfo.faculties.Length];
91
                    lastStartTime = date;
92
                }
93

    
94
                // aggregate data
95
                if (TagInfo.jisPlaces.ContainsKey(place))
96
                {
97
                    int index = TagInfo.jisPlaces[place];
98
                    if (index == -1)
99
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
100
                            recordedAmount[l] += list[i].amount;
101
                    else
102
                        recordedAmount[index] += list[i].amount;
103

    
104
                }
105
                else
106
                {
107
                    // TODO uknown code handling
108
                    Console.WriteLine("Unknown code " + list[i].placeTag);
109
                }
110

    
111
            }
112

    
113
            // last day
114
            for (int k = 0; k < TagInfo.faculties.Length; k++)
115
            {
116
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
117
                jisInfo.Add(dayInfo);
118
            }
119

    
120
            return jisInfo;
121
        }
122

    
123
        private static List<ActivityInfo> ProcessOneJisFileAsIntervals(string path, int interval)
124
        {
125
            if (!File.Exists(path))
126
                return null;
127

    
128
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
129
            List<JisInstance> list = CsvDataLoader.LoadJisFile(path);
130

    
131
            // data for each faculty
132
            int[] recordedAmount = new int[TagInfo.faculties.Length];
133
            // min/max hour taken into account
134
            int[] minmaxHour = new int[] { 7, 18 };
135
            int range = minmaxHour[1] - minmaxHour[0];
136

    
137
            if (interval > range)
138
                return null;
139

    
140
            int indices = (int) Math.Ceiling(range / (double)interval);
141
            int[] to = new int[indices];
142
            for (int i = 0; i < to.Length; i++) 
143
                to[i] = minmaxHour[0] + interval * (i+1);
144

    
145
            // first day
146
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
147
            int endtime = to[0];
148
            int startTime = minmaxHour[0];
149
            int index = 0;
150
            for (int i = 0; i < list.Count; i++)
151
            {
152
                int currHour = list[i].dateTime.Hour;
153
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
154
                    continue;
155

    
156
                // start of the day -> make an instance
157
                string place = list[i].placeTag;
158
                bool trigger = false;
159
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
160

    
161
                // end of the day
162
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
163
                {
164
                    trigger = true;
165
                    index = 0;
166
                }
167

    
168
                // end of interval period
169
                if ( date.Hour >= endtime || trigger) 
170
                {
171
                    // start time of last interval
172
                    DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, startTime, 0, 0);
173

    
174
                    // find end and start time
175
                    if (!trigger)
176
                        index++;
177
                    while (date.Hour >= to[index])
178
                        index++;
179

    
180
                    endtime = to[index];
181
                    if (index == 0)
182
                        startTime = minmaxHour[0];
183
                    else
184
                        startTime = to[index - 1];
185

    
186
                    // data for each faculty separate
187
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
188
                    {
189
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], stTime, interval);
190
                        jisInfo.Add(dayInfo);
191
                    }
192

    
193
                    recordedAmount = new int[TagInfo.faculties.Length];
194
                    lastStartTime = date;
195
                }
196

    
197
                // aggregate data
198
                if (TagInfo.jisPlaces.ContainsKey(place))
199
                {
200
                    int faculty = TagInfo.jisPlaces[place];
201
                    if (faculty == -1)
202
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
203
                            recordedAmount[l] += list[i].amount;
204
                    else
205
                        recordedAmount[faculty] += list[i].amount;
206

    
207
                }
208
                else
209
                {
210
                    // TODO uknown code handling
211
                    Console.WriteLine("Unknown code " + list[i].placeTag);
212
                }
213

    
214
            }
215

    
216
            // last day
217
            for (int k = 0; k < TagInfo.faculties.Length; k++)
218
            {
219
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, interval);
220
                jisInfo.Add(dayInfo);
221
            }
222

    
223
            return jisInfo;
224
        }
225
    }
226
}
(2-2/5)