$now - 5 * 60, 'Last 15 minutes' => $now - 15 * 60, 'Last hour' => $now - 60 * 60, 'Last day' => $now - 24 * 60 * 60, ]; foreach ($timeFrames as $label => $time) { $qb = $this->connection->getQueryBuilder(); $result = $qb->select($qb->createFunction('COUNT(DISTINCT ' . $qb->getColumnName('uid') . ')')) ->from('authtoken') ->where($qb->expr()->gte('last_activity', $qb->createNamedParameter($time))) ->executeQuery(); yield new Metric((int)$result->fetchOne(), ['time' => $label]); } } }