More complex message (preparation for knowing status of tapped items in UI) (#49)

* no message
* no message
This commit is contained in:
gadotroee 2021-05-23 13:11:59 +03:00 committed by GitHub
parent 8774639cbe
commit 5cbb5a011e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 41 deletions

View File

@ -38,8 +38,7 @@ api: ## build API server
docker: ## build Docker image docker: ## build Docker image
@(echo "building docker image" ) @(echo "building docker image" )
docker build -t up9inc/mizu:latest . ./build-push-featurebranch.sh
#./build-push-featurebranch.sh
push: push-docker push-cli ## build and publish Mizu docker image & CLI push: push-docker push-cli ## build and publish Mizu docker image & CLI

View File

@ -33,7 +33,7 @@ func init() {
go func() { go func() {
for { for {
select { select {
case err := <- errOut: case err := <-errOut:
fmt.Printf("name resolving error %s", err) fmt.Printf("name resolving error %s", err)
} }
} }
@ -97,7 +97,7 @@ func saveHarToDb(entry *har.Entry, sender string) {
serviceName, urlPath, serviceHostName := getServiceNameFromUrl(entry.Request.URL) serviceName, urlPath, serviceHostName := getServiceNameFromUrl(entry.Request.URL)
entryId := primitive.NewObjectID().Hex() entryId := primitive.NewObjectID().Hex()
var ( var (
resolvedSource *string resolvedSource *string
resolvedDestination *string resolvedDestination *string
) )
if k8sResolver != nil { if k8sResolver != nil {
@ -105,22 +105,23 @@ func saveHarToDb(entry *har.Entry, sender string) {
resolvedDestination = k8sResolver.Resolve(serviceHostName) resolvedDestination = k8sResolver.Resolve(serviceHostName)
} }
mizuEntry := models.MizuEntry{ mizuEntry := models.MizuEntry{
EntryId: entryId, EntryId: entryId,
Entry: string(entryBytes), // simple way to store it and not convert to bytes Entry: string(entryBytes), // simple way to store it and not convert to bytes
Service: serviceName, Service: serviceName,
Url: entry.Request.URL, Url: entry.Request.URL,
Path: urlPath, Path: urlPath,
Method: entry.Request.Method, Method: entry.Request.Method,
Status: entry.Response.Status, Status: entry.Response.Status,
RequestSenderIp: sender, RequestSenderIp: sender,
Timestamp: entry.StartedDateTime.UnixNano() / int64(time.Millisecond), Timestamp: entry.StartedDateTime.UnixNano() / int64(time.Millisecond),
ResolvedSource: resolvedSource, ResolvedSource: resolvedSource,
ResolvedDestination: resolvedDestination, ResolvedDestination: resolvedDestination,
} }
database.GetEntriesTable().Create(&mizuEntry) database.GetEntriesTable().Create(&mizuEntry)
baseEntry := utils.GetResolvedBaseEntry(mizuEntry) baseEntry := utils.GetResolvedBaseEntry(mizuEntry)
baseEntryBytes, _ := json.Marshal(&baseEntry) messageToSend := models.CreateBaseEntryWebSocketMessage(&baseEntry)
baseEntryBytes, _ := json.Marshal(&messageToSend)
broadcastToBrowserClients(baseEntryBytes) broadcastToBrowserClients(baseEntryBytes)
} }

View File

@ -1,21 +1,23 @@
package models package models
import "time" import (
"time"
)
type MizuEntry struct { type MizuEntry struct {
ID uint `gorm:"primarykey"` ID uint `gorm:"primarykey"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
Entry string `json:"entry,omitempty" gorm:"column:entry"` Entry string `json:"entry,omitempty" gorm:"column:entry"`
EntryId string `json:"entryId" gorm:"column:entryId"` EntryId string `json:"entryId" gorm:"column:entryId"`
Url string `json:"url" gorm:"column:url"` Url string `json:"url" gorm:"column:url"`
Method string `json:"method" gorm:"column:method"` Method string `json:"method" gorm:"column:method"`
Status int `json:"status" gorm:"column:status"` Status int `json:"status" gorm:"column:status"`
RequestSenderIp string `json:"requestSenderIp" gorm:"column:requestSenderIp"` RequestSenderIp string `json:"requestSenderIp" gorm:"column:requestSenderIp"`
Service string `json:"service" gorm:"column:service"` Service string `json:"service" gorm:"column:service"`
Timestamp int64 `json:"timestamp" gorm:"column:timestamp"` Timestamp int64 `json:"timestamp" gorm:"column:timestamp"`
Path string `json:"path" gorm:"column:path"` Path string `json:"path" gorm:"column:path"`
ResolvedSource *string `json:"resolvedSource,omitempty" gorm:"column:resolvedSource"` ResolvedSource *string `json:"resolvedSource,omitempty" gorm:"column:resolvedSource"`
ResolvedDestination *string `json:"resolvedDestination,omitempty" gorm:"column:resolvedDestination"` ResolvedDestination *string `json:"resolvedDestination,omitempty" gorm:"column:resolvedDestination"`
} }
@ -31,7 +33,7 @@ type BaseEntryDetails struct {
} }
type EntryData struct { type EntryData struct {
Entry string `json:"entry,omitempty"` Entry string `json:"entry,omitempty"`
ResolvedDestination *string `json:"resolvedDestination,omitempty" gorm:"column:resolvedDestination"` ResolvedDestination *string `json:"resolvedDestination,omitempty" gorm:"column:resolvedDestination"`
} }
@ -40,3 +42,41 @@ type EntriesFilter struct {
Operator string `query:"operator" validate:"required,oneof='lt' 'gt'"` Operator string `query:"operator" validate:"required,oneof='lt' 'gt'"`
Timestamp int64 `query:"timestamp" validate:"required,min=1"` Timestamp int64 `query:"timestamp" validate:"required,min=1"`
} }
type WebSocketMessageType string
const (
WebSocketMessageTypeEntry WebSocketMessageType = "entry"
// WebSocketMessageTypeUpdateStatus WebSocketMessageType = "status"
)
func CreateBaseEntryWebSocketMessage(base *BaseEntryDetails) *WebSocketEntryMessage {
return &WebSocketEntryMessage{
WebSocketMessageMetadata: &WebSocketMessageMetadata{
MessageType: WebSocketMessageTypeEntry,
},
Data: base,
}
}
//func CreateWebSocketStatusMessage() *WebSocketStatusMessage {
// return &WebSocketEntryMessage{
// WebSocketMessageMetadata: &WebSocketMessageMetadata{
// MessageType: WebSocketMessageTypeUpdateStatus,
// }
// }
//}
type WebSocketEntryMessage struct {
*WebSocketMessageMetadata
Data *BaseEntryDetails `json:"data,omitempty"`
}
type WebSocketStatusMessage struct {
*WebSocketMessageMetadata
}
type WebSocketMessageMetadata struct {
MessageType WebSocketMessageType `json:"messageType,omitempty"`
}

View File

@ -60,18 +60,29 @@ export const HarPage: React.FC = () => {
if(ws.current) { if(ws.current) {
ws.current.onmessage = e => { ws.current.onmessage = e => {
if(!e?.data) return; if(!e?.data) return;
const entry = JSON.parse(e.data); const message = JSON.parse(e.data);
if(connection === ConnectionStatus.Paused) {
setNoMoreDataBottom(false) switch (message.messageType) {
return; case "entry":
const entry = message.data
if(connection === ConnectionStatus.Paused) {
setNoMoreDataBottom(false)
return;
}
if(!focusedEntryId) setFocusedEntryId(entry.id)
let newEntries = [...entries];
if(entries.length === 1000) {
newEntries = newEntries.splice(1);
setNoMoreDataTop(false);
}
setEntries([...newEntries, entry])
break
case "updateStatus":
console.log("not implemented yet")
break
default:
console.error(`unsupported websocket message type, Got: ${message.messageType}`)
} }
if(!focusedEntryId) setFocusedEntryId(entry.id)
let newEntries = [...entries];
if(entries.length === 1000) {
newEntries = newEntries.splice(1);
setNoMoreDataTop(false);
}
setEntries([...newEntries, entry])
} }
} }