@VisibleForTesting internal fun getHistorySearchGroups( metadata: List<HistoryMetadata>, ): List<HistoryGroupInternal> { return metadata .filter { it.totalViewTime > 0 && it.key.searchTerm != null } .groupBy { it.key.searchTerm!! } .mapValues { group -> // Within a group, we dedupe entries based on their url so we don't display // a page multiple times in the same group, and we sum up the total view time // of deduped entries while making sure to keep the latest updatedAt value. val metadataInGroup = group.value val metadataUrlGroups = metadataInGroup.groupBy { metadata -> metadata.key.url } metadataUrlGroups.map { metadata -> metadata.value.reduce { acc, elem -> acc.copy( totalViewTime = acc.totalViewTime + elem.totalViewTime, updatedAt = max(acc.updatedAt, elem.updatedAt), ) } } } .map { HistoryGroupInternal( groupName = it.key, groupItems = it.value, ) } .filter { it.groupItems.size >= SEARCH_GROUP_MINIMUM_SITES } }
Group all urls accessed following a particular search. Automatically dedupes identical urls and adds each url's view time to the group's total.