Projekt

Obecné

Profil

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