Projekt

Obecné

Profil

Stáhnout (8.52 KB) Statistiky
| Větev: | Tag: | Revize:
1
//
2
// Author: A. Konig
3
//
4

    
5
using ServerApp.DataDownload;
6
using ServerApp.Parser.InputData;
7
using ServerApp.Parser.OutputInfo;
8
using System;
9
using System.Collections.Generic;
10
using System.Globalization;
11
using System.Threading;
12

    
13
namespace ServerApp.Parser.Parsers
14
{
15
    /// <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
    /// <author>A. Konig</author>
20
    public class DataParser : IDataParser
21
    {
22
        /// <summary> Data downloader </summary>
23
        DataDownloader downloader;
24

    
25
        /// <summary> Weather data parser </summary>
26
        WeatherParser weatherParser;
27
        /// <summary> Jis data parser </summary>
28
        JisParser jisParser;
29
        /// <summary> Login data parser </summary>
30
        LogInParser loginParser;
31

    
32
        /// <summary> ActivityInfo representing jis activity </summary>
33
        List<ActivityInfo> jisList;
34
        /// <summary> ActivityInfo representing login activity</summary>
35
        List<ActivityInfo> loginList;
36

    
37
        /// <summary>
38
        /// Constructor
39
        /// </summary>
40
        public DataParser(DataDownloader downloader)
41
        {
42
            this.downloader = downloader;
43

    
44
            TagInfo.CreateDictionaries();
45

    
46
            IDataLoader loader = new CsvDataLoader();
47

    
48
            weatherParser = new WeatherParser(loader);
49
            jisParser = new JisParser(loader);
50
            loginParser = new LogInParser(loader);
51
        }
52

    
53
        /// <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
        /// <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
        {
62
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
63
            Thread.CurrentThread.CurrentCulture = cultureInfo;
64
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
65

    
66
            // get path to folder w/ datafiles
67
            string pathWeather = downloader.DataSubDirectories[DataType.POCASI];
68
            string pathJis = downloader.DataSubDirectories[DataType.JIS];
69
            string pathLogIn = downloader.DataSubDirectories[DataType.STROJE];
70

    
71
            // get all files that should be parsed
72
            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
            {
76
                start = null;
77
                end = null;
78
            }
79

    
80
            var weatherFiles = downloader.GetData(pathWeather, start, end);
81
            var jisFiles = downloader.GetData(pathJis, start, end);
82
            var loginFiles = downloader.GetData(pathLogIn, start, end);
83

    
84
            WeatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
85
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
86
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
87

    
88
            //Console.WriteLine("WEATHER");
89
            //WriteToConsole(weatherList);
90
            //Console.WriteLine("JIS");
91
            //WriteToConsole(jisList);
92
            //Console.WriteLine("LOGIN");
93
            //WriteToConsole(loginList);
94

    
95
            //WriteToConsole(AddListToOne(jisList));
96

    
97
            AttendanceList = MergeAttendance(jisList, loginList);
98

    
99
            //Console.WriteLine("MERGED IN ONE LIST");
100
            //WriteToConsole(attendanceList);
101

    
102
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
103
                return false;
104

    
105
            return true;
106
        }
107

    
108

    
109
        /// <summary>
110
        /// Merges two lists into one
111
        /// Adds information from the same time period together, doesn't change the rest
112
        /// </summary>
113
        /// <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
        {
118
            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

    
127
            int indexJis = 0, indexLogin = 0;
128
            while (true)
129
            {
130
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
131
                    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
                        res.Add(jis);
146
                        indexJis++; indexLogin++;
147
                    }
148

    
149
                    else if (jisTag < loginTag)
150
                    {
151
                        indexJis++;
152
                        res.Add(jis);
153
                    }
154

    
155
                    else if (jisTag > loginTag)
156
                    {
157
                        indexLogin++;
158
                        res.Add(login);
159
                    }
160

    
161
                }
162

    
163
                // if jis time is smaller -> add jis and move
164
                else if (jis.startTime < login.startTime)
165
                {
166
                    indexJis++;
167
                    res.Add(jis);
168
                }
169

    
170
                // if login time is smaller -> add login and move
171
                else if (login.startTime < jis.startTime)
172
                {
173
                    indexLogin++;
174
                    res.Add(login);
175
                }
176

    
177
            }
178

    
179
            // 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
                res.AddRange(loginList.GetRange(indexLogin, loginList.Count - indexLogin));
186

    
187
            return res;
188
        }
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
        }
227

    
228
        /// <summary>
229
        /// Debug method - writing lists to console
230
        /// </summary>
231
        private void WriteToConsole(List<WeatherInfo> list)
232
        {
233
            // TODO useless in finished app
234
            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
}
(1-1/6)