]> Pileus Git - ~andy/rhawk/commitdiff
Add statistics functions
authorAndy Spencer <andy753421@gmail.com>
Mon, 12 Oct 2015 02:15:50 +0000 (02:15 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 12 Oct 2015 02:15:50 +0000 (02:15 +0000)
mkfile
spades.awk

diff --git a/mkfile b/mkfile
index b1ef8c44d57b650eed393c2520b9ae7f2ef8f16d..ed945c11d0509fd695098655c0233989f8aa4697 100644 (file)
--- a/mkfile
+++ b/mkfile
@@ -7,9 +7,12 @@ CPPFLAGS = -I/usr/include/awk \
            -DHAVE_STDBOOL_H   \
            -DHAVE_STDDEF_H
 
-default: select.so
+default:V: stats
 
-test:Q:
+stats:QV:
+       awk -f stats.awk
+
+test:QV:
        rm -f var/sp_cur.json
        #awk -f rhawk < testirc.txt
        #awk -f rhawk < testirc.txt
index a824464a7a6d6e42b7c99cb008d36d2b94e58c16..613976bc12cbb19fca798e148fbcca748137c2ee 100644 (file)
@@ -379,6 +379,63 @@ function sp_play(card,     winner, pi)
        }
 }
 
+# Statistics
+function sp_delay(sec)
+{
+       return (sec > 60*60*24 ? int(sec/60/60/24) "d " : "") \
+              (sec > 60*60    ? int(sec/60/60)%24 "h " : "") \
+                                int(sec/60)%60    "m"      
+}
+
+function sp_max(list,    i, max)
+{
+       for (i=0; i<length(list); i++)
+               if (max == "" || list[i] > max)
+                       max = list[i]
+       return max
+}
+
+function sp_avg(list,    i, sum)
+{
+       for (i=0; i<length(list); i++)
+               sum += list[i]
+       return sum / length(list)
+}
+
+function sp_stats(file,   line, arr, time, user, turn, start, delay)
+{
+       # Process log file
+       while (getline line < file) {
+               # Parse date
+               if (!match(line, /^([0-9\- \:]*) \| (.*)$/, arr))
+                       continue
+               gsub(/[:-]/, " ", arr[1])
+               time = mktime(arr[1])
+
+               # Parse user
+               if (!match(arr[2], /^([^:]*): (.*)$/, arr))
+                       continue
+               user = arr[1]
+
+               # Record user latency
+               if (turn) {
+                       delay[turn][length(delay[turn])] = time - start
+                       turn  = 0
+               }
+               if (match(arr[2], /^it is your.*$/, arr)) {
+                       turn  = user
+                       start = time
+               } 
+       }
+
+       # Output statistics
+       for (user in delay) {
+               say("latency for " user \
+                       ": " sp_delay(sp_avg(delay[user])) " (avg)" \
+                       ", " sp_delay(sp_max(delay[user])) " (max)")
+       }
+}
+
 # Misc
 BEGIN {
        cmd = "od -An -N4 -td4 /dev/random"
@@ -844,6 +901,11 @@ sp_state == "play" &&
        say("http://pileus.org/andy/spades/" sp_log)
 }
 
+(DST == sp_channel) &&
+/^\.stats/ {
+       sp_stats("logs/" sp_log);
+}
+
 /^\.((new|end|load)game|join|look|bid|pass|play|notify)/ {
        sp_save("var/sp_cur.json");
 }