Save the number of sites in JobStats as soon as it is available

See https://bugs.launchpad.net/oq-engine/+bug/1295964. The PR request also includes some small refactoring and cleanup. The tests are running: https://ci.openquake.org/job/zdevel_oq-engine/349/