Projekt

Obecné

Profil

Stáhnout (8.85 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 ce0940b5 Roman Kalivoda
            WeatherDataUsed = new List<string>();
85
            ActivityDataUsed = new List<string>();
86
87
            WeatherDataUsed.AddRange(weatherFiles);
88
            ActivityDataUsed.AddRange(jisFiles);
89
            ActivityDataUsed.AddRange(loginFiles);
90
91 99e5517e A-Konig
            WeatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
92 cdf3c217 A-Konig
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
93
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
94 93452c41 A-Konig
95 260b1217 A-Konig
            //Console.WriteLine("WEATHER");
96 23e8ae04 A-Konig
            //WriteToConsole(WeatherList);
97 9fb55c71 A-Konig
            //Console.WriteLine("JIS");
98
            //WriteToConsole(jisList);
99 ecdce631 A-Konig
            //Console.WriteLine("LOGIN");
100
            //WriteToConsole(loginList);
101 d39750f3 A-Konig
102
            //WriteToConsole(AddListToOne(jisList));
103
104 99e5517e A-Konig
            AttendanceList = MergeAttendance(jisList, loginList);
105 d39750f3 A-Konig
106 9fb55c71 A-Konig
            //Console.WriteLine("MERGED IN ONE LIST");
107 23e8ae04 A-Konig
            //WriteToConsole(AttendanceList);
108 d39750f3 A-Konig
109 99e5517e A-Konig
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
110 d39750f3 A-Konig
                return false;
111
112
            return true;
113
        }
114
115 99e5517e A-Konig
116 d39750f3 A-Konig
        /// <summary>
117 99e5517e A-Konig
        /// Merges two lists into one
118 d39750f3 A-Konig
        /// Adds information from the same time period together, doesn't change the rest
119
        /// </summary>
120 99e5517e A-Konig
        /// <param name="jisList">List to merge</param>
121
        /// <param name="loginList">List to merge</param>
122
        /// <returns>Merged list</returns>
123
        private List<ActivityInfo> MergeAttendance(List<ActivityInfo> jisList, List<ActivityInfo> loginList)
124 d39750f3 A-Konig
        {
125 99e5517e A-Konig
            List<ActivityInfo> res = new List<ActivityInfo>();
126
127
            if (jisList == null && loginList == null)
128
                return res;
129
            else if (jisList == null)
130
                return loginList;
131
            else if (loginList == null)
132
                return jisList;
133 d39750f3 A-Konig
134
            int indexJis = 0, indexLogin = 0;
135
            while (true)
136
            {
137 4cb8ae48 A-Konig
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
138 d39750f3 A-Konig
                    break;
139
140
                ActivityInfo jis = jisList[indexJis];
141
                int jisTag = TagInfo.IndexOfBuilding(jis.building);
142
                ActivityInfo login = loginList[indexLogin];
143
                int loginTag = TagInfo.IndexOfBuilding(login.building);
144
145
                // if same time times -> add to one info
146
                if (jis.startTime == login.startTime)
147
                {
148
                    // need to have same building tags
149
                    if (jisTag == loginTag)
150
                    {
151
                        jis.amount += login.amount;
152 99e5517e A-Konig
                        res.Add(jis);
153 d39750f3 A-Konig
                        indexJis++; indexLogin++;
154
                    }
155
156 99e5517e A-Konig
                    else if (jisTag < loginTag)
157 d39750f3 A-Konig
                    {
158
                        indexJis++;
159 99e5517e A-Konig
                        res.Add(jis);
160 d39750f3 A-Konig
                    }
161
162 99e5517e A-Konig
                    else if (jisTag > loginTag)
163 d39750f3 A-Konig
                    {
164
                        indexLogin++;
165 99e5517e A-Konig
                        res.Add(login);
166 d39750f3 A-Konig
                    }
167
168
                }
169
170
                // if jis time is smaller -> add jis and move
171 99e5517e A-Konig
                else if (jis.startTime < login.startTime)
172 d39750f3 A-Konig
                {
173
                    indexJis++;
174 99e5517e A-Konig
                    res.Add(jis);
175 d39750f3 A-Konig
                }
176
177
                // if login time is smaller -> add login and move
178 99e5517e A-Konig
                else if (login.startTime < jis.startTime)
179 d39750f3 A-Konig
                {
180
                    indexLogin++;
181 99e5517e A-Konig
                    res.Add(login);
182 d39750f3 A-Konig
                }
183
184
            }
185
186 4cb8ae48 A-Konig
            // add all not yet processed
187
            if (indexJis < jisList.Count)
188
                res.AddRange(jisList.GetRange(indexJis, jisList.Count - indexJis));
189
                    
190
                    
191
            if (indexLogin < loginList.Count)
192 a53b1de8 A-Konig
                res.AddRange(loginList.GetRange(indexLogin, loginList.Count - indexLogin));
193 4cb8ae48 A-Konig
194 99e5517e A-Konig
            return res;
195 d39750f3 A-Konig
        }
196
197
        /// <summary>
198
        /// Sum all info for separate intervals in the list to the appropriate day
199
        /// </summary>
200
        /// <param name="list">List with activity info, info separated into buildings and intervals</param>
201
        /// <returns></returns>
202
        private List<ActivityInfo> AddListToOne(List<ActivityInfo> list)
203
        {
204
            List<ActivityInfo> a2 = new List<ActivityInfo>();
205
            for (int i = 0; i < list.Count; i+=TagInfo.buildings.Length)
206
            {
207
                int amount = 0;
208
                for (int j = 0; j < TagInfo.buildings.Length; j++)
209
                {
210
                    amount += list[i + j].amount;
211
                }
212
                ActivityInfo info = new ActivityInfo("ALL", amount, list[i].startTime, list[i].intervalLength);
213
                a2.Add(info);
214
            }
215
            return a2;
216
        }
217
218
        /// <summary>
219
        /// Debug method - writing lists to console
220
        /// </summary>
221
        private void WriteToConsole(List<ActivityInfo> list)
222
        {
223
            // TODO useless in finished app
224
            if (list == null)
225
            {
226
                Console.WriteLine("Unsuccessful parsing");
227
                return;
228
            }
229
            Console.WriteLine(list.Count);
230
231
            for (int i = 0; i < list.Count; i++)
232 23e8ae04 A-Konig
            {
233
                if (list[i].startTime.Year >= 2019)
234
                    Console.WriteLine(list[i].ToString());
235
            }
236 93452c41 A-Konig
        }
237
238 734533a8 A-Konig
        /// <summary>
239
        /// Debug method - writing lists to console
240
        /// </summary>
241 d39750f3 A-Konig
        private void WriteToConsole(List<WeatherInfo> list)
242 93452c41 A-Konig
        {
243 734533a8 A-Konig
            // TODO useless in finished app
244 93452c41 A-Konig
            if (list == null)
245
            {
246
                Console.WriteLine("Unsuccessful parsing");
247
                return;
248
            }
249
            Console.WriteLine(list.Count);
250
251
            for (int i = 0; i < list.Count; i++)
252
                Console.WriteLine(list[i].ToString());
253
        }
254
255
    }
256
}