Geoloc: a trigger to track the coordinates of each checkin

Thursday, September 01, 2016 0 Comments

While the office is not far, I code from home very often. So I want to keep track of the location of each checkin to extract some statistics later on, and figure out how much code I check in from each place. In fact, I have checked in some interesting code from planes too, and I added the info on the comments, which is fan to read later on.

So, I have been using a small trigger for about a week now, tracking the exact location of each of my checkins and adding the info as attributes to the changesets in Plastic.

The trigger code is available on GitHub and you are free to modify it and tune it for your own purposes. Each time I checkin I get a notification like this one on your desktop (Windows only so far):

What the trigger does?

Well, as I said, it is just a very simple "after-clientcheckin" trigger that obtains your location and shows a notification on the desktop and records the info associating a "geoloc" attribute to each changeset:

Since the geolocation API finds small variations on your coordinates even if you are on the same place, the trigger keeps a list of "well-known" locations. Each time a new location is detected, it is compared to each entry in the list of well-known ones, and if there is one not farther than 1000 m, it is selected. Otherwise, the trigger asks you to give a name to the new place:

The list of known places is stored at AppData\Local\plastic4\geolocatedcheckins.conf under your user directory.

How to install the trigger

I released the binaries of the trigger on GitHub so you don’t have to care about building the code if you don’t want to: https://github.com/PlasticSCM/geolocatedcheckin/releases

So just download the binaries, and then create a new trigger as follows:

cm maketrigger after-clientcheckin "add geolocation" "C:\Users\pablo\plasticscm\clienttriggers\geolocation.exe"

Where obviously you should enter your actual path.

There are options to specify a different attribute name (default is "geoloc").

How it is implemented

Well, it is really easy, but I think it is worth sharing :-) It is just a little bit of glue code around the System.Device.Location API plus some short code to automate the Plastic SCM command line (CmdRunner).

How to calculate the location on a Windows 10 machine is as simple as creating a new GeoCoordinateWatcher instance and letting it do its job. It is explained at: https://github.com/PlasticSCM/geolocatedcheckin/blob/master/CalculateLocation.cs

Then, I get extended info about the location (street, city and more) using the Google Maps API. I do it this way because the CivicAddress class never worked for me.

The GeoCoordinate class is capable of calculating distances between coordinates, so it is straightforward to know if a new location is close enough to an existing one.

The rest of my code is just about handling the notifications (I didn’t use the W10 Toast stuff to make it work well on older windozes), and asking for the name of the new location when found.

Finally, I used CmdRunner (stripped down version) to add attributes to changesets. Check the class PlasticAutomation on Program.cs on my repo. It is as simple as running "cm mkatt" command to create attributes, and "cm statt" to set a given attribute-value pair to a changeset.

As a final touch, I used the fabulous Fody/Costura to embed all the required dlls into a single exe, so that the trigger is much easier to distribute.

Use cm find to extract stats

You can use cm find to list all the locations where you checked in code from as follows:

cm find attribute where type='geoloc' --format="{date} {value}"
8/23/2016 13:42:57 home - 41.xxxxx
8/23/2016 14:12:15 home - 41.xxxxx
8/23/2016 15:34:01 office- 41.xxxxx

And then import in Excel or use some grepping to find out where you are more productive :-)

Conclusion and next steps

Well, triggers are good to enforce development policies, but can also be used to achieve simple and fun things like tracking the location of each commit, so that you can extract interesting stats later on.

The current version is just working on Windows, but it would be interesting to use web-based APIs to turn it into a cross-platform utility.

Finally, it is interesting to highlight how attributes can be used to track additional custom info.

0 comentarios: