refactoring the query builder and adding key counts option. also sort get events by time for mark