-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.go
More file actions
182 lines (169 loc) · 4.44 KB
/
Copy pathdataloader.go
File metadata and controls
182 lines (169 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bufio"
"encoding/csv"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"time"
)
func readParseSort(filename string, s Settings) []Entry {
settingsReader(filename)
csvContent := csvReader(filename, s)
data := extractCSVData(csvContent, s)
// sort by calendar week
sort.Slice(data, func(i, j int) bool {
return data[i].kw < data[j].kw
})
return data
}
func timeDiff(start string, end string, format string) time.Duration {
s, err := time.Parse(format, start)
if err != nil {
log.Print(err)
}
e, err := time.Parse(format, end)
if err != nil {
log.Print(err)
}
diff := e.Sub(s)
return diff
}
func parseError(s Settings) {
fmt.Printf("ttt detected an issue with the input file!\n\n")
fmt.Printf("Make sure it adheres to the layout: DATE START_TIME END_TIME JOBNAME\n")
fmt.Printf("Your current settings are:\n\nDelimiter: %s\nDate format: %s\nTime format: %s\nWeekly Hours: %s\n", string(s.csvDelim), s.datefmt, s.timefmt, s.weeklyHours.String())
fmt.Printf("\nError:\n")
}
func timeParse(timestring string, format string) time.Time {
time, err := time.Parse(format, timestring) //-> time.Time
if err != nil {
parseError(settings)
log.Fatal(err)
}
return time
}
func extractCSVData(input [][]string, s Settings) []Entry {
var tmpEntry Entry
var allEntries []Entry
// #todo: this assumes:
// date, start time, end time, job
for _, entry := range input {
tmpEntry.date = timeParse(entry[0], s.datefmt)
_, kw := tmpEntry.date.ISOWeek() // returns year and week as int
tmpEntry.kw = kw
tmpEntry.start = timeParse(entry[1], s.timefmt)
tmpEntry.end = timeParse(entry[2], s.timefmt)
tmpEntry.duration = timeDiff(entry[1], entry[2], s.timefmt)
tmpEntry.job = entry[3]
allEntries = append(allEntries, tmpEntry)
}
return allEntries
}
func settingsParser(set []string) {
const sep string = ":"
for _, line := range set {
if strings.Contains(line, "hours") {
Hours := hoursParser(line, sep)
fmt.Printf("hours: %d\n", Hours)
settings.weeklyHours = time.Duration(Hours * time.Hour)
} else if strings.Contains(line, "delimiter") {
delimiter := delimiterParser(line, sep)
fmt.Printf("delimiter: %s\n", string(delimiter))
settings.csvDelim = delimiter
} else if strings.Contains(line, "datefmt") {
dateformat := dateTimeFormat(line, sep)
fmt.Printf("date format: %s\n", dateformat)
settings.datefmt = dateformat
} else if strings.Contains(line, "timefmt") {
timeformat := dateTimeFormat(line, sep)
fmt.Printf("time format: %s\n\n", timeformat)
settings.timefmt = timeformat
} else {
continue
}
}
}
func dateTimeFormat(line string, sep string) string {
_, value, valid := strings.Cut(line, sep)
if !valid {
log.Fatal("not a valid config: \n", line)
}
dateTime := strings.TrimSpace(value)
return dateTime
}
func delimiterParser(line string, sep string) rune {
_, value, valid := strings.Cut(line, sep)
if !valid {
log.Fatal("not a valid config: \n", line)
}
value = strings.TrimSpace(value)
if value == "" {
val := []rune(" ")
return val[0]
}
val := []rune(value)
return val[0]
}
func hoursParser(line string, sep string) time.Duration {
_, value, valid := strings.Cut(line, sep)
if !valid {
log.Fatal("not a valid config: \n", line)
}
value = strings.TrimSpace(value)
hours, err := strconv.Atoi(value)
if err != nil {
log.Fatal(err)
}
return time.Duration(hours)
}
func settingsReader(fileName string) {
// file open
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var settings []string
for scanner.Scan() {
// omit empty lines
if scanner.Text() == "" {
continue
} else if scanner.Text()[0] != csvComment {
continue
}
settings = append(settings, scanner.Text())
}
// if no settings were applied we use the defaults
if len(settings) == 0 {
fmt.Printf("\nINFO No configuration found:\nUsing default settings\n\n")
} else {
settingsParser(settings)
}
}
func csvReader(fileName string, s Settings) [][]string {
// file open
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// csv reader init
reader := csv.NewReader(file)
// csv reader settings
reader.TrimLeadingSpace = true
reader.Comma = s.csvDelim
reader.Comment = csvComment
// csv parsing
// read entire file is fine because we deal with sub MB file sizes
content, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
return content
}