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)
52 if sizeRaw, exists := le["Size"]; exists {
53 sizeParsed, err = strconv.ParseUint(sizeRaw, 10, 64)
57 size = humanize.IBytes(sizeParsed)
66 "File %s (%s) transfer to %s:%s: %s",
67 le["Src"], size, nodeS, le["Dst"], le["Msg"],
71 "File request from %s:%s to %s: %s",
72 nodeS, le["Src"], le["Dst"], le["Msg"],
76 "Exec to %s@%s (%s): %s",
77 nodeS, le["Dst"], size, le["Msg"],
81 "Transitional packet to %s (%s) (nice %s): %s",
82 nodeS, size, le["Nice"], le["Msg"],
85 return "", errors.New("unknown \"tx\" type")
87 if err, exists := le["Err"]; exists {
93 msg = fmt.Sprintf("Got exec from %s to %s (%s)", nodeS, le["Dst"], size)
95 msg = fmt.Sprintf("Got file %s (%s) from %s", le["Dst"], size, nodeS)
97 msg = fmt.Sprintf("Got file request %s to %s", le["Src"], nodeS)
100 node, err := ctx.FindNode(nodeT)
105 "Got transitional packet from %s to %s (%s)",
109 return "", errors.New("unknown \"rx\" type")
111 if err, exists := le["Err"]; exists {
115 msg = fmt.Sprintf("Checking: %s/%s/%s", le["Node"], le["XX"], le["Pkt"])
116 if err, exists := le["Err"]; exists {
117 msg += fmt.Sprintf(" %s", err)
122 msg = "Packet transfer, received from"
124 msg = "Packet transfer, sent to"
126 return "", errors.New("unknown XX")
129 msg += " node " + nodeS
132 msg += fmt.Sprintf(" (%s)", size)
134 if err, exists := le["Err"]; exists {
137 msg += " " + le["Msg"]
142 msg = "Bundle transfer, received from"
144 msg = "Bundle transfer, sent to"
146 return "", errors.New("unknown XX")
149 msg += " node " + nodeS
151 msg += " " + le["Pkt"]
153 msg += fmt.Sprintf(" (%s)", size)
155 if err, exists := le["Err"]; exists {
159 msg += "removing " + le["File"]
161 msg = fmt.Sprintf("Connection to %s", nodeS)
162 if err, exists := le["Err"]; exists {
166 rx, err := strconv.ParseUint(le["RxBytes"], 10, 64)
170 rxs, err := strconv.ParseUint(le["RxSpeed"], 10, 64)
174 tx, err := strconv.ParseUint(le["TxBytes"], 10, 64)
178 txs, err := strconv.ParseUint(le["TxSpeed"], 10, 64)
183 "Finished call with %s: %s received (%s/sec), %s transferred (%s/sec)",
185 humanize.IBytes(uint64(rx)), humanize.IBytes(uint64(rxs)),
186 humanize.IBytes(uint64(tx)), humanize.IBytes(uint64(txs)),
191 if peer, exists := le["Peer"]; exists {
192 msg += fmt.Sprintf(": %s", peer)
195 nice, err := NicenessParse(le["Nice"])
199 msg += fmt.Sprintf("SP with %s (nice %s)", nodeS, NicenessFmt(nice))
201 if m, exists := le["Msg"]; exists {
204 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 if offset := le["Offset"]; offset != "" {
217 offsetParsed, err := strconv.ParseUint(offset, 10, 64)
221 msg += fmt.Sprintf(": %d%%", 100*offsetParsed/sizeParsed)
223 if m, exists := le["Msg"]; exists {
229 msg = fmt.Sprintf("%s has got for us: ", nodeS)
231 msg = fmt.Sprintf("We have got for %s: ", nodeS)
233 return "", errors.New("unknown XX")
235 msg += fmt.Sprintf("%s packets, %s", le["Pkts"], size)
237 msg = fmt.Sprintf("%s has %s (%s): %s", nodeS, le["Pkt"], size, le["Msg"])
245 return "", errors.New("unknown XX")
247 fullsize, err := strconv.ParseUint(le["FullSize"], 10, 64)
254 100*sizeParsed/fullsize,
255 humanize.IBytes(uint64(sizeParsed)),
256 humanize.IBytes(uint64(fullsize)),
261 msg = fmt.Sprintf("Packet %s is retreived (%s)", le["Pkt"], size)
263 msg = fmt.Sprintf("Packet %s is sent", le["Pkt"])
265 return "", errors.New("unknown XX")
268 chunkNum, exists := le["Chunk"]
271 "Reassembling chunked file \"%s\" (chunk %s): %s",
272 le["Path"], chunkNum, le["Msg"],
276 "Reassembling chunked file \"%s\": %s",
277 le["Path"], le["Msg"],
280 if err, exists := le["Err"]; exists {
284 msg = fmt.Sprintf("Acquire lock for %s: %s", le["Path"], le["Err"])
286 return "", errors.New("unknown Who")
288 when, err := time.Parse(time.RFC3339Nano, le["When"])
293 if _, isErr := le["Err"]; isErr {
296 return fmt.Sprintf("%s %s%s", when.Format(time.RFC3339), level, msg), nil