@@ -1648,6 +1648,70 @@ public function sumDaily(array $queries = [], string $attribute = 'value'): int
16481648 return (is_array ($ json ) && isset ($ json ['data ' ][0 ]['total ' ])) ? (int ) $ json ['data ' ][0 ]['total ' ] : 0 ;
16491649 }
16501650
1651+ /**
1652+ * Sum multiple event metrics from the pre-aggregated daily table in one query.
1653+ *
1654+ * @param array<string> $metrics
1655+ * @param array<Query> $queries
1656+ * @return array<string, int>
1657+ * @throws Exception
1658+ */
1659+ public function sumDailyBatch (array $ metrics , array $ queries = []): array
1660+ {
1661+ if (empty ($ metrics )) {
1662+ return [];
1663+ }
1664+
1665+ $ this ->setOperationContext ('sumDailyBatch() ' );
1666+
1667+ $ totals = \array_fill_keys ($ metrics , 0 );
1668+
1669+ $ fromTable = $ this ->buildTableReference ($ this ->getEventsDailyTableName ());
1670+
1671+ // Build metric IN params
1672+ $ metricParams = [];
1673+ $ metricPlaceholders = [];
1674+ foreach ($ metrics as $ i => $ metric ) {
1675+ $ paramName = 'metric_ ' . $ i ;
1676+ $ metricParams [$ paramName ] = $ metric ;
1677+ $ metricPlaceholders [] = "{ {$ paramName }:String} " ;
1678+ }
1679+ $ metricInClause = implode (', ' , $ metricPlaceholders );
1680+
1681+ $ parsed = $ this ->parseQueries ($ queries , Usage::TYPE_EVENT );
1682+ $ params = array_merge ($ metricParams , $ parsed ['params ' ]);
1683+
1684+ $ whereData = $ this ->buildWhereClause ($ parsed ['filters ' ], $ params );
1685+ $ whereClause = $ whereData ['clause ' ];
1686+ $ params = $ whereData ['params ' ];
1687+
1688+ $ metricFilter = $ this ->escapeIdentifier ('metric ' ) . " IN ( {$ metricInClause }) " ;
1689+ $ whereClause = !empty ($ whereClause )
1690+ ? $ whereClause . ' AND ' . $ metricFilter
1691+ : ' WHERE ' . $ metricFilter ;
1692+
1693+ $ sql = "
1694+ SELECT metric, SUM(value) as total
1695+ FROM {$ fromTable }{$ whereClause }
1696+ GROUP BY metric
1697+ FORMAT JSON
1698+ " ;
1699+
1700+ $ result = $ this ->query ($ sql , $ params );
1701+ $ json = json_decode ($ result , true );
1702+
1703+ if (is_array ($ json ) && isset ($ json ['data ' ]) && is_array ($ json ['data ' ])) {
1704+ foreach ($ json ['data ' ] as $ row ) {
1705+ $ metricName = $ row ['metric ' ] ?? '' ;
1706+ if (isset ($ totals [$ metricName ])) {
1707+ $ totals [$ metricName ] = (int ) ($ row ['total ' ] ?? 0 );
1708+ }
1709+ }
1710+ }
1711+
1712+ return $ totals ;
1713+ }
1714+
16511715 /**
16521716 * Get time series data for metrics with query-time aggregation.
16531717 *
0 commit comments