Projekt

Obecné

Profil

Stáhnout (7.24 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ServerApp.Parser.InputData;
2
using ServerApp.Parser.OutputInfo;
3
using System;
4
using System.Collections.Generic;
5
using System.Globalization;
6
using System.Threading;
7

    
8
namespace ServerApp.Parser.Parsers
9
{
10
    /// <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
    /// <author>Alex Konig</author>
15
    class DataParser
16
    {
17
        /// <summary> Path to data folder </summary>
18
        string path;
19
        /// <summary> Weather data parser </summary>
20
        WeatherParser weatherParser;
21
        /// <summary> Jis data parser </summary>
22
        JisParser jisParser;
23
        /// <summary> Login data parser </summary>
24
        LogInParser loginParser;
25

    
26
        /// <summary> WeatherInfo </summary>
27
        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
        /// <summary> ActivityInfo representing jis activity </summary>
34
        List<ActivityInfo> jisList;
35
        /// <summary> ActivityInfo representing login activity</summary>
36
        List<ActivityInfo> loginList;
37

    
38
        /// <summary>
39
        /// Constructor
40
        /// </summary>
41
        public DataParser(string path)
42
        {
43
            TagInfo.CreateDictionaries();
44

    
45
            // TODO ask for data folder
46
            this.path = path;
47
            DataLoader loader = new CsvDataLoader();
48

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

    
54
        /// <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
        public bool Parse(DateTime startTime, DateTime endTime, int interval = 1, bool wholeDay = true)
60
        {
61
            // 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
            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
            weatherList = weatherParser.ParseWeatherData(pathWeather, wholeDay, interval);
72
            jisList = jisParser.ParseJisData(pathJis, wholeDay, interval);
73
            loginList = loginParser.ParseLogInData(pathLogIn, wholeDay, interval);
74

    
75
            //Console.WriteLine("WEATHER");
76
            //WriteToConsole(weatherList);
77
            //Console.WriteLine("JIS");
78
            //WriteToConsole(jisList);
79
            //Console.WriteLine("LOGIN");
80
            //WriteToConsole(loginList);
81

    
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
        }
194

    
195
        /// <summary>
196
        /// Debug method - writing lists to console
197
        /// </summary>
198
        private void WriteToConsole(List<WeatherInfo> list)
199
        {
200
            // TODO useless in finished app
201
            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
}
(1-1/6)