2 NNCP -- Node to Node copy
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/dustin/go-humanize"
28 "go.cypherpunks.ru/recfile"
31 func (ctx *Ctx) HumanizeRec(rec string) string {
32 r := recfile.NewReader(strings.NewReader(rec))
33 le, err := r.NextMap()
37 humanized, err := ctx.Humanize(le)
39 return fmt.Sprintf("Can not humanize: %s\n%s", err, rec)
44 func (ctx *Ctx) Humanize(le map[string]string) (string, error) {
46 node, err := ctx.FindNode(nodeS)
51 if sizeRaw, exists := le["Size"]; exists {
52 sp, err := strconv.ParseUint(sizeRaw, 10, 64)
56 size = humanize.IBytes(uint64(sp))
65 "File %s (%s) transfer to %s:%s: %s",
66 le["Src"], size, nodeS, le["Dst"], le["Msg"],
70 "File request from %s:%s to %s: %s",
71 nodeS, le["Src"], le["Dst"], le["Msg"],
75 "Exec to %s@%s (%s): %s",
76 nodeS, le["Dst"], size, le["Msg"],
80 "Transitional packet to %s (%s) (nice %s): %s",
81 nodeS, size, le["Nice"], le["Msg"],
84 return "", errors.New("unknown \"tx\" type")
86 if err, exists := le["Err"]; exists {
92 msg = fmt.Sprintf("Got exec from %s to %s (%s)", nodeS, le["Dst"], size)
94 msg = fmt.Sprintf("Got file %s (%s) from %s", le["Dst"], size, nodeS)
96 msg = fmt.Sprintf("Got file request %s to %s", le["Src"], nodeS)
99 node, err := ctx.FindNode(nodeT)
104 "Got transitional packet from %s to %s (%s)",
108 return "", errors.New("unknown \"rx\" type")
110 if err, exists := le["Err"]; exists {
114 msg = fmt.Sprintf("Checking: %s/%s/%s", le["Node"], le["XX"], le["Pkt"])
115 if err, exists := le["Err"]; exists {
116 msg += fmt.Sprintf(" %s", err)
121 msg = "Packet transfer, received from"
123 msg = "Packet transfer, sent to"
125 return "", errors.New("unknown XX")
128 msg += " node " + nodeS
131 msg += fmt.Sprintf(" (%s)", size)
133 if err, exists := le["Err"]; exists {
136 msg += " " + le["Msg"]
141 msg = "Bundle transfer, received from"
143 msg = "Bundle transfer, sent to"
145 return "", errors.New("unknown XX")
148 msg += " node " + nodeS
150 msg += " " + le["Pkt"]
152 msg += fmt.Sprintf(" (%s)", size)
154 if err, exists := le["Err"]; exists {
158 msg += "removing " + le["File"]
160 msg = fmt.Sprintf("Connection to %s", nodeS)
161 if err, exists := le["Err"]; exists {
165 rx, err := strconv.ParseUint(le["RxBytes"], 10, 64)
169 rxs, err := strconv.ParseUint(le["RxSpeed"], 10, 64)
173 tx, err := strconv.ParseUint(le["TxBytes"], 10, 64)
177 txs, err := strconv.ParseUint(le["TxSpeed"], 10, 64)
182 "Finished call with %s: %s received (%s/sec), %s transferred (%s/sec)",
184 humanize.IBytes(uint64(rx)), humanize.IBytes(uint64(rxs)),
185 humanize.IBytes(uint64(tx)), humanize.IBytes(uint64(txs)),
190 if peer, exists := le["Peer"]; exists {
191 msg += fmt.Sprintf(": %s", peer)
194 nice, err := NicenessParse(le["Nice"])
198 msg += fmt.Sprintf("SP with %s (nice %s)", nodeS, NicenessFmt(nice))
200 if m, exists := le["Msg"]; exists {
203 if err, exists := le["Err"]; exists {
208 nice, err := NicenessParse(le["Nice"])
213 "Packet %s (%s) (nice %s)",
214 le["Pkt"], size, NicenessFmt(nice),
216 offsetParsed, err := strconv.ParseUint(le["Offset"], 10, 64)
220 sizeParsed, err := strconv.ParseUint(le["Size"], 10, 64)
224 msg += fmt.Sprintf(": %d%%", 100*offsetParsed/sizeParsed)
225 if m, exists := le["Msg"]; exists {
231 msg = fmt.Sprintf("%s has got for us: ", nodeS)
233 msg = fmt.Sprintf("We have got for %s: ", nodeS)
235 return "", errors.New("unknown XX")
237 msg += fmt.Sprintf("%s packets, %s", le["Pkts"], size)
239 msg = fmt.Sprintf("%s has %s (%s): %s", nodeS, le["Pkt"], size, le["Msg"])
247 return "", errors.New("unknown XX")
249 fullsize, err := strconv.ParseUint(le["FullSize"], 10, 64)
253 sizeParsed, err := strconv.ParseUint(le["Size"], 10, 64)
260 100*sizeParsed/fullsize,
261 humanize.IBytes(uint64(sizeParsed)),
262 humanize.IBytes(uint64(fullsize)),
267 msg = fmt.Sprintf("Packet %s is retreived (%s)", le["Pkt"], size)
269 msg = fmt.Sprintf("Packet %s is sent", le["Pkt"])
271 return "", errors.New("unknown XX")
274 chunkNum, exists := le["Chunk"]
277 "Reassembling chunked file \"%s\" (chunk %s): %s",
278 le["Path"], chunkNum, le["Msg"],
282 "Reassembling chunked file \"%s\": %s",
283 le["Path"], le["Msg"],
286 if err, exists := le["Err"]; exists {
290 msg = fmt.Sprintf("Acquire lock for %s: %s", le["Path"], le["Err"])
292 return "", errors.New("unknown Who")
294 when, err := time.Parse(time.RFC3339Nano, le["When"])
299 if _, isErr := le["Err"]; isErr {
302 return fmt.Sprintf("%s %s%s", when.Format(time.RFC3339), level, msg), nil