Showing off with shell “nr” (NumerousApp) command

I wrote some scripts demonstrating what you can do with all the bits and pieces I have now for manipulating NumerousApp metrics from python and shell.

In particular, I was interested in finding the maximum value of one of my metrics:3626358291300704487 (this is the actual NumerousApp metric ID)

I have already installed the python numerous client library and the corresponding shell command “nr” that goes with it (see the github page for details). I also already have my NUMEROUSAPIKEY environment variable set and exported. With all that in place I can read the current value of the metric from a shell prompt like this:

% nr 3626358291300704487

I can read the event stream with the “-E” option:

% nr -E 3626358291300704487

Each line of that output is a (somewhat) human-readable display of a value update event on the metric and looks something like this:

4 @ 2015-05-27T20:12:09.375Z by 9310717522265
3 @ 2015-05-27T19:12:09.348Z by 9310717522265 
2 @ 2015-05-27T18:12:09.207Z by 9310717522265 
1 @ 2015-05-27T17:12:09.373Z by 9310717522265 
0 @ 2015-05-27T15:42:11.373Z by 9310717522265 
518 @ 2015-05-27T14:42:09.254Z by 9310717522265 
517 @ 2015-05-27T13:42:09.116Z by 9310717522265 
516 @ 2015-05-27T12:42:09.317Z by 9310717522265 ...

Rather than parse that output I can ask the “nr” command to only print the value field for each of the events:

% nr -E '3626358291300704487[value]'

From there it’s a simple task to write an awk script that will display the maximum found on stdin:

awk 'BEGIN {max=-999999999} {if ($1>max) {max=$1}} END {print max}'

NOTE: In my script the awk command is all on one line. It’s likely showing up in your browser broken into multiple lines; you need to be careful about those line breaks if you are cutting and pasting this example from your browser display.

That script returns the wrong result if all the input values are negative and below the initial value in the BEGIN statement, so you could do it this way if you prefer:

awk 'BEGIN {max="NOTHING"} {if (max=="NOTHING") max = $1; if ($1>max) {max=$1}} END {print max}'

Again be wary of poorly-located line breaks in your browser display if you copy/paste this.

Given those pieces, finding the max value is a simple pipeline:

% nr -E '3626358291300704487[value]' |
awk 'BEGIN {max="NOTHING"} {if (max=="NOTHING") max = $1; if ($1>max) {max=$1}} END {print max}'

Pretty cool, or maybe somewhat demented.

Sometimes you might not have awk (some linux distributions don’t install it by default), so you could also compute the max with a python program. We know python is on the machine or none of this works, so here’s a version using a python program for the max:

MAXPGM="import sys
maxv = None
for x in sys.stdin:
        xval = int(x)
    except ValueError:
        xval = float(x)
    if maxv is None or xval > maxv:
       maxv = xval


nr -E '3626358291300704487[value]' | python -c "$MAXPGM"

Making this a script with more general options is left as an exercise for the reader.

Of course if we are going  to the trouble of writing a python program we might want to go ahead and write the entire script in python, which might arguably have been simpler. But, this example notwithstanding, it’s still fun to hack around and see how many powerful things you can do with the “nr” command and some shell.