Projekt

Obecné

Profil

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

    
7
namespace ServerApp.Parser.Parsers
8
{
9
    /// <summary>
10
    /// Class parsing jis files into instances of ActivityInfo divided by given time interval
11
    /// Data parsed from 7am (included) to 18pm (included)
12
    /// </summary>
13
    class JisParser
14
    {
15
        
16
        /// <summary>
17
        /// Parses jis data to ActivityInfo instances
18
        /// Data parsed from 7am (included) to 18pm (included)
19
        /// </summary>
20
        /// <param name="folder">Folder with login data files</param>
21
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
22
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
23
        /// <returns></returns>
24
        public List<ActivityInfo> ParseJisData(string folder, bool wholeDay = true, int interval = 1)
25
        {
26
            if (!Directory.Exists(folder))
27
                return null;
28

    
29
            List<ActivityInfo> list = new List<ActivityInfo>();
30

    
31
            // parse all files
32
            string[] fileEntries = Directory.GetFiles(folder);
33
            foreach (string fileName in fileEntries)
34
            {
35
                List<ActivityInfo> loadedData = null;
36

    
37
                // parse as one instance per day
38
                if (wholeDay)
39
                    loadedData = ProcessOneJisFileAsDays(fileName);
40
                // parse by interval length
41
                else
42
                    loadedData = ProcessOneJisFileAsIntervals(fileName, interval);
43

    
44
                list.AddRange(loadedData);
45
            }
46

    
47
            return list;
48
        }
49

    
50
        /// <summary>
51
        /// Parses data from one data file as one instance per day
52
        /// </summary>
53
        /// <param name="path">Path ti file</param>
54
        /// <returns>List with ActivityInfo instances</returns>
55
        private static List<ActivityInfo> ProcessOneJisFileAsDays(string path)
56
        {
57
            if (!File.Exists(path))
58
                return null;
59

    
60
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
61
            List<JisInstance> list =  CsvDataLoader.LoadJisFile(path);
62

    
63
            // data for each faculty
64
            int[] recordedAmount = new int[TagInfo.faculties.Length];
65
            // min/max hour taken into account
66
            int[] minmaxHour = new int[] { 7, 18 };
67
            // interval length
68
            int range = minmaxHour[1] - minmaxHour[0];
69

    
70
            // first day
71
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
72
            for (int i = 0; i < list.Count; i++)
73
            {
74
                int currHour = list[i].dateTime.Hour;
75
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
76
                    continue;
77

    
78
                // start of the day -> make an instance
79
                string place = list[i].placeTag;
80
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
81
                if (!date.Equals(lastStartTime)) 
82
                {
83
                    // data for each faculty separate
84
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
85
                    {
86
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
87
                        jisInfo.Add(dayInfo);
88
                    }
89

    
90
                    recordedAmount = new int[TagInfo.faculties.Length];
91
                    lastStartTime = date;
92
                }
93

    
94
                // aggregate data
95
                if (TagInfo.jisPlaces.ContainsKey(place))
96
                {
97
                    int index = TagInfo.jisPlaces[place];
98
                    if (index == -1)
99
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
100
                            recordedAmount[l] += list[i].amount;
101
                    else
102
                        recordedAmount[index] += list[i].amount;
103

    
104
                }
105
                else
106
                {
107
                    // TODO uknown code handling
108
                    Console.WriteLine("Unknown code " + list[i].placeTag);
109
                }
110

    
111
            }
112

    
113
            // last day
114
            for (int k = 0; k < TagInfo.faculties.Length; k++)
115
            {
116
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, range);
117
                jisInfo.Add(dayInfo);
118
            }
119

    
120
            return jisInfo;
121
        }
122

    
123
        private static List<ActivityInfo> ProcessOneJisFileAsIntervals(string path, int interval)
124
        {
125
            if (!File.Exists(path))
126
                return null;
127

    
128
            List<ActivityInfo> jisInfo = new List<ActivityInfo>();
129
            List<JisInstance> list = CsvDataLoader.LoadJisFile(path);
130

    
131
            // data for each faculty
132
            int[] recordedAmount = new int[TagInfo.faculties.Length];
133
            // min/max hour taken into account
134
            int[] minmaxHour = new int[] { 7, 18 };
135
            int range = minmaxHour[1] - minmaxHour[0];
136

    
137
            if (interval > range)
138
                return null;
139

    
140
            int indices = (int) Math.Ceiling(range / (double)interval);
141
            int[] from = new int[indices];
142
            for (int i = 0; i < from.Length; i++) {
143
                from[i] = minmaxHour[0] + interval * (i+1);
144
                Console.Write(from[i] + " ");
145
            }
146
            Console.WriteLine();
147

    
148
            // first day
149
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
150
            for (int i = 0; i < list.Count; i++)
151
            {
152
                int currHour = list[i].dateTime.Hour;
153
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
154
                    continue;
155

    
156
                // start of the day -> make an instance
157
                string place = list[i].placeTag;
158
                bool trigger = false;
159
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
160
               
161
                // end of the day
162
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
163
                    trigger = true;
164

    
165
                // end of interval period
166
                if ( (date.Hour - lastStartTime.Hour) >= interval || trigger)
167
                {
168
                    // data for each faculty separate
169
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
170
                    {
171
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, interval);
172
                        jisInfo.Add(dayInfo);
173
                    }
174

    
175
                    recordedAmount = new int[TagInfo.faculties.Length];
176
                    lastStartTime = date;
177
                }
178

    
179
                // aggregate data
180
                if (TagInfo.jisPlaces.ContainsKey(place))
181
                {
182
                    int index = TagInfo.jisPlaces[place];
183
                    if (index == -1)
184
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
185
                            recordedAmount[l] += list[i].amount;
186
                    else
187
                        recordedAmount[index] += list[i].amount;
188

    
189
                }
190
                else
191
                {
192
                    // TODO uknown code handling
193
                    Console.WriteLine("Unknown code " + list[i].placeTag);
194
                }
195

    
196
            }
197

    
198
            // last day
199
            for (int k = 0; k < TagInfo.faculties.Length; k++)
200
            {
201
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartTime, interval);
202
                jisInfo.Add(dayInfo);
203
            }
204

    
205
            return jisInfo;
206
        }
207
    }
208
}
(2-2/5)