Projekt

Obecné

Profil

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

    
30
            // get all files in folder
31
            string[] fileEntries = Directory.GetFiles(folder);
32
            foreach (string fileName in fileEntries)
33
            {
34
                List<ActivityInfo> loadedData = null;
35
                
36
                // parse as one instance per day
37
                if (wholeDay)
38
                    loadedData = ProcessOneLogInFileAsDays(fileName);
39
                // parse by interval length
40
                else
41
                    loadedData = ProcessOneLoginFileAsIntervals(fileName, interval);
42

    
43
                list.AddRange(loadedData);
44
            }
45

    
46
            return list;
47
        }
48

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

    
59
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
60
            List<LogInInstance> list = CsvDataLoader.LoadLoginFile(path);
61

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

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

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

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

    
93
                // aggregate data
94
                if (TagInfo.buildingTags.ContainsKey(place))
95
                {
96
                    int index = TagInfo.buildingTags[place];
97
                    // to all
98
                    if (index == -1)
99
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
100
                            recordedAmount[l] += list[i].amount;
101
                    // to first two
102
                    else if (index == -2)
103
                    {
104
                        recordedAmount[0] += list[i].amount;
105
                        recordedAmount[1] += list[i].amount;
106
                    }
107
                    else
108
                        recordedAmount[index] += list[i].amount;
109

    
110
                }
111
                else
112
                {
113
                    // TODO uknown code handling
114
                    Console.WriteLine("Unknown code " + list[i].building);
115
                }
116

    
117
            }
118

    
119
            // last day
120
            for (int k = 0; k < TagInfo.faculties.Length; k++)
121
            {
122
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range);
123
                loginInfo.Add(dayInfo);
124
            }
125

    
126
            return loginInfo;
127
        }
128

    
129
        private static List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval)
130
        {
131
            if (!File.Exists(path))
132
                return null;
133

    
134
            List<ActivityInfo> loignInfo = new List<ActivityInfo>();
135
            List<LogInInstance> list = CsvDataLoader.LoadLoginFile(path);
136

    
137
            // data for each faculty
138
            int[] recordedAmount = new int[TagInfo.faculties.Length];
139
            // min/max hour taken into account
140
            int[] minmaxHour = new int[] { 7, 18 };
141
            int range = minmaxHour[1] - minmaxHour[0];
142

    
143
            if (interval > range)
144
                return null;
145

    
146
            int indices = (int)Math.Ceiling(range / (double)interval);
147
            int[] to = new int[indices];
148
            int[][] data = new int[indices][];
149
            for (int i = 0; i < to.Length; i++)
150
            {
151
                to[i] = minmaxHour[0] + interval * (i + 1);
152
                data[i] = new int[TagInfo.faculties.Length];
153
            }
154
            
155
            // 0.15h sem todle už dělal
156
            // +1h
157
            // udělat gitignore
158

    
159
            // first day
160
            DateTime lastStartTime = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
161
            int index = 0;
162
            for (int i = 0; i < list.Count; i++)
163
            {
164
                int currHour = list[i].lessonStart.Hour;
165
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
166
                    continue;
167

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

    
172
                // end of the day
173
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
174
                {
175
                    // note down all aggregated data
176
                    for (int k = 0; k < to.Length; k++)
177
                    {
178
                        DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
179

    
180
                        // data for each faculty separate
181
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
182
                        {
183
                            ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[l], data[k][l], stTime, interval);
184
                            loignInfo.Add(dayInfo);
185
                        }
186

    
187
                        data[k] = new int[TagInfo.faculties.Length];
188
                    }
189
                    lastStartTime = date;
190
                }
191

    
192
                // find index for current instance
193
                index = 0;
194
                for (int k = 1; k < to.Length; k++)
195
                {
196
                    if (to[k] > list[i].lessonStart.Hour && to[k - 1] <= list[i].lessonStart.Hour)
197
                    {
198
                        index = k;
199
                        break;
200
                    }
201
                }
202

    
203
                // aggregate data
204
                if (TagInfo.buildingTags.ContainsKey(place))
205
                {
206
                    int faculty = TagInfo.buildingTags[place];
207
                    // to all
208
                    if (faculty == -1)
209
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
210
                            data[index][l] += list[i].amount;
211
                    // to first two
212
                    else if (faculty == -2)
213
                    {
214
                        data[index][0] += list[i].amount;
215
                        data[index][1] += list[i].amount;
216
                    }
217
                    else
218
                        data[index][faculty] += list[i].amount;
219

    
220
                }
221
                else
222
                {
223
                    // TODO uknown code handling
224
                    Console.WriteLine("Unknown code " + list[i].building);
225
                }
226

    
227
            }
228

    
229
            for (int k = 0; k < to.Length; k++)
230
            {
231
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
232

    
233
                // data for each faculty separate
234
                for (int l = 0; l < TagInfo.faculties.Length; l++)
235
                {
236
                    ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[l], data[k][l], stTime, interval);
237
                    loignInfo.Add(dayInfo);
238
                }
239
            }
240

    
241
            return loignInfo;
242
        }
243

    
244
    }
245
}
(3-3/5)