Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 8ad283d

Browse files
committed
Add StatsPeriodUnit.hour
1 parent 58092a4 commit 8ad283d

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

Sources/WordPressKit/Models/Stats/StatsSubscribersSummaryData.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ extension StatsSubscribersSummaryData: StatsTimeIntervalData {
1717
return "stats/subscribers"
1818
}
1919

20+
static var hourlyDateFormatter: DateFormatter {
21+
let df = DateFormatter()
22+
df.locale = Locale(identifier: "en_US_POS")
23+
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
24+
return df
25+
}
26+
2027
static var dateFormatter: DateFormatter = {
2128
let df = DateFormatter()
2229
df.locale = Locale(identifier: "en_US_POS")
@@ -71,6 +78,9 @@ extension StatsSubscribersSummaryData: StatsTimeIntervalData {
7178

7279
private static func parsedDate(from dateString: String, for period: StatsPeriodUnit) -> Date? {
7380
switch period {
81+
case .hour:
82+
// Example: "2025-07-17 09:00:00" (in a site timezone)
83+
return self.hourlyDateFormatter.date(from: dateString)
7484
case .week:
7585
return self.weeksDateFormatter.date(from: dateString)
7686
case .day, .month, .year:

Sources/WordPressKit/Models/Stats/Time Interval/StatsSummaryTimeIntervalData.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@frozen public enum StatsPeriodUnit: Int {
2+
case hour
23
case day
34
case week
45
case month
@@ -177,13 +178,23 @@ private extension StatsSummaryData {
177178

178179
static func parsedDate(from dateString: String, for period: StatsPeriodUnit) -> Date? {
179180
switch period {
181+
case .hour:
182+
// Example: "2025-07-17 09:00:00" (in a site timezone)
183+
return self.hourlyDateFormatter.date(from: dateString)
180184
case .week:
181185
return self.weeksDateFormatter.date(from: dateString)
182186
case .day, .month, .year:
183187
return self.regularDateFormatter.date(from: dateString)
184188
}
185189
}
186190

191+
static var hourlyDateFormatter: DateFormatter {
192+
let df = DateFormatter()
193+
df.locale = Locale(identifier: "en_US_POS")
194+
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
195+
return df
196+
}
197+
187198
static var regularDateFormatter: DateFormatter {
188199
let df = DateFormatter()
189200
df.locale = Locale(identifier: "en_US_POS")

Sources/WordPressKit/Services/StatsServiceRemoteV2.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,23 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
9898
/// - endDate: Date on which the `period` for which data you're interested in **is ending**.
9999
/// e.g. if you want data spanning 11-17 Feb 2019, you should pass in a period of `.week` and an
100100
/// ending date of `Feb 17 2019`.
101-
/// - timeZone: The time zone in which the dates are represented.
102101
/// - limit: Limit of how many objects you want returned for your query. Default is `10`. `0` means no limit.
103102
open func getData<TimeStatsType: StatsTimeIntervalData>(
104103
period: StatsPeriodUnit,
105104
unit: StatsPeriodUnit? = nil,
106105
startDate: Date? = nil,
107106
endDate: Date,
108-
timeZone: TimeZone? = nil,
109107
limit: Int = 10,
110108
fields: [String]? = nil,
111109
completion: @escaping ((TimeStatsType?, Error?) -> Void)
112110
) {
113111
let pathComponent = TimeStatsType.pathComponent
114112
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
115113

116-
func formattedDate(_ date: Date) -> String {
117-
guard let timeZone else {
118-
// For backward-compatibility, use the existing periodDataQueryDateFormatter
119-
// with the current time zone.
120-
return periodDataQueryDateFormatter.string(from: date)
121-
}
122-
let formatter = DateFormatter()
123-
formatter.locale = Locale(identifier: "en_US_POSIX")
124-
formatter.dateFormat = "yyyy-MM-dd"
125-
formatter.timeZone = timeZone
126-
return formatter.string(from: date)
127-
}
128-
129114
var properties = [
130115
"period": period.stringValue,
131116
"unit": unit?.stringValue ?? period.stringValue,
132-
"date": formattedDate(endDate)
117+
"date": periodDataQueryDateFormatter.string(from: endDate)
133118
] as [String: Any]
134119

135120
for (key, value) in TimeStatsType.queryProperties(period: unit ?? period, maxCount: limit) {
@@ -138,7 +123,7 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
138123

139124
if let startDate {
140125
properties["period"] = nil
141-
properties["start_date"] = formattedDate(startDate)
126+
properties["start_date"] = periodDataQueryDateFormatter.string(from: startDate)
142127
}
143128
if let fields {
144129
properties["stat_fields"] = fields.joined(separator: ",")
@@ -304,6 +289,9 @@ extension StatsServiceRemoteV2 {
304289

305290
private func startDate(for period: StatsPeriodUnit, endDate: Date) -> Date {
306291
switch period {
292+
case .hour:
293+
assertionFailure("unsupported period: \(period)")
294+
return calendarForSite.startOfDay(for: endDate)
307295
case .day:
308296
return calendarForSite.startOfDay(for: endDate)
309297
case .week:
@@ -425,6 +413,8 @@ extension StatsTimeIntervalData {
425413
public extension StatsPeriodUnit {
426414
var stringValue: String {
427415
switch self {
416+
case .hour:
417+
return "hour"
428418
case .day:
429419
return "day"
430420
case .week:
@@ -438,6 +428,8 @@ public extension StatsPeriodUnit {
438428

439429
init?(string: String) {
440430
switch string {
431+
case "hour":
432+
self = .hour
441433
case "day":
442434
self = .day
443435
case "week":

0 commit comments

Comments
 (0)