A library to assist in keeping and reporting statistics for some set of timed actions.
See here.
We assume that only Go applications will be using this library.
Use
$ go get github.com/cargregter/jc_assignmentUnit and integration tests are provided. A couple of mechanisms for running the tests are suggested.
From directory .../jc_assignment/action, use
$ go test --test.vGoConvey provides a nice framework within which to define test cases along with lots of matchers and a convenient web interface.
This library exposes two functions.
Add(action string) error: Used to add the details of an action, theactionparameter is a JSON string like
`{"action":"run", "time":75}`Units of the time value are your choice. An error is returned
if there is difficulty interpreting the input.
GetStats() string: Used to obtain a report of the statistics having been added. The report is in formatted JSON form like
[
{
"action": "jump",
"avg": 200
},
{
"action": "run",
"avg": 75
}
]In your code, add lines like the following as appropriate:
...
import "github.com/cargregter/jc_assignment/action"
...
errAdd := action.Add(`{"action":"jump", "time":100}`)
if errAdd != nil {
return errAdd
}
...
statsReport := action.GetStats()
if statsReport == "" {
return error.New("error generating statistics report")
}
...Both functions Add() and GetStats() are thread safe.
However, keep in mind that if you invoke GetStats() while
Add() is being invoked (or could be invoked) in parallel,
the returned statistics report may or may not include the latest
added actions.
Added actions are kept in memory. There are no provisions made to recover stored actions in the event that the application using this library should fail.
There may be value in writing actions to a persistent store or at least offering the option.
Of course, more statistics could be added as thought useful. It would likely mean capturing more raw data. But even with what we currently capture, one could see the usefulness of including the total number of observations for an action, and perhaps the mode and median of the provided times.
Currently actions can only be added. There is no contemplation of modifying or removing them, except that each added action is kept separately.
While all action detail is kept, there is currently no mechanism for dumping them for inspection.