-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (73 loc) · 1.8 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const Uri = "http://0.0.0.0:22022/evaluate/io/dsntk/DecisionContract/"
const SlaUri = Uri + "SLA"
const FineUri = Uri + "Fine"
const ContentType = "application/json"
type SlaParams struct {
YearsAsCustomer int64 `json:"YearsAsCustomer"`
NumberOfUnits int64 `json:"NumberOfUnits"`
}
type SlaResult struct {
Data int64 `json:"data"`
}
type FineParams struct {
YearsAsCustomer int64 `json:"YearsAsCustomer"`
NumberOfUnits int64 `json:"NumberOfUnits"`
DefectiveUnits float64 `json:"DefectiveUnits"`
}
type FineResult struct {
Data float64 `json:"data"`
}
func querySla(yearsAsCustomer int64, numberOfUnits int64) int64 {
slaParams := SlaParams{
YearsAsCustomer: yearsAsCustomer,
NumberOfUnits: numberOfUnits,
}
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(&slaParams)
if err != nil {
panic(err)
}
response, err := http.Post(SlaUri, ContentType, &body)
if err != nil {
panic(err)
}
slaResult := SlaResult{}
err = json.NewDecoder(response.Body).Decode(&slaResult)
if err != nil {
panic(err)
}
return slaResult.Data
}
func queryFine(yearsAsCustomer int64, numberOfUnits int64, defectiveUnits float64) float64 {
fineParams := FineParams{
YearsAsCustomer: yearsAsCustomer,
NumberOfUnits: numberOfUnits,
DefectiveUnits: defectiveUnits,
}
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(&fineParams)
if err != nil {
panic(err)
}
response, err := http.Post(FineUri, ContentType, &body)
if err != nil {
panic(err)
}
fineResult := FineResult{}
err = json.NewDecoder(response.Body).Decode(&fineResult)
if err != nil {
panic(err)
}
return fineResult.Data
}
func main() {
fmt.Printf("SLA = %d\n", querySla(1, 1000))
fmt.Printf("Fine = %.0f%%\n", queryFine(1, 1000, 0.034)*100)
}