Projekt

Obecné

Profil

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