Re-factor code and add ability to query WMStats DB when dataset ids are missing