GroupByUntil with maxGroups

Split from #641

Purpose: limit the active groups to a certain number. If new group is opened, the oldest group is closed.