blob: 087942750951cf81c0c23a1d4760566b11dc2b0f [file] [log] [blame]
package model
import (
"context"
"fmt"
"strconv"
"time"
"github.com/golang/glog"
"github.com/jmoiron/sqlx"
)
func (s *sqlModel) GetPeerCheckResults(ctx context.Context, asn []int64) ([]*PeerCheckResult, error) {
asns := make([]string, len(asn))
for i, asn := range asn {
asns[i] = fmt.Sprintf("%d", asn)
}
data := []struct {
sqlPeer `db:"peers"`
sqlPeerCheck `db:"peer_checks"`
}{}
q := `
SELECT
peers.asn "peers.asn",
peer_checks.check_name "peer_checks.check_name",
peer_checks.check_time "peer_checks.check_time",
peer_checks.check_status "peer_checks.check_status",
peer_checks.check_message "peer_checks.check_message"
FROM peers
LEFT JOIN peer_checks
ON peers.id = peer_checks.peer_id
WHERE peers.asn IN (?)
`
query, args, err := sqlx.In(q, asns)
if err != nil {
return nil, fmt.Errorf("SELECT peers: %v", err)
}
query = s.db.Rebind(query)
if err := s.db.SelectContext(ctx, &data, query, args...); err != nil {
return nil, fmt.Errorf("SELECT peers: %v", err)
}
res := make([]*PeerCheckResult, len(data))
for i, d := range data {
asn, err := strconv.ParseInt(d.sqlPeer.ASN, 10, 64)
if err != nil {
return nil, err
}
status := PeerCheckStatus_Invalid
switch d.sqlPeerCheck.CheckStatus {
case "okay":
status = PeerCheckStatus_Okay
case "failed":
status = PeerCheckStatus_Failed
default:
glog.Errorf("Unhandled check status %q", d.sqlPeerCheck.CheckStatus)
}
res[i] = &PeerCheckResult{
PeerASN: asn,
CheckName: d.sqlPeerCheck.CheckName,
Time: time.Unix(0, d.sqlPeerCheck.CheckTime),
Status: status,
Message: d.sqlPeerCheck.CheckMessage,
}
}
return res, nil
}