Run statistics on typed shell commands
Weekdays 0 to 6
The length 7
array created at the beginning of line 9 in Listing 2 provides count values at positions
through 6
for events recorded per individual weekday. The callback function increments these values by one for each incoming command according to the weekday found in the timestamp. An interesting property of a callback function defined inline in Go is that it has access to previously defined local variables, like countByDoW
, which also remains in scope even after the walking phase, further down in the for
loop from line 21.
The loop iterates over the index positions
to 6
and digs out the counters for the individual weekdays. Now how can an integer value be converted back into a weekday string? The time
package contains the Weekday
data type, which defines integer constants from
to 6
, which correspond to the weekdays Sunday through Saturday. Furthermore there is a String()
function that converts the constant values into English weekday strings. Line 22 determines the weekday following this scheme. Line 23 then only needs to output the strings, along with the cumulated counters. Figure 4 shows how to compile the listings to create a binary and a call to it. Running the program reveals that the user seems to do the most typing on Mondays.
Hit of the Week
Listing 3 shows another evaluation of the history data, revealing the three most frequently typed commands. As a data structure for counting identical commands, line 9 creates a hash map that assigns commands as strings to integer counters. The callback function starting in line 11 has access to the data structure and receives from histWalk()
both a timestamp and the string with the typed command line for each history line that passes by.
Listing 3
top3.go
01 package main 02 03 import ( 04 "fmt" 05 "sort" 06 ) 07 08 func main() { 09 cmds := map[string]int{} 10 11 err := histWalk(func(stamp int64, line string) error { 12 cmds[line]++ 13 return nil 14 }) 15 16 if err != nil { 17 panic(err) 18 } 19 20 type kv struct { 21 Key string 22 Value int 23 } 24 25 kvs := []kv{} 26 for k, v := range cmds { 27 kvs = append(kvs, kv{k, v}) 28 } 29 30 sort.Slice(kvs, func(i, j int) bool { 31 return kvs[i].Value > kvs[j].Value 32 }) 33 34 for i := 0; i < 3; i++ { 35 fmt.Printf("%s (%dx)\n", kvs[i].Key, kvs[i].Value) 36 } 37 }
More Work Due to Strict Types
At the end of the program run, the winners with the highest counter values are determined. But how do you separate the top three from the rest of the field? Due to their weak typing, scripting languages offer far more convenient methods for sorting a hash map by the values it contains. Go has much stricter typing rules, on the other hand, and requires a certain rigamarole to get the job done. First, line 20 creates a new data structure, a combination of a string named Key
and an integer named Value
.
Then the for
loop starts at line 26 and builds a sortable array from the hash map with the hash structure's keys and values. The sort.Slice()
function then sorts the array numerically in descending order by the Value
field (i.e., the integer values). After that, it is easy for the for
loop from line 34 on to output the top three as the first three elements of the sorted array slice.
Since Listing 3 does not need any extra packages, it is simply compiled with
go build top3.go histWalk.go
Shortly after this, a binary named top3
is available, which scans the history file and displays the winning trio of the most frequently typed commands (Listing 4).
Listing 4
The Winning Trio
$ ./top3 make (72x) vi histWalk.go (57x) vi top3.go (23x)
With some algorithmic tricks (e.g, by using a heap structure), the work for determining the top N from a list could be made even more efficient than through sorting the whole list. However, since the number of hand-typed commands is fairly manageable by computer standards, Listing 3 does not bother to do this.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)