sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/daemon/tester/output.go

80 lines
1.8 KiB
Go
Raw Normal View History

2018-02-10 13:34:42 +01:00
package tester
import (
"fmt"
"strings"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"dev.sum7.eu/genofire/yaja/xmpp/base"
2018-02-10 13:34:42 +01:00
)
type Output struct {
Timestamp jsontime.Time `json:"timestamp"`
Status []*Status `json:"nodes"`
Links []*Link `json:"links"`
}
type Link struct {
Source string `json:"source"`
SourceJID string `json:"source_jid"`
2018-02-10 13:34:42 +01:00
Target string `json:"target"`
TargetJID string `json:"target_jid"`
2018-02-10 13:34:42 +01:00
FromSource bool `json:"from_source"`
FromTarget bool `json:"from_target"`
}
func (t *Tester) Output() *Output {
output := &Output{
Timestamp: jsontime.Now(),
Status: make([]*Status, 0),
Links: make([]*Link, 0),
}
links := make(map[string]*Link)
for from, status := range t.Status {
output.Status = append(output.Status, status)
2018-02-11 11:19:56 +01:00
if !status.Login {
continue
}
2018-02-10 13:34:42 +01:00
for to, linkOK := range status.Connections {
var key string
// keep source and target in the same order
switchSourceTarget := strings.Compare(from, to) > 0
if switchSourceTarget {
key = fmt.Sprintf("%s-%s", from, to)
} else {
key = fmt.Sprintf("%s-%s", to, from)
}
if link := links[key]; link != nil {
if switchSourceTarget {
link.FromTarget = linkOK
} else {
link.FromSource = linkOK
}
continue
}
toJID := xmppbase.NewJID(to)
2018-02-10 13:34:42 +01:00
link := &Link{
Source: status.JID.Domain,
SourceJID: status.JID.Bare(),
2018-02-10 13:34:42 +01:00
Target: toJID.Domain,
TargetJID: toJID.Bare(),
2018-02-10 13:34:42 +01:00
FromSource: linkOK,
FromTarget: false,
}
if switchSourceTarget {
link.Source = toJID.Domain
link.SourceJID = toJID.Bare()
2018-02-10 13:34:42 +01:00
link.Target = status.JID.Domain
link.TargetJID = status.JID.Bare()
2018-02-10 13:34:42 +01:00
link.FromSource = false
link.FromTarget = linkOK
}
links[key] = link
output.Links = append(output.Links, link)
}
}
return output
}