-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
59 lines (46 loc) · 1.59 KB
/
analyze.go
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
package electricity
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/mtenrero/electricity-bill-analyzer/analyzers"
"github.com/mtenrero/electricity-bill-analyzer/parser"
)
func FullMeanAnalysis(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Access-Control-Allow-Origin", "*")
(w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
r.ParseMultipartForm(10 << 20)
file, _, err := r.FormFile("file")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, "Could not read request", http.StatusBadRequest)
}
consumptions, err := parser.ParseCSVBytes(b)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte("Error processing CSV data file. It may not have the appropriate format"))
}
weekDaysHourReport := analyzers.ReportWeekDaysHourly(&consumptions)
hourlyReport := analyzers.ReportHourly(&consumptions)
weekDaysReport := analyzers.ReportWeekDays(&consumptions)
fullReport := analyzers.ResponseReport{
WeekDaysReport: weekDaysReport,
HourlyReport: hourlyReport,
WeekDayHoursReport: weekDaysHourReport,
}
jsonReport, err := json.Marshal(fullReport)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error generating JSON report"))
}
w.Header().Add("Content-Type", "application/json")
w.Write(jsonReport)
}