-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.txt
214 lines (148 loc) · 4.59 KB
/
task.txt
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# API Implementation Exercise
In this exercise, you will implement an API in Golang that enables users to send requests for reading and writing alert data to a data storage system.
## Guidelines
- You can chose any storage system; a relational database, a file system, or even an in-memory data structure.
## Write Alerts
Users should be able to send requests to this API to write alert data to the chosen data storage.
### Write Request
**HTTP Method:** POST
**Endpoint:** /alerts
**Request Body:**
```json
{
"alert_id": "b950482e9911ec7e41f7ca5e5d9a424f",
"service_id": "my_test_service_id",
"service_name": "my_test_service",
"model": "my_test_model",
"alert_type": "anomaly",
"alert_ts": "1695644160",
"severity": "warning",
"team_slack": "slack_ch"
}
```
### Write Response
Success
**HTTP Status Code:** 200 OK
**Response Body:**
```json
{
"alert_id": "b950482e9911ec7e41f7ca5e5d9a424f",
"error": ""
}
```
Error
**HTTP Status Code:** 500 Internal Server Error
**Response Body:**
```json
{
"alert_id": "b950482e9911ec7e41f7ca5e5d9a424f",
"error": "<error details>"
}
```
## Read Alerts
Users should be able to query alerts using the `service_id` and specifying a time period defined by `start_ts` and `end_ts`.
### Read Request
**HTTP Method:** GET
**Endpoint:** /alerts
**Query Parameters:**
- `service_id`: The identifier of the service for which alerts are requested.
- `start_ts`: The starting timestamp epoch of the time period.
- `end_ts`: The ending timestamp epoch of the time period.
**Example:** `/alerts?service_id=my_test_service_id&start_ts=1695643160&end_ts=1695644360`
### Read Response
Success
**HTTP Status Code:** 200 OK
**Response Body:**
```json
{
"service_id" : "my_test_service_id"
"service_name": "my_test_service",
"alerts" : [
{
"alert_id": "b950482e9911ec7e41f7ca5e5d9a424f",
"model": "my_test_model",
"alert_type": "anomaly",
"alert_ts": "1695644060",
"severity": "warning",
"team_slack": "slack_ch"
},
{
"alert_id": "b950482e9911ecsdfs41f75e5d9az23cv",
"model": "my_test_model",
"alert_type": "anomaly",
"alert_ts": "1695644160",
"severity": "warning",
"team_slack": "slack_ch"
},
]
}
```
Error
**HTTP Status Code:** Appropriate HTTP error status (e.g., 400 Bad Request, 404 Not Found, 500 Internal Server Error)
**Response Body:**
```json
{
"alert_id": "b950482e9911ec7e41f7ca5e5d9a424f",
"error": "<error details>"
}
```
## Skeleton Project for Golang
Here is a sample skeleton code with chi router to get started:
```go
package main
import (
"fmt"
"log"
"net/http"
"encoding/json"
"github.com/go-chi/chi"
)
type Alert struct {
AlertID string `json:"alert_id"`
ServiceID string `json:"service_id"`
ServiceName string `json:"service_name"`
Model string `json:"model"`
AlertType string `json:"alert_type"`
AlertTS string `json:"alert_ts"`
Severity string `json:"severity"`
TeamSlack string `json:"team_slack"`
}
var alerts []Alert
func main() {
// Router setup
r := chi.NewRouter()
// Route requests
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from home"))
})
r.Post("/alerts", WriteAlert)
r.Get("/alerts", ReadAlerts)
// Server start
srv := &http.Server{
Addr: fmt.Sprintf(":%s", "8080"),
Handler: r,
}
log.Println("Server started...")
if err := srv.ListenAndServe(); err != nil {
log.Fatal(fmt.Sprintf("%+v", err))
}
}
// POST Request Handler (Write Alert)
func WriteAlert(w http.ResponseWriter, r *http.Request) {
// 1. Parse the JSON request body
// 2. Validate the input data
// 3. Store the alert data
// 4. Handle errors
// 5. Respond with an appropriate HTTP status code and JSON response
}
// GET Request Handler (Read Alerts)
func ReadAlerts(w http.ResponseWriter, r *http.Request) {
// 1. Parse and validate query parameters
// 2. Query data storage to retrieve alerts
// 3. Create a response JSON object
// 4. Handle errors
// 5. Respond with an appropriate HTTP status code and JSON response
}
```
This code adds the necessary routes for writing and reading alerts. You should fill in the details of each step according to your specific requirements and chosen data storage solution.
Make sure to import the required packages (encoding/json and github.com/go-chi/chi) and handle error cases appropriately. Also, consider adding unit tests to verify the functionality of your API.