Skip to content

Commit 1bd4563

Browse files
committed
Fetch site summary stats for the current selected period
1 parent dd6a894 commit 1bd4563

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/AnalyticsHubViewModel.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ private extension AnalyticsHubViewModel {
126126
try await self.retrieveOrderStats(currentTimeRange: currentTimeRange, previousTimeRange: previousTimeRange)
127127
}
128128
group.addTask {
129-
try await self.retrieveVisitorStats(currentTimeRange: currentTimeRange, previousTimeRange: previousTimeRange)
129+
try await self.retrieveItemsSoldStats(currentTimeRange: currentTimeRange, previousTimeRange: previousTimeRange)
130+
}
131+
group.addTask {
132+
try await self.retrieveSiteStats(currentTimeRange: currentTimeRange)
130133
}
131134
try await group.waitForAll()
132135
}
@@ -147,7 +150,7 @@ private extension AnalyticsHubViewModel {
147150
}
148151

149152
@MainActor
150-
func retrieveVisitorStats(currentTimeRange: AnalyticsHubTimeRange, previousTimeRange: AnalyticsHubTimeRange) async throws {
153+
func retrieveItemsSoldStats(currentTimeRange: AnalyticsHubTimeRange, previousTimeRange: AnalyticsHubTimeRange) async throws {
151154
async let itemsSoldRequest = retrieveTopItemsSoldStats(earliestDateToInclude: currentTimeRange.start,
152155
latestDateToInclude: currentTimeRange.end,
153156
forceRefresh: true)
@@ -156,6 +159,14 @@ private extension AnalyticsHubViewModel {
156159
self.itemsSoldStats = itemsSoldStats
157160
}
158161

162+
@MainActor
163+
func retrieveSiteStats(currentTimeRange: AnalyticsHubTimeRange) async throws {
164+
async let siteStatsRequest = retrieveSiteSummaryStats(latestDateToInclude: currentTimeRange.end)
165+
166+
let summaryStats = try await siteStatsRequest
167+
self.siteStats = summaryStats
168+
}
169+
159170
@MainActor
160171
func retrieveStats(earliestDateToInclude: Date,
161172
latestDateToInclude: Date,
@@ -191,6 +202,25 @@ private extension AnalyticsHubViewModel {
191202
stores.dispatch(action)
192203
}
193204
}
205+
206+
@MainActor
207+
/// Retrieves site summary stats using the `retrieveSiteSummaryStats` action.
208+
///
209+
func retrieveSiteSummaryStats(latestDateToInclude: Date) async throws -> SiteSummaryStats? {
210+
guard let period = timeRangeSelectionType.period else {
211+
return nil
212+
}
213+
214+
return try await withCheckedThrowingContinuation { continuation in
215+
let action = StatsActionV4.retrieveSiteSummaryStats(siteID: siteID,
216+
period: period,
217+
quantity: timeRangeSelectionType.quantity,
218+
latestDateToInclude: latestDateToInclude) { result in
219+
continuation.resume(with: result)
220+
}
221+
stores.dispatch(action)
222+
}
223+
}
194224
}
195225

196226
// MARK: Data - UI mapping
@@ -202,13 +232,15 @@ private extension AnalyticsHubViewModel {
202232
self.ordersCard = ordersCard.redacted
203233
self.productsStatsCard = productsStatsCard.redacted
204234
self.itemsSoldCard = itemsSoldCard.redacted
235+
self.sessionsCard = sessionsCard.redacted
205236
}
206237

207238
@MainActor
208239
func switchToErrorState() {
209240
self.currentOrderStats = nil
210241
self.previousOrderStats = nil
211242
self.itemsSoldStats = nil
243+
self.siteStats = nil
212244
}
213245

214246
func bindViewModelsWithData() {

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/Time Range/AnalyticsHubTimeRangeSelection.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,38 @@ extension AnalyticsHubTimeRangeSelection {
126126
}
127127
}
128128

129+
/// The period used to request site summary stats from the given SelectedType.
130+
///
131+
/// Returns `nil` if there isn't a `StatGranularity` period that can be used to fetch stats for the given SelectedType.
132+
///
133+
var period: StatGranularity? {
134+
switch self {
135+
case .custom:
136+
return nil
137+
case .today, .yesterday:
138+
return .day
139+
case .weekToDate, .lastWeek:
140+
return .week
141+
case .monthToDate, .lastMonth, .quarterToDate, .lastQuarter:
142+
return .month
143+
case .yearToDate, .lastYear:
144+
return .year
145+
}
146+
}
147+
148+
/// The quantity of periods used to request site summary stats from the given SelectedType.
149+
///
150+
/// Defaults to 1 (a single period) except for ranges not matching a `StatGranularity` period.
151+
///
152+
var quantity: Int {
153+
switch self {
154+
case .quarterToDate, .lastQuarter:
155+
return 3 // Stats summary calculated from 3 months of data
156+
default:
157+
return 1
158+
}
159+
}
160+
129161
init(_ statsTimeRange: StatsTimeRangeV4) {
130162
switch statsTimeRange {
131163
case .today:

0 commit comments

Comments
 (0)