Skip to content

Commit 52fca2a

Browse files
committed
Add helpers for calculating the delta percentage for specific analytics stats
1 parent f64f9b9 commit 52fca2a

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Factories/StatsV4DataHelper.swift

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ final class StatsV4DataHelper {
2424
}
2525
}
2626

27+
/// Creates the text to display for the total revenue delta.
28+
///
29+
static func createTotalRevenueDelta(from previousPeriod: OrderStatsV4?, to currentPeriod: OrderStatsV4?, locale: Locale = .current) -> String {
30+
if let previousRevenue = totalRevenue(at: nil, orderStats: previousPeriod), let currentRevenue = totalRevenue(at: nil, orderStats: currentPeriod) {
31+
return createDeltaText(from: previousRevenue, to: currentRevenue, locale: locale)
32+
} else {
33+
return Constants.placeholderText
34+
}
35+
}
36+
2737
// MARK: Orders Stats
2838

2939
/// Creates the text to display for the order count.
@@ -36,6 +46,16 @@ final class StatsV4DataHelper {
3646
}
3747
}
3848

49+
/// Creates the text to display for the order count delta.
50+
///
51+
static func createOrderCountDelta(from previousPeriod: OrderStatsV4?, to currentPeriod: OrderStatsV4?, locale: Locale = .current) -> String {
52+
if let previousCount = orderCount(at: nil, orderStats: previousPeriod), let currentCount = orderCount(at: nil, orderStats: currentPeriod) {
53+
return createDeltaText(from: previousCount, to: currentCount, locale: locale)
54+
} else {
55+
return Constants.placeholderText
56+
}
57+
}
58+
3959
/// Creates the text to display for the average order value.
4060
///
4161
static func createAverageOrderValueText(orderStats: OrderStatsV4?, currencyFormatter: CurrencyFormatter, currencyCode: String) -> String {
@@ -48,6 +68,16 @@ final class StatsV4DataHelper {
4868
}
4969
}
5070

71+
/// Creates the text to display for the average order value delta.
72+
///
73+
static func createAverageOrderValueDelta(from previousPeriod: OrderStatsV4?, to currentPeriod: OrderStatsV4?, locale: Locale = .current) -> String {
74+
if let previousAverage = averageOrderValue(orderStats: previousPeriod), let currentAverage = averageOrderValue(orderStats: currentPeriod) {
75+
return createDeltaText(from: previousAverage, to: currentAverage, locale: locale)
76+
} else {
77+
return Constants.placeholderText
78+
}
79+
}
80+
5181
// MARK: Views and Visitors Stats
5282

5383
/// Creates the text to display for the visitor count.
@@ -60,6 +90,16 @@ final class StatsV4DataHelper {
6090
}
6191
}
6292

93+
/// Creates the text to display for the visitor count delta.
94+
///
95+
static func createVisitorCountDelta(from previousPeriod: SiteVisitStats?, to currentPeriod: SiteVisitStats?, locale: Locale = .current) -> String {
96+
if let previousCount = visitorCount(at: nil, siteStats: previousPeriod), let currentCount = visitorCount(at: nil, siteStats: currentPeriod) {
97+
return createDeltaText(from: previousCount, to: currentCount, locale: locale)
98+
} else {
99+
return Constants.placeholderText
100+
}
101+
}
102+
63103
// MARK: Conversion Stats
64104

65105
/// Creates the text to display for the conversion rate.
@@ -82,12 +122,15 @@ final class StatsV4DataHelper {
82122
return Constants.placeholderText
83123
}
84124
}
125+
}
126+
127+
extension StatsV4DataHelper {
85128

86129
// MARK: Delta Calculations
87130

88131
/// Creates the text showing the percent change from the previous `Decimal` value to the current `Decimal` value
89132
///
90-
static func createDeltaText(from previousValue: Decimal, to currentValue: Decimal, locale: Locale = Locale.current) -> String {
133+
static func createDeltaText(from previousValue: Decimal, to currentValue: Decimal, locale: Locale = .current) -> String {
91134
let numberFormatter = NumberFormatter()
92135
numberFormatter.numberStyle = .percent
93136
numberFormatter.positivePrefix = numberFormatter.plusSign
@@ -103,7 +146,7 @@ final class StatsV4DataHelper {
103146

104147
/// Creates the text showing the percent change from the previous `Double` value to the current `Double` value
105148
///
106-
static func createDeltaText(from previousValue: Double, to currentValue: Double, locale: Locale = Locale.current) -> String {
149+
static func createDeltaText(from previousValue: Double, to currentValue: Double, locale: Locale = .current) -> String {
107150
createDeltaText(from: Decimal(previousValue), to: Decimal(currentValue))
108151
}
109152

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/StatsV4DataHelperTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ final class StatsV4DataHelperTests: XCTestCase {
6363
XCTAssertEqual(totalRevenue, "$25")
6464
}
6565

66+
func test_createTotalRevenueDelta_returns_expected_delta_text() {
67+
// Given
68+
let previousOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(grossRevenue: 10))
69+
let currentOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(grossRevenue: 15))
70+
71+
// When
72+
let totalRevenueDelta = StatsV4DataHelper.createTotalRevenueDelta(from: previousOrderStats, to: currentOrderStats, locale: locale)
73+
74+
// Then
75+
XCTAssertEqual(totalRevenueDelta, "+50%")
76+
}
77+
6678
// MARK: Orders Stats
6779

6880
func test_createOrderCountText_returns_expected_order_count() {
@@ -95,6 +107,18 @@ final class StatsV4DataHelperTests: XCTestCase {
95107
XCTAssertEqual(orderCount, "1")
96108
}
97109

110+
func test_createOrderCountDelta_returns_expected_delta_text() {
111+
// Given
112+
let previousOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(totalOrders: 10))
113+
let currentOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(totalOrders: 15))
114+
115+
// When
116+
let orderCountDelta = StatsV4DataHelper.createOrderCountDelta(from: previousOrderStats, to: currentOrderStats, locale: locale)
117+
118+
// Then
119+
XCTAssertEqual(orderCountDelta, "+50%")
120+
}
121+
98122
func test_createAverageOrderValueText_does_not_return_decimal_points_for_integer_value() {
99123
// Given
100124
let orderStats = OrderStatsV4.fake().copy(totals: .fake().copy(averageOrderValue: 62))
@@ -121,6 +145,18 @@ final class StatsV4DataHelperTests: XCTestCase {
121145
XCTAssertEqual(averageOrderValue, "$62.86")
122146
}
123147

148+
func test_createAverageOrderValueDelta_returns_expected_delta_text() {
149+
// Given
150+
let previousOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(averageOrderValue: 10.00))
151+
let currentOrderStats = OrderStatsV4.fake().copy(totals: .fake().copy(averageOrderValue: 15.00))
152+
153+
// When
154+
let averageOrderValueDelta = StatsV4DataHelper.createAverageOrderValueDelta(from: previousOrderStats, to: currentOrderStats, locale: locale)
155+
156+
// Then
157+
XCTAssertEqual(averageOrderValueDelta, "+50%")
158+
}
159+
124160
// MARK: Views and Visitors Stats
125161

126162
// This test reflects the current method for computing total visitor count.
@@ -151,6 +187,18 @@ final class StatsV4DataHelperTests: XCTestCase {
151187
XCTAssertEqual(visitorCount, "17")
152188
}
153189

190+
func test_createVisitorCountDelta_returns_expected_delta_text() {
191+
// Given
192+
let previousSiteStats = SiteVisitStats.fake().copy(items: [.fake().copy(period: "0", visitors: 10)])
193+
let currentSiteStats = SiteVisitStats.fake().copy(items: [.fake().copy(period: "0", visitors: 15)])
194+
195+
// When
196+
let visitorCountDelta = StatsV4DataHelper.createVisitorCountDelta(from: previousSiteStats, to: currentSiteStats, locale: locale)
197+
198+
// Then
199+
XCTAssertEqual(visitorCountDelta, "+50%")
200+
}
201+
154202
// MARK: Conversion Stats
155203

156204
func test_createConversionRateText_returns_placeholder_when_visitor_count_is_zero() {

0 commit comments

Comments
 (0)