2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-13 20:05:18 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
2018-02-10 13:34:42 +01:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Output struct {
|
|
|
|
Timestamp jsontime.Time `json:"timestamp"`
|
|
|
|
Status []*Status `json:"nodes"`
|
|
|
|
Links []*Link `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Source string `json:"source"`
|
2018-02-10 16:45:01 +01:00
|
|
|
SourceJID string `json:"source_jid"`
|
2018-02-10 13:34:42 +01:00
|
|
|
Target string `json:"target"`
|
2018-02-10 16:45:01 +01:00
|
|
|
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
|
|
|
|
}
|
2018-02-13 20:05:18 +01:00
|
|
|
toJID := messages.NewJID(to)
|
2018-02-10 13:34:42 +01:00
|
|
|
link := &Link{
|
|
|
|
Source: status.JID.Domain,
|
2018-02-10 16:45:01 +01:00
|
|
|
SourceJID: status.JID.Bare(),
|
2018-02-10 13:34:42 +01:00
|
|
|
Target: toJID.Domain,
|
2018-02-10 16:45:01 +01:00
|
|
|
TargetJID: toJID.Bare(),
|
2018-02-10 13:34:42 +01:00
|
|
|
FromSource: linkOK,
|
|
|
|
FromTarget: false,
|
|
|
|
}
|
|
|
|
if switchSourceTarget {
|
|
|
|
link.Source = toJID.Domain
|
2018-02-10 16:45:01 +01:00
|
|
|
link.SourceJID = toJID.Bare()
|
2018-02-10 13:34:42 +01:00
|
|
|
link.Target = status.JID.Domain
|
2018-02-10 16:45:01 +01:00
|
|
|
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
|
|
|
|
}
|