Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for fetching subscriber history #795

Merged
merged 15 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _None._
- Add `getPost(withID)` to `PostServiceRemoteExtended` [#785]
- Add support for metadata to `PostServiceRemoteExtended` [#783]
- Add fetching of `StatsEmailsSummaryData` to `StatsService` [#794]
- Add fetching of `StatsSubscribersSummaryData` to `StatsService` [#795]

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Foundation
import WordPressShared

public struct StatsSubscribersSummaryData: Equatable {
public let history: [SubscriberData]
public let period: StatsPeriodUnit
public let periodEndDate: Date

public init(history: [SubscriberData], period: StatsPeriodUnit, periodEndDate: Date) {
self.history = history
self.period = period
self.periodEndDate = periodEndDate
}
}

extension StatsSubscribersSummaryData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/subscribers"
}

static var dateFormatter: DateFormatter = {
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POS")
df.dateFormat = "yyyy-MM-dd"
return df
}()

static var weeksDateFormatter: DateFormatter = {
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POS")
df.dateFormat = "yyyy'W'MM'W'dd"
return df
}()

public struct SubscriberData: Equatable {
public let date: Date
public let count: Int

public init(date: Date, count: Int) {
self.date = date
self.count = count
}
}

public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
guard
let fields = jsonDictionary["fields"] as? [String],
let data = jsonDictionary["data"] as? [[Any]],
let dateIndex = fields.firstIndex(of: "period"),
let countIndex = fields.firstIndex(of: "subscribers")
guarani marked this conversation as resolved.
Show resolved Hide resolved
else {
return nil
}

let history: [SubscriberData?] = data.map { elements in
guard elements.indices.contains(dateIndex) && elements.indices.contains(countIndex),
let dateString = elements[dateIndex] as? String,
let date = StatsSubscribersSummaryData.parsedDate(from: dateString, for: period)
else {
return nil
}

let count = elements[countIndex] as? Int ?? 0

return SubscriberData(date: date, count: count)
}

let sorted = history.compactMap { $0 }.sorted { $0.date < $1.date }

self = .init(history: sorted, period: period, periodEndDate: date)
}

private static func parsedDate(from dateString: String, for period: StatsPeriodUnit) -> Date? {
switch period {
case .week:
return self.weeksDateFormatter.date(from: dateString)
case .day, .month, .year:
return self.dateFormatter.date(from: dateString)
}
}

public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
return ["quantity": String(maxCount), "unit": period.stringValue]
}
}
2 changes: 1 addition & 1 deletion Sources/WordPressKit/Services/StatsServiceRemoteV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ extension StatsTimeIntervalData {

// We'll bring `StatsPeriodUnit` into this file when the "old" `WPStatsServiceRemote` gets removed.
// For now we can piggy-back off the old type and add this as an extension.
extension StatsPeriodUnit {
public extension StatsPeriodUnit {
var stringValue: String {
switch self {
case .day:
Expand Down
Loading