@@ -6,13 +6,13 @@ import WooFoundation
66// MARK: - StatsStoreV4
77//
88public final class StatsStoreV4 : Store {
9- private let siteVisitStatsRemote : SiteStatsRemote
9+ private let siteStatsRemote : SiteStatsRemote
1010 private let leaderboardsRemote : LeaderboardsRemote
1111 private let orderStatsRemote : OrderStatsRemoteV4
1212 private let productsRemote : ProductsRemote
1313
1414 public override init ( dispatcher: Dispatcher , storageManager: StorageManagerType , network: Network ) {
15- self . siteVisitStatsRemote = SiteStatsRemote ( network: network)
15+ self . siteStatsRemote = SiteStatsRemote ( network: network)
1616 self . leaderboardsRemote = LeaderboardsRemote ( network: network)
1717 self . orderStatsRemote = OrderStatsRemoteV4 ( network: network)
1818 self . productsRemote = ProductsRemote ( network: network)
@@ -90,6 +90,16 @@ public final class StatsStoreV4: Store {
9090 forceRefresh: forceRefresh,
9191 saveInStorage: saveInStorage,
9292 onCompletion: onCompletion)
93+ case . retrieveSiteSummaryStats( let siteID,
94+ let period,
95+ let quantity,
96+ let latestDateToInclude,
97+ let onCompletion) :
98+ retrieveSiteSummaryStats ( siteID: siteID,
99+ period: period,
100+ quantity: quantity,
101+ latestDateToInclude: latestDateToInclude,
102+ onCompletion: onCompletion)
93103 }
94104 }
95105}
@@ -164,7 +174,7 @@ private extension StatsStoreV4 {
164174
165175 let quantity = timeRange. siteVisitStatsQuantity ( date: latestDateToInclude, siteTimezone: siteTimezone)
166176
167- siteVisitStatsRemote . loadSiteVisitorStats ( for: siteID,
177+ siteStatsRemote . loadSiteVisitorStats ( for: siteID,
168178 siteTimezone: siteTimezone,
169179 unit: timeRange. siteVisitStatsGranularity,
170180 latestDateToInclude: latestDateToInclude,
@@ -174,7 +184,50 @@ private extension StatsStoreV4 {
174184 self ? . upsertStoredSiteVisitStats ( readOnlyStats: siteVisitStats, timeRange: timeRange)
175185 onCompletion ( . success( ( ) ) )
176186 case . failure( let error) :
177- onCompletion ( . failure( SiteVisitStatsStoreError ( error: error) ) )
187+ onCompletion ( . failure( SiteStatsStoreError ( error: error) ) )
188+ }
189+ }
190+ }
191+
192+ /// Retrieves the site summary stats for the provided site ID, period(s), and date, without saving them to the Storage layer.
193+ ///
194+ func retrieveSiteSummaryStats( siteID: Int64 ,
195+ period: StatGranularity ,
196+ quantity: Int ,
197+ latestDateToInclude: Date ,
198+ onCompletion: @escaping ( Result < SiteSummaryStats , Error > ) -> Void ) {
199+ if quantity == 1 {
200+ siteStatsRemote. loadSiteSummaryStats ( for: siteID,
201+ period: period,
202+ includingDate: latestDateToInclude) { result in
203+ switch result {
204+ case . success( let siteSummaryStats) :
205+ onCompletion ( . success( siteSummaryStats) )
206+ case . failure( let error) :
207+ onCompletion ( . failure( SiteStatsStoreError ( error: error) ) )
208+ }
209+ }
210+ } else {
211+ // If we are not fetching stats for a single period, we need to summarize the stats manually.
212+ // The remote summary stats endpoint only retrieves visitor stats for a single period.
213+ // We should only do this for periods of a month or greater; otherwise the visitor total is inaccurate.
214+ // See: pe5uwI-5c-p2
215+ siteStatsRemote. loadSiteVisitorStats ( for: siteID,
216+ unit: period,
217+ latestDateToInclude: latestDateToInclude,
218+ quantity: quantity) { result in
219+ switch result {
220+ case . success( let siteVisitStats) :
221+ let totalViews = siteVisitStats. items? . map ( { $0. views } ) . reduce ( 0 , + ) ?? 0
222+ let summaryStats = SiteSummaryStats ( siteID: siteID,
223+ date: siteVisitStats. date,
224+ period: siteVisitStats. granularity,
225+ visitors: siteVisitStats. totalVisitors,
226+ views: totalViews)
227+ onCompletion ( . success( summaryStats) )
228+ case . failure( let error) :
229+ onCompletion ( . failure( SiteStatsStoreError ( error: error) ) )
230+ }
178231 }
179232 }
180233 }
@@ -571,7 +624,7 @@ public enum StatsStoreV4Error: Error {
571624/// - statsModuleDisabled: Jetpack site stats module is disabled for the site.
572625/// - unknown: other error cases.
573626///
574- public enum SiteVisitStatsStoreError : Error {
627+ public enum SiteStatsStoreError : Error {
575628 case statsModuleDisabled
576629 case noPermission
577630 case unknown
0 commit comments