-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (168 loc) · 4.64 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
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
183
184
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"go.elastic.co/apm/module/apmhttp"
"gopkg.in/go-playground/validator.v9"
)
type TransactionReq struct {
Amount float64 `json:"amount"`
Timestamp time.Time `json:"timestamp"`
}
type Response struct {
Sumvalue float64 `json:"sum"`
Average float64 `json:"avg"`
Maxvalue float64 `json:"max"`
Minvalue float64 `json:"min"`
Countvalue float64 `json:"count"`
}
type SliceAmount struct {
TimeStamp time.Time
Resvalue Response
}
var Value []TransactionReq
var ResponseSlice []SliceAmount
var Input TransactionReq
var responsesetStruct Response
var dataSlicestruct SliceAmount
func main() {
mux := NewRouter()
http.Handle("/", mux)
http.ListenAndServe(":8080", apmhttp.Wrap(mux))
}
func NewRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", appstart).Methods("POST")
r.HandleFunc("/transactions", Transactions).Methods("POST")
r.HandleFunc("/statistics", Statistics).Methods("GET")
r.HandleFunc("/transactions", DeleteTransactions).Methods("DELETE")
return r
}
func appstart(res http.ResponseWriter, req *http.Request) {
_, err := io.WriteString(res, "Hi!, This is the assignment application.")
log.Println("Successfully Listening on 8080 port ")
if err != nil {
return
}
}
func Transactions(res http.ResponseWriter, req *http.Request) {
// var Input TransactionReq
// var responsesetStruct Response
// var dataSlicestruct SliceAmount
err := json.NewDecoder(req.Body).Decode(&Input)
if err != nil {
fmt.Println("Incorrect Json Req", err.Error())
res.WriteHeader(422)
json.NewEncoder(res).Encode("Incorrect Json Req")
return
}
Valid := TransactionReq{
Amount: Input.Amount,
Timestamp: Input.Timestamp,
}
v := validator.New()
err = v.Struct(Valid)
if err != nil {
fmt.Println("Invalid Json Req", err.Error())
res.WriteHeader(400)
json.NewEncoder(res).Encode("Invalid Json Req")
return
}
fmt.Println("req body", Input)
presentTime := time.Now().UTC()
if presentTime.UTC().Sub(Input.Timestamp).Seconds() < 0 {
fmt.Println("future Transaction")
res.WriteHeader(422)
json.NewEncoder(res).Encode("Future Transaction")
return
}
if presentTime.Sub(Input.Timestamp).Seconds() > 60 {
fmt.Println("60 sec oldTransaction")
Value = append(Value, Input)
fmt.Println("valuecheck", Value)
res.WriteHeader(204)
return
}
Value = append(Value, Input)
var data TransactionReq
var max float64
var sum float64
var min float64
var count float64
fmt.Println("value", Value)
dataCount := len(Value)
fmt.Println("datacount value", dataCount)
if dataCount != 0 {
for i := 0; i < dataCount; i++ {
data = Value[i]
if time.Now().UTC().Sub(data.Timestamp).Seconds() <= 60 {
sum = sum + data.Amount
fmt.Println("sum", sum)
count++
fmt.Println("count", count)
if data.Amount > max {
max = data.Amount
fmt.Println("max", max)
}
if min == 0 {
min = data.Amount
fmt.Println("min", min)
} else if data.Amount < min {
min = data.Amount
}
}
}
}
responsesetStruct.Maxvalue = max
responsesetStruct.Minvalue = min
responsesetStruct.Sumvalue = sum
responsesetStruct.Countvalue = count
if count != 0 {
responsesetStruct.Average = sum / count
}
dataSlicestruct.TimeStamp = Input.Timestamp
dataSlicestruct.Resvalue = responsesetStruct
ResponseSlice = append(ResponseSlice, dataSlicestruct)
fmt.Println("Data slice ", ResponseSlice)
res.WriteHeader(201)
json.NewEncoder(res).Encode("Successfuly Saved")
return
}
func Statistics(res http.ResponseWriter, req *http.Request) {
var response Response
fmt.Println("response slice", ResponseSlice)
fmt.Println("Get req started")
presentTIme := time.Now().UTC()
sliceLength := len(ResponseSlice)
if sliceLength != 0 {
fmt.Println("entry into 1st loop")
reqSlice := ResponseSlice[sliceLength-1]
if presentTIme.Sub(reqSlice.TimeStamp).Seconds() <= 60 {
fmt.Println("enter into second loop")
response = reqSlice.Resvalue
res.WriteHeader(200)
fmt.Println("response for get req", response)
json.NewEncoder(res).Encode(response)
return
} else {
res.WriteHeader(200)
json.NewEncoder(res).Encode(response)
return
}
}
res.WriteHeader(200)
json.NewEncoder(res).Encode(response)
return
}
func DeleteTransactions(res http.ResponseWriter, req *http.Request) {
fmt.Println("Delete all req stored")
fmt.Println("Data stored are ", Value)
Value = nil
fmt.Println("data after clearing", Value)
res.WriteHeader(204)
}