-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (82 loc) · 2.4 KB
/
main.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
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
package main
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"log"
"net/http"
"strings"
"xml-parser/calculation"
"xml-parser/models"
)
func main() {
http.HandleFunc("/kpis", kpis)
http.HandleFunc("/counters", counters)
log.Fatal(http.ListenAndServe(":8082", nil))
}
func kpis(w http.ResponseWriter, req *http.Request) {
var mdc models.MDC
siteDir, err := ioutil.ReadDir("data/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
println("data directory not found")
return
}
for _, site := range siteDir {
if site.IsDir() {
siteFiles, _ := ioutil.ReadDir("data/" + site.Name())
for _, file := range siteFiles {
xmlFile, err := ioutil.ReadFile("data/" + site.Name() + "/" + file.Name())
if err != nil {
println("Error in loading" + site.Name() + "data file...")
}
xml.Unmarshal(xmlFile, &mdc)
output := calculation.Calculate(mdc)
jsOutput, err := json.Marshal(output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
println("kips are ok!")
w.Header().Set("Content-Type", "application/json")
w.Write(jsOutput)
}
}
}
}
func counters(w http.ResponseWriter, req *http.Request) {
var mdc models.MDC
siteDir, err := ioutil.ReadDir("data/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
println("data directory not found")
return
}
for _, site := range siteDir {
if site.IsDir() {
siteFiles, _ := ioutil.ReadDir("data/" + site.Name())
for _, file := range siteFiles {
xmlFile, err := ioutil.ReadFile("data/" + site.Name() + "/" + file.Name())
if err != nil {
println("Error in loading" + site.Name() + "data file...")
}
xml.Unmarshal(xmlFile, &mdc)
siteName := strings.Split(strings.Split(mdc.Mfh.Sn, ",")[2], "=")[1]
countersModelList := calculation.PopulateCountersKeyValue(mdc, siteName)
for i, counterModel := range countersModelList {
outputIntCounter := calculation.ConvertCounterToInt(counterModel.StringCounters)
//counterModel.Counters = make(map[string]int)
countersModelList[i].Counters = outputIntCounter
}
jsOutput, err := json.Marshal(countersModelList)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
println("counters are ok!")
w.Header().Set("Content-Type", "application/json")
w.Write(jsOutput)
}
}
}
}