package cping import ( "strconv" "strings" "time" "rocketgit.ru/rsmon/worker/app/models" "rocketgit.ru/rsmon/worker/internal/checkresult" ) // Result is the outcome of a single ping check. It embeds // checkresult.CheckResult for the standard fields (State, Error, // Duration, Warnings, Infos) and adds Ping-specific metrics consumed // by the metrics writers when the check produces influx telemetry. type Result struct { checkresult.CheckResult PacketsSent int PacketsRecv int AvgRttMs float64 } // InfluxFields reports ping metrics in the same shape as the rest of // the check packages: "took" is elapsed ms (for graphs and alerts). func (r *Result) InfluxFields() map[string]interface{} { ret := make(map[string]interface{}, 0) ret["took"] = int64(r.Duration / time.Millisecond) ret["packets_sent"] = r.PacketsSent ret["packets_recv"] = r.PacketsRecv ret["packet_loss"] = lossPercent(r.PacketsSent, r.PacketsRecv) if r.AvgRttMs > 0 { ret["rtt_ms"] = int64(r.AvgRttMs) } return ret } // InfluxTags returns the standard set of tags used by the chttp/cdns // packages. The "state" tag is taken from the embedded CheckResult // after Perform() has populated it so the writer sees the actual // final state (OK/ERR/FAIL/WARN). func (r *Result) InfluxTags(c models.Check) map[string]string { //nolint:gocritic // hugeParam: accepted for interface compatibility ret := make(map[string]string, 0) ret["check"] = strconv.FormatInt(c.ID, 10) ret["state"] = r.State if r.Error != nil { ret["error"] = r.Error.Error() } ret["warnings"] = strings.Join(r.Warnings, ",") return ret } func lossPercent(sent, recv int) int64 { if sent <= 0 { return 0 } if recv >= sent { return 0 } return int64(100 * (sent - recv) / sent) }