Projekt

Obecné

Profil

Stáhnout (5.17 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.IO;
6

    
7
namespace ServerApp.Parser.Parsers
8
{
9
    /// <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
    class LogInParser
14
    {
15
        /// <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
        {
25
            if (!Directory.Exists(folder))
26
                return null;
27
            
28
            List<ActivityInfo> list = new List<ActivityInfo>();
29

    
30
            // get all files in folder
31
            string[] fileEntries = Directory.GetFiles(folder);
32
            foreach (string fileName in fileEntries)
33
            {
34
                List<ActivityInfo> loadedData = null;
35
                
36
                // parse as one instance per day
37
                if (wholeDay)
38
                    loadedData = ProcessOneLogInFileAsDays(fileName);
39
                // parse by interval length
40
                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
        /// <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
        {
59
            if (!File.Exists(path))
60
                return null;
61

    
62
            List<ActivityInfo> loginInfo = new List<ActivityInfo>();
63
            List<LogInInstance> list = CsvDataLoader.LoadLoginFile(path);
64

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

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

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

    
92
                    recordedAmount = new int[TagInfo.faculties.Length];
93
                    lastStartDay = date;
94
                }
95

    
96
                // aggregate data
97
                if (TagInfo.buildingTags.ContainsKey(place))
98
                {
99
                    int index = TagInfo.buildingTags[place];
100
                    // to all
101
                    if (index == -1)
102
                        for (int l = 0; l < TagInfo.faculties.Length; l++)
103
                            recordedAmount[l] += list[i].amount;
104
                    // to first two
105
                    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
                    // TODO uknown code handling
117
                    Console.WriteLine("Unknown code " + list[i].building);
118
                }
119

    
120
            }
121

    
122
            // last day
123
            for (int k = 0; k < TagInfo.faculties.Length; k++)
124
            {
125
                ActivityInfo dayInfo = new ActivityInfo(TagInfo.faculties[k], recordedAmount[k], lastStartDay, range);
126
                loginInfo.Add(dayInfo);
127
            }
128

    
129
            return loginInfo;
130
        }
131

    
132
        private static List<ActivityInfo> ProcessOneLoginFileAsIntervals(string path, int interval)
133
        {
134
            throw new NotImplementedException();
135
        }
136

    
137
    }
138
}
(3-3/5)