-
Notifications
You must be signed in to change notification settings - Fork 16
Add endpoint for fetching subscriber history #795
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7cb9042
Add subscriber chart API endpoint
guarani 50562f8
Add subscriber chart API endpoint
guarani 4dd2361
Fix subscriber history query
guarani 5ca2cff
Merge branch 'trunk' into task/subscribers-api-endpoint
guarani 0591961
Reverted unnecessary change
guarani 213fa55
Merge branch 'task/subscribers-api-endpoint' of github.com:wordpress-…
guarani 72dc8b5
Fixed subscriber data parsing
guarani a7ccb2e
Reverted changes to Package.resolved
guarani 5a7d637
added missing files, fixed tests
guarani 5d1359c
fix linter issue
guarani 3197069
fix another linter issue
guarani 7d6e0e7
Conform to StatsTimeIntervalData
staskus 1b31242
Made StatsPeriodUnit stringValue public
guarani 8e020f8
Merge branch 'trunk' into task/subscribers-api-endpoint
guarani 7f47dbc
Fix subscriber fetch new sites
guarani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Sources/WordPressKit/Models/Stats/StatsSubscribersSummaryData.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
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] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.