-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcover.html
More file actions
347 lines (313 loc) · 12.3 KB
/
Copy pathcover.html
File metadata and controls
347 lines (313 loc) · 12.3 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>service: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/ansh-devs/commercelens/order-service/service/registry.go (0.0%)</option>
<option value="file1">github.com/ansh-devs/commercelens/order-service/service/service_impl.go (45.6%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package service
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/go-kit/log/level"
"github.com/hashicorp/consul/api"
)
func (s *OrderService) RegisterService(addr *string) <span class="cov0" title="0">{
ttl := time.Second * 2
checkClient := &api.AgentServiceCheck{
CheckID: "service_alive" + s.SrvID,
Name: "ORDER-SERVICE",
TTL: ttl.String(),
TLSSkipVerify: true,
DeregisterCriticalServiceAfter: ttl.String(),
Notes: "Agent alive",
}
port, err := strconv.Atoi(strings.Trim(*addr, ":"))
if err != nil </span><span class="cov0" title="0">{
_ = level.Error(s.logger).Log("err", err)
}</span>
<span class="cov0" title="0">srvRegister := &api.AgentServiceRegistration{
ID: s.SrvID,
Name: "ORDER-SERVICE",
Tags: []string{"order"},
Port: port,
Address: s.getLocalIP().String(),
Meta: map[string]string{"registered_at": time.Now().String()},
Check: checkClient,
}
if err := s.ConsulClient.Agent().ServiceRegister(srvRegister); err != nil </span><span class="cov0" title="0">{
_ = level.Error(s.logger).Log("err", err)
}</span>
}
func (s *OrderService) UpdateHealthStatus() <span class="cov0" title="0">{
ticker := time.NewTicker(time.Second * 1)
for </span><span class="cov0" title="0">{
if err := s.ConsulClient.Agent().UpdateTTL(
"service_alive"+s.SrvID,
"[MESSAGE]: Agent reachable",
api.HealthPassing,
); err != nil </span><span class="cov0" title="0">{
_ = level.Error(s.logger).Log("err", err)
}</span>
<span class="cov0" title="0"><-ticker.C</span>
}
}
func (s *OrderService) getLocalIP() net.IP <span class="cov0" title="0">{
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil </span><span class="cov0" title="0">{
_ = level.Error(s.logger).Log("err", "can't get local ip")
}</span>
<span class="cov0" title="0">defer func(conn net.Conn) </span><span class="cov0" title="0">{
err := conn.Close()
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
}(conn)
<span class="cov0" title="0">localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package service
import (
"context"
"fmt"
"math/rand"
"strconv"
"time"
"github.com/ansh-devs/commercelens/order-service/dto"
"github.com/ansh-devs/commercelens/order-service/natsutil"
"github.com/ansh-devs/commercelens/order-service/repo"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/hashicorp/consul/api"
"github.com/nats-io/nats.go"
"github.com/opentracing/opentracing-go"
)
// OrderService - Implementation of service interface for business logic...
type OrderService struct {
SrvID string
repository repo.Repository
logger log.Logger
ConsulClient *api.Client
trace opentracing.Tracer
nats *natsutil.NATSComponent
}
// NewOrderService - constructor for the OrderService...
func NewOrderService(rep repo.Repository, logger log.Logger, tracer opentracing.Tracer) *OrderService <span class="cov8" title="1">{
client, err := api.NewClient(&api.Config{
Address: "service-discovery:8500",
})
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
<span class="cov8" title="1">srvID := "instance_" + strconv.Itoa(rand.Intn(99))
return &OrderService{
repository: rep,
logger: log.With(logger, "layer", "service"),
ConsulClient: client,
trace: tracer,
SrvID: srvID,
}</span>
}
func (r *OrderService) InitNATS() <span class="cov0" title="0">{
nc := natsutil.NewNatsComponent(r.SrvID)
err := nc.ConnectToNATS("nats://nats-srvr:4222", nil)
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
<span class="cov0" title="0">r.nats = nc</span>
}
// PlaceOrder - dto wrapper function around the method that makes calls to the repository.
func (s *OrderService) PlaceOrder(ctx context.Context) <span class="cov0" title="0">{
msgHandler := func(msg *nats.Msg) </span><span class="cov0" title="0">{
spn := s.trace.StartSpan("place-order")
defer spn.Finish()
_ = level.Info(s.logger).Log("method-invoked", "PlaceOrder")
order, _ := s.nats.DecryptMsgToOrder(msg.Data)
buffUserID, err := s.nats.UserIdEncoder(order.UserId)
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
<span class="cov0" title="0">request, err := s.nats.NATS().Request("user.getdetails", buffUserID.Bytes(), time.Second*2)
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
<span class="cov0" title="0">time.Sleep(time.Second * 2)
user, err := s.nats.DecryptMsgToUser(request.Data)
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
<span class="cov0" title="0">newCtx := opentracing.ContextWithSpan(ctx, spn)
_, err = s.repository.PlaceOrder(newCtx, order.Product, user, spn)
if err != nil </span><span class="cov0" title="0">{
fmt.Println(err)
}</span>
}
<span class="cov0" title="0">for </span><span class="cov0" title="0">{
_, _ = s.nats.NATS().Subscribe("products.purchase", msgHandler)
}</span>
}
// GetOrder - place dto wrapper function around the method that makes calls to the repo...
func (s *OrderService) GetOrder(ctx context.Context, productId string) (dto.GetOrderResp, error) <span class="cov8" title="1">{
spn := s.trace.StartSpan("get-order")
defer spn.Finish()
_ = level.Info(s.logger).Log("method-invoked", "GetOrder for "+productId)
newCtx := opentracing.ContextWithSpan(ctx, spn)
result, err := s.repository.GetOrder(newCtx, productId, spn)
if err != nil </span><span class="cov0" title="0">{
return dto.GetOrderResp{}, err
}</span>
<span class="cov8" title="1">return dto.GetOrderResp{
Status: "successful",
Message: "ok",
Order: dto.Order{
ID: result.ID,
ProductID: result.ProductID,
UserID: result.UserID,
TotalCost: result.TotalCost,
Username: result.Username,
ProductName: result.ProductName,
Description: result.Description,
Price: result.Price,
Status: result.Status,
ShippingAddress: result.ShippingAddress,
},
}, nil</span>
}
// CancelOrder - place dto wrapper function around the method that makes calls to the repo...
func (s *OrderService) CancelOrder(ctx context.Context, orderId string) (dto.CancelOrderResp, error) <span class="cov8" title="1">{
_ = level.Info(s.logger).Log("method-invoked", "CancelOrder")
spn := s.trace.StartSpan("cancel-order")
defer spn.Finish()
newCtx := opentracing.ContextWithSpan(ctx, spn)
resp, err := s.repository.CancelOrder(newCtx, orderId, spn)
if err != nil </span><span class="cov0" title="0">{
return dto.CancelOrderResp{
Status: "failed",
Message: err.Error(),
}, nil
}</span>
<span class="cov8" title="1">if resp == "failed" </span><span class="cov8" title="1">{
return dto.CancelOrderResp{
Status: "failed",
Message: fmt.Sprintf("failed "),
}, nil
}</span> else<span class="cov0" title="0"> {
return dto.CancelOrderResp{
Status: "successful",
Message: "successfully canceled the order",
}, nil
}</span>
}
// GetAllUserOrders - place dto wrapper function around the method that makes calls to the repo...
func (s *OrderService) GetAllUserOrders(ctx context.Context, userId string) (dto.GetAllUserOrdersResp, error) <span class="cov8" title="1">{
_ = level.Info(s.logger).Log("method-invoked", "GetAllUserOrders")
spn := s.trace.StartSpan("get-all-user-orders")
defer spn.Finish()
newCtx := opentracing.ContextWithSpan(ctx, spn)
orders, err := s.repository.GetUserAllOrders(newCtx, userId, spn)
if err != nil </span><span class="cov0" title="0">{
return dto.GetAllUserOrdersResp{
Status: "failed",
Message: "failed to fetch your orders",
Orders: nil,
}, err
}</span>
<span class="cov8" title="1">return dto.GetAllUserOrdersResp{
Status: "successful",
Message: "list of all orders for the user",
Orders: orders,
}, nil</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>