Projekt

Obecné

Profil

Stáhnout (5.17 KB) Statistiky
| Větev: | Tag: | Revize:
1 734533a8 A-Konig
using ServerApp.Parser.InputData;
2
using ServerApp.Parser.OutputInfo;
3 0da0ac88 A-Konig
using System;
4 5d9a5bd9 A-Konig
using System.Collections.Generic;
5 0da0ac88 A-Konig
using System.IO;
6 5d9a5bd9 A-Konig
7 734533a8 A-Konig
namespace ServerApp.Parser.Parsers
8 5d9a5bd9 A-Konig
{
9 734533a8 A-Konig
    /// <summary>
10
    /// Class parsing login files into instances of ActivityInfo divided by given time interval
11
    /// Data parsed from 7am (included) to 18pm (included)
12
    /// </summary>
13 5d9a5bd9 A-Konig
    class LogInParser
14
    {
15 734533a8 A-Konig
        /// <summary>
16
        /// Parses login data to ActivityInfo instances
17
        /// Data parsed from 7am (included) to 18pm (included)
18
        /// </summary>
19
        /// <param name="folder">Folder with login data files</param>
20
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
21
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
22
        /// <returns></returns>
23
        public List<ActivityInfo> ParseLogInData(string folder, bool wholeDay = true, int interval = 1)
24 0da0ac88 A-Konig
        {
25
            if (!Directory.Exists(folder))
26
                return null;
27 734533a8 A-Konig
            
28
            List<ActivityInfo> list = new List<ActivityInfo>();
29 0da0ac88 A-Konig
30 734533a8 A-Konig
            // get all files in folder
31 0da0ac88 A-Konig
            string[] fileEntries = Directory.GetFiles(folder);
32
            foreach (string fileName in fileEntries)
33
            {
34 734533a8 A-Konig
                List<ActivityInfo> loadedData = null;
35
                
36
                // parse as one instance per day
37 0da0ac88 A-Konig
                if (wholeDay)
38
                    loadedData = ProcessOneLogInFileAsDays(fileName);
39 734533a8 A-Konig
                // parse by interval length
40 0da0ac88 A-Konig
                else
41
                {
42
                    // pokud: konec dne nebo konec aktuálního intervalu -> vemu to co sem nasčítal
43
                    throw new NotImplementedException();
44
                }
45
46
                list.AddRange(loadedData);
47
            }
48
49
            return list;
50
        }
51
52 734533a8 A-Konig
        /// <summary>
53
        /// Parses data from one data file as one instance per day
54
        /// </summary>
55
        /// <param name="path">Path ti file</param>
56
        /// <returns>List with ActivityInfo instances</returns>
57
        private static List<ActivityInfo> ProcessOneLogInFileAsDays(string path)
58 0da0ac88 A-Konig
        {
59 734533a8 A-Konig
            if (!File.Exists(path))
60
                return null;
61 0da0ac88 A-Konig
62 734533a8 A-Konig
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
63 0da0ac88 A-Konig
            List<LogInInstance> list = CsvDataLoader.LoadLoginFile(path);
64
65 734533a8 A-Konig
            // data for each faculty
66 0da0ac88 A-Konig
            int[] recordedAmount = new int[TagInfo.faculties.Length];
67 734533a8 A-Konig
            // min/max hour taken into account
68 0da0ac88 A-Konig
            int[] minmaxHour = new int[] { 7, 18 };
69 734533a8 A-Konig
            // interval length
70 0da0ac88 A-Konig
            int range = minmaxHour[1] - minmaxHour[0];
71
72 734533a8 A-Konig
            // first day
73
            DateTime lastStartDay = new DateTime(list[0].date.Year, list[0].date.Month, list[0].date.Day, minmaxHour[0], 0, 0);
74 0da0ac88 A-Konig
            for (int i = 0; i < list.Count; i++)
75
            {
76 93452c41 A-Konig
                int currHour = list[i].lessonStart.Hour;
77 0da0ac88 A-Konig
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
78
                    continue;
79
80 734533a8 A-Konig
                // start of the day -> make an instance
81 0da0ac88 A-Konig
                string place = list[i].building;
82 734533a8 A-Konig
                DateTime date = new DateTime(list[i].date.Year, list[i].date.Month, list[i].date.Day, minmaxHour[0], 0, 0);
83 0da0ac88 A-Konig
                if (!date.Equals(lastStartDay))
84
                {
85 734533a8 A-Konig
                    // data for each faculty separate
86 0da0ac88 A-Konig
                    for (int k = 0; k < TagInfo.faculties.Length; k++)
87
                    {
88 734533a8 A-Konig
                        ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range); 
89 0da0ac88 A-Konig
                        loginInfo.Add(dayInfo);
90
                    }
91
92
                    recordedAmount = new int[TagInfo.faculties.Length];
93
                    lastStartDay = date;
94
                }
95
96 734533a8 A-Konig
                // aggregate data
97 0da0ac88 A-Konig
                if (TagInfo.buildingTags.ContainsKey(place))
98
                {
99
                    int index = TagInfo.buildingTags[place];
100 734533a8 A-Konig
                    // to all
101 0da0ac88 A-Konig
                    if (index == -1)
102
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
103
                            recordedAmount[l] += list[i].amount;
104 734533a8 A-Konig
                    // to first two
105 0da0ac88 A-Konig
                    else if (index == -2)
106
                    {
107
                        recordedAmount[0] += list[i].amount;
108
                        recordedAmount[1] += list[i].amount;
109
                    }
110
                    else
111
                        recordedAmount[index] += list[i].amount;
112
113
                }
114
                else
115
                {
116 734533a8 A-Konig
                    // TODO uknown code handling
117 0da0ac88 A-Konig
                    Console.WriteLine("Unknown code " + list[i].building);
118
                }
119
120
            }
121
122 734533a8 A-Konig
            // last day
123 0da0ac88 A-Konig
            for (int k = 0; k < TagInfo.faculties.Length; k++)
124
            {
125 734533a8 A-Konig
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range);
126 0da0ac88 A-Konig
                loginInfo.Add(dayInfo);
127
            }
128
129
            return loginInfo;
130
        }
131
132 734533a8 A-Konig
        private static List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval)
133 0da0ac88 A-Konig
        {
134
            throw new NotImplementedException();
135
        }
136
137 5d9a5bd9 A-Konig
    }
138
}