Implement representBasicPublish method

This commit is contained in:
M. Mert Yildiran
2021-08-21 13:15:17 +03:00
parent d4adc04c38
commit 5cf0fe952c
2 changed files with 168 additions and 17 deletions

View File

@@ -1,7 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/up9inc/mizu/tap/api"
@@ -90,7 +92,7 @@ type AMQPWrapper struct {
Details interface{}
}
func emitBasicPublish(eventBasicPublish BasicPublish, connectionInfo *api.ConnectionInfo, emitter api.Emitter) {
func emitBasicPublish(event BasicPublish, connectionInfo *api.ConnectionInfo, emitter api.Emitter) {
request := &api.GenericMessage{
IsRequest: true,
CaptureTime: time.Now(),
@@ -98,7 +100,7 @@ func emitBasicPublish(eventBasicPublish BasicPublish, connectionInfo *api.Connec
Type: "basic_publish",
Data: &AMQPWrapper{
Method: "Basic Publish",
Details: eventBasicPublish,
Details: event,
},
},
}
@@ -114,18 +116,160 @@ func emitBasicPublish(eventBasicPublish BasicPublish, connectionInfo *api.Connec
emitter.Emit(item)
}
func printEventBasicPublish(eventBasicPublish BasicPublish) {
return
fmt.Printf(
"[%s] Exchange: %s, RoutingKey: %s, Mandatory: %t, Immediate: %t, Properties: %v, Body: %s\n",
basicMethodMap[40],
eventBasicPublish.Exchange,
eventBasicPublish.RoutingKey,
eventBasicPublish.Mandatory,
eventBasicPublish.Immediate,
eventBasicPublish.Properties,
eventBasicPublish.Body,
)
func representBasicPublish(event map[string]interface{}) []interface{} {
rep := make([]interface{}, 0)
details, _ := json.Marshal([]map[string]string{
{
"name": "Exchange",
"value": event["Exchange"].(string),
},
{
"name": "Immediate",
"value": strconv.FormatBool(event["Immediate"].(bool)),
},
{
"name": "Mandatory",
"value": strconv.FormatBool(event["Mandatory"].(bool)),
},
})
rep = append(rep, map[string]string{
"type": "table",
"title": "Details",
"data": string(details),
})
contentType := ""
contentEncoding := ""
deliveryMode := ""
priority := ""
correlationId := ""
replyTo := ""
expiration := ""
messageId := ""
timestamp := ""
_type := ""
userId := ""
appId := ""
properties := event["Properties"].(map[string]interface{})
if properties["ContentType"] != nil {
contentType = properties["ContentType"].(string)
}
if properties["ContentEncoding"] != nil {
contentEncoding = properties["ContentEncoding"].(string)
}
if properties["Delivery Mode"] != nil {
deliveryMode = fmt.Sprintf("%g", properties["DeliveryMode"].(float64))
}
if properties["Priority"] != nil {
priority = fmt.Sprintf("%g", properties["Priority"].(float64))
}
if properties["CorrelationId"] != nil {
correlationId = properties["CorrelationId"].(string)
}
if properties["ReplyTo"] != nil {
replyTo = properties["ReplyTo"].(string)
}
if properties["Expiration"] != nil {
expiration = properties["Expiration"].(string)
}
if properties["MessageId"] != nil {
messageId = properties["MessageId"].(string)
}
if properties["Timestamp"] != nil {
timestamp = properties["Timestamp"].(string)
}
if properties["Type"] != nil {
_type = properties["Type"].(string)
}
if properties["UserId"] != nil {
userId = properties["UserId"].(string)
}
if properties["AppId"] != nil {
appId = properties["AppId"].(string)
}
props, _ := json.Marshal([]map[string]string{
{
"name": "Content Type",
"value": contentType,
},
{
"name": "Content Encoding",
"value": contentEncoding,
},
{
"name": "Delivery Mode",
"value": deliveryMode,
},
{
"name": "Priority",
"value": priority,
},
{
"name": "Correlation ID",
"value": correlationId,
},
{
"name": "Reply To",
"value": replyTo,
},
{
"name": "Expiration",
"value": expiration,
},
{
"name": "Message ID",
"value": messageId,
},
{
"name": "Timestamp",
"value": timestamp,
},
{
"name": "Type",
"value": _type,
},
{
"name": "User ID",
"value": userId,
},
{
"name": "App ID",
"value": appId,
},
})
rep = append(rep, map[string]string{
"type": "table",
"title": "Properties",
"data": string(props),
})
headers := make([]map[string]string, 0)
for name, value := range properties["Headers"].(map[string]interface{}) {
headers = append(headers, map[string]string{
"name": name,
"value": value.(string),
})
}
headersMarshaled, _ := json.Marshal(headers)
rep = append(rep, map[string]string{
"type": "table",
"title": "Headers",
"data": string(headersMarshaled),
})
rep = append(rep, map[string]string{
"type": "body",
"title": "Body",
"encoding": contentEncoding,
"mime_type": contentType,
"data": event["Body"].(string),
}) // FIXME: `Body` value seems wrong
return rep
}
func printEventBasicDeliver(eventBasicDeliver BasicDeliver) {

View File

@@ -103,7 +103,6 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
switch lastMethodFrameMessage.(type) {
case *BasicPublish:
eventBasicPublish.Body = f.Body
printEventBasicPublish(*eventBasicPublish)
emitBasicPublish(*eventBasicPublish, connectionInfo, emitter)
case *BasicDeliver:
eventBasicDeliver.Body = f.Body
@@ -258,11 +257,19 @@ func (d dissecting) Represent(entry string) ([]byte, error) {
var root map[string]interface{}
json.Unmarshal([]byte(entry), &root)
representation := make(map[string]interface{}, 0)
// request := root["request"].(map[string]interface{})["payload"].(map[string]interface{})
request := root["request"].(map[string]interface{})["payload"].(map[string]interface{})
log.Printf("request: %+v\n", request)
var repRequest []interface{}
details := request["Details"].(map[string]interface{})
switch request["Method"].(string) {
case "Basic Publish":
repRequest = representBasicPublish(details)
break
}
// response := root["response"].(map[string]interface{})["payload"].(map[string]interface{})
// repRequest := representRequest(request)
// repResponse := representResponse(response)
// representation["request"] = repRequest
representation["request"] = repRequest
// representation["response"] = repResponse
return json.Marshal(representation)
}