Projekt

Obecné

Profil

Stáhnout (8.52 KB) Statistiky
| Větev: | Tag: | Revize:
1 cdf3c217 A-Konig
//
2
// Author: A. Konig
3
//
4
5
using ServerApp.DataDownload;
6
using ServerApp.Parser.InputData;
7 98b568bc A-Konig
using ServerApp.Parser.OutputInfo;
8 93452c41 A-Konig
using System;
9
using System.Collections.Generic;
10
using System.Globalization;
11
using System.Threading;
12
13 734533a8 A-Konig
namespace ServerApp.Parser.Parsers
14 93452c41 A-Konig
{
15 734533a8 A-Konig
    /// <summary>
16
    /// Class parsing data files into instances of ActivityInfo and WeatherInfo divided by given time interval
17
    /// Data parsed from 7am (included) to 18pm (included)
18
    /// </summary>
19 cdf3c217 A-Konig
    /// <author>A. Konig</author>
20 99e5517e A-Konig
    public class DataParser : IDataParser
21 93452c41 A-Konig
    {
22 cdf3c217 A-Konig
        /// <summary> Data downloader </summary>
23
        DataDownloader downloader;
24
25 734533a8 A-Konig
        /// <summary> Weather data parser </summary>
26 93452c41 A-Konig
        WeatherParser weatherParser;
27 734533a8 A-Konig
        /// <summary> Jis data parser </summary>
28 93452c41 A-Konig
        JisParser jisParser;
29 734533a8 A-Konig
        /// <summary> Login data parser </summary>
30 93452c41 A-Konig
        LogInParser loginParser;
31
32 734533a8 A-Konig
        /// <summary> ActivityInfo representing jis activity </summary>
33 d39750f3 A-Konig
        List<ActivityInfo> jisList;
34 734533a8 A-Konig
        /// <summary> ActivityInfo representing login activity</summary>
35 d39750f3 A-Konig
        List<ActivityInfo> loginList;
36 78428231 A-Konig
37 734533a8 A-Konig
        /// <summary>
38
        /// Constructor
39
        /// </summary>
40 cdf3c217 A-Konig
        public DataParser(DataDownloader downloader)
41 93452c41 A-Konig
        {
42 cdf3c217 A-Konig
            this.downloader = downloader;
43
44 93452c41 A-Konig
            TagInfo.CreateDictionaries();
45 734533a8 A-Konig
46 cdf3c217 A-Konig
            IDataLoader loader = new CsvDataLoader();
47 93452c41 A-Konig
48 98b568bc A-Konig
            weatherParser = new WeatherParser(loader);
49
            jisParser = new JisParser(loader);
50
            loginParser = new LogInParser(loader);
51 93452c41 A-Konig
        }
52
53 734533a8 A-Konig
        /// <summary>
54
        /// Parse data
55
        /// </summary>
56
        /// <param name="interval">Length of an interval</param>
57
        /// <param name="wholeDay">Parse data as one instance per day</param>
58 cdf3c217 A-Konig
        /// <param name="endTime">End time of related data</param>
59
        /// <param name="startTime">Start time of related data</param>
60
        override public bool Parse(DateTime startTime, DateTime endTime, int interval = 1, bool wholeDay = true)
61 93452c41 A-Konig
        {
62
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
63
            Thread.CurrentThread.CurrentCulture = cultureInfo;
64
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
65
66 cdf3c217 A-Konig
            // get path to folder w/ datafiles
67 9fb55c71 A-Konig
            string pathWeather = downloader.DataSubDirectories[DataType.POCASI];
68
            string pathJis = downloader.DataSubDirectories[DataType.JIS];
69
            string pathLogIn = downloader.DataSubDirectories[DataType.STROJE];
70 cdf3c217 A-Konig
71
            // get all files that should be parsed
72 a53b1de8 A-Konig
            Date start = new Date((uint)startTime.Month, (uint)startTime.Year);
73
            Date end = new Date((uint)endTime.Month, (uint)endTime.Year);
74
            if (startTime == DateTime.MinValue || endTime == DateTime.MaxValue)
75 9fb55c71 A-Konig
            {
76 a53b1de8 A-Konig
                start = null;
77
                end = null;
78 9fb55c71 A-Konig
            }
79
80 cdf3c217 A-Konig
            var weatherFiles = downloader.GetData(pathWeather, start, end);
81
            var jisFiles = downloader.GetData(pathJis, start, end);
82
            var loginFiles = downloader.GetData(pathLogIn, start, end);
83 93452c41 A-Konig
84 99e5517e A-Konig
            WeatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
85 cdf3c217 A-Konig
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
86
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
87 93452c41 A-Konig
88 260b1217 A-Konig
            //Console.WriteLine("WEATHER");
89
            //WriteToConsole(weatherList);
90 9fb55c71 A-Konig
            //Console.WriteLine("JIS");
91
            //WriteToConsole(jisList);
92 ecdce631 A-Konig
            //Console.WriteLine("LOGIN");
93
            //WriteToConsole(loginList);
94 d39750f3 A-Konig
95
            //WriteToConsole(AddListToOne(jisList));
96
97 99e5517e A-Konig
            AttendanceList = MergeAttendance(jisList, loginList);
98 d39750f3 A-Konig
99 9fb55c71 A-Konig
            //Console.WriteLine("MERGED IN ONE LIST");
100
            //WriteToConsole(attendanceList);
101 d39750f3 A-Konig
102 99e5517e A-Konig
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
103 d39750f3 A-Konig
                return false;
104
105
            return true;
106
        }
107
108 99e5517e A-Konig
109 d39750f3 A-Konig
        /// <summary>
110 99e5517e A-Konig
        /// Merges two lists into one
111 d39750f3 A-Konig
        /// Adds information from the same time period together, doesn't change the rest
112
        /// </summary>
113 99e5517e A-Konig
        /// <param name="jisList">List to merge</param>
114
        /// <param name="loginList">List to merge</param>
115
        /// <returns>Merged list</returns>
116
        private List<ActivityInfo> MergeAttendance(List<ActivityInfo> jisList, List<ActivityInfo> loginList)
117 d39750f3 A-Konig
        {
118 99e5517e A-Konig
            List<ActivityInfo> res = new List<ActivityInfo>();
119
120
            if (jisList == null && loginList == null)
121
                return res;
122
            else if (jisList == null)
123
                return loginList;
124
            else if (loginList == null)
125
                return jisList;
126 d39750f3 A-Konig
127
            int indexJis = 0, indexLogin = 0;
128
            while (true)
129
            {
130 4cb8ae48 A-Konig
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
131 d39750f3 A-Konig
                    break;
132
133
                ActivityInfo jis = jisList[indexJis];
134
                int jisTag = TagInfo.IndexOfBuilding(jis.building);
135
                ActivityInfo login = loginList[indexLogin];
136
                int loginTag = TagInfo.IndexOfBuilding(login.building);
137
138
                // if same time times -> add to one info
139
                if (jis.startTime == login.startTime)
140
                {
141
                    // need to have same building tags
142
                    if (jisTag == loginTag)
143
                    {
144
                        jis.amount += login.amount;
145 99e5517e A-Konig
                        res.Add(jis);
146 d39750f3 A-Konig
                        indexJis++; indexLogin++;
147
                    }
148
149 99e5517e A-Konig
                    else if (jisTag < loginTag)
150 d39750f3 A-Konig
                    {
151
                        indexJis++;
152 99e5517e A-Konig
                        res.Add(jis);
153 d39750f3 A-Konig
                    }
154
155 99e5517e A-Konig
                    else if (jisTag > loginTag)
156 d39750f3 A-Konig
                    {
157
                        indexLogin++;
158 99e5517e A-Konig
                        res.Add(login);
159 d39750f3 A-Konig
                    }
160
161
                }
162
163
                // if jis time is smaller -> add jis and move
164 99e5517e A-Konig
                else if (jis.startTime < login.startTime)
165 d39750f3 A-Konig
                {
166
                    indexJis++;
167 99e5517e A-Konig
                    res.Add(jis);
168 d39750f3 A-Konig
                }
169
170
                // if login time is smaller -> add login and move
171 99e5517e A-Konig
                else if (login.startTime < jis.startTime)
172 d39750f3 A-Konig
                {
173
                    indexLogin++;
174 99e5517e A-Konig
                    res.Add(login);
175 d39750f3 A-Konig
                }
176
177
            }
178
179 4cb8ae48 A-Konig
            // add all not yet processed
180
            if (indexJis < jisList.Count)
181
                res.AddRange(jisList.GetRange(indexJis, jisList.Count - indexJis));
182
                    
183
                    
184
            if (indexLogin < loginList.Count)
185 a53b1de8 A-Konig
                res.AddRange(loginList.GetRange(indexLogin, loginList.Count - indexLogin));
186 4cb8ae48 A-Konig
187 99e5517e A-Konig
            return res;
188 d39750f3 A-Konig
        }
189
190
        /// <summary>
191
        /// Sum all info for separate intervals in the list to the appropriate day
192
        /// </summary>
193
        /// <param name="list">List with activity info, info separated into buildings and intervals</param>
194
        /// <returns></returns>
195
        private List<ActivityInfo> AddListToOne(List<ActivityInfo> list)
196
        {
197
            List<ActivityInfo> a2 = new List<ActivityInfo>();
198
            for (int i = 0; i < list.Count; i+=TagInfo.buildings.Length)
199
            {
200
                int amount = 0;
201
                for (int j = 0; j < TagInfo.buildings.Length; j++)
202
                {
203
                    amount += list[i + j].amount;
204
                }
205
                ActivityInfo info = new ActivityInfo("ALL", amount, list[i].startTime, list[i].intervalLength);
206
                a2.Add(info);
207
            }
208
            return a2;
209
        }
210
211
        /// <summary>
212
        /// Debug method - writing lists to console
213
        /// </summary>
214
        private void WriteToConsole(List<ActivityInfo> list)
215
        {
216
            // TODO useless in finished app
217
            if (list == null)
218
            {
219
                Console.WriteLine("Unsuccessful parsing");
220
                return;
221
            }
222
            Console.WriteLine(list.Count);
223
224
            for (int i = 0; i < list.Count; i++)
225
                Console.WriteLine(list[i].ToString());
226 93452c41 A-Konig
        }
227
228 734533a8 A-Konig
        /// <summary>
229
        /// Debug method - writing lists to console
230
        /// </summary>
231 d39750f3 A-Konig
        private void WriteToConsole(List<WeatherInfo> list)
232 93452c41 A-Konig
        {
233 734533a8 A-Konig
            // TODO useless in finished app
234 93452c41 A-Konig
            if (list == null)
235
            {
236
                Console.WriteLine("Unsuccessful parsing");
237
                return;
238
            }
239
            Console.WriteLine(list.Count);
240
241
            for (int i = 0; i < list.Count; i++)
242
                Console.WriteLine(list[i].ToString());
243
        }
244
245
    }
246
}