@@ -95,26 +95,53 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
9595 /// - parameters:
9696 /// - period: An enum representing whether either a day, a week, a month or a year worth's of data.
9797 /// - unit: An enum representing whether the data is retuned in a day, a week, a month or a year granularity. Default is `period`.
98- /// - endingOn : Date on which the `period` for which data you're interested in **is ending**.
98+ /// - 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.
101102 /// - limit: Limit of how many objects you want returned for your query. Default is `10`. `0` means no limit.
102- open func getData< TimeStatsType: StatsTimeIntervalData > ( for period: StatsPeriodUnit ,
103- unit: StatsPeriodUnit ? = nil ,
104- endingOn: Date ,
105- limit: Int = 10 ,
106- completion: @escaping ( ( TimeStatsType ? , Error ? ) -> Void ) ) {
103+ open func getData< TimeStatsType: StatsTimeIntervalData > (
104+ period: StatsPeriodUnit ,
105+ unit: StatsPeriodUnit ? = nil ,
106+ startDate: Date ? = nil ,
107+ endDate: Date ,
108+ timeZone: TimeZone ? = nil ,
109+ limit: Int = 10 ,
110+ fields: [ String ] ? = nil ,
111+ completion: @escaping ( ( TimeStatsType ? , Error ? ) -> Void )
112+ ) {
107113 let pathComponent = TimeStatsType . pathComponent
108114 let path = self . path ( forEndpoint: " sites/ \( siteID) / \( pathComponent) / " , withVersion: . _1_1)
109115
110- let staticProperties = [ " period " : period. stringValue,
111- " unit " : unit? . stringValue ?? period. stringValue,
112- " date " : periodDataQueryDateFormatter. string ( from: endingOn) ] as [ String : AnyObject ]
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+ }
113128
114- let classProperties = TimeStatsType . queryProperties ( with: endingOn, period: unit ?? period, maxCount: limit) as [ String : AnyObject ]
129+ var properties = [
130+ " period " : period. stringValue,
131+ " unit " : unit? . stringValue ?? period. stringValue,
132+ " date " : formattedDate ( endDate)
133+ ] as [ String : Any ]
115134
116- let properties = staticProperties. merging ( classProperties) { val1, _ in
117- return val1
135+ for (key, value) in TimeStatsType . queryProperties ( period: unit ?? period, maxCount: limit) {
136+ properties [ key] = value
137+ }
138+
139+ if let startDate {
140+ properties [ " period " ] = nil
141+ properties [ " start_date " ] = formattedDate ( startDate)
142+ }
143+ if let fields {
144+ properties [ " stat_fields " ] = fields. joined ( separator: " , " )
118145 }
119146
120147 wordPressComRESTAPI. get ( path, parameters: properties, success: { [ weak self] ( response, _) in
@@ -361,7 +388,7 @@ public protocol StatsTimeIntervalData {
361388 init ? ( date: Date , period: StatsPeriodUnit , jsonDictionary: [ String : AnyObject ] )
362389 init ? ( date: Date , period: StatsPeriodUnit , unit: StatsPeriodUnit ? , jsonDictionary: [ String : AnyObject ] )
363390
364- static func queryProperties( with date : Date , period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ]
391+ static func queryProperties( period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ]
365392}
366393
367394extension StatsTimeIntervalData {
@@ -370,7 +397,7 @@ extension StatsTimeIntervalData {
370397 return nil
371398 }
372399
373- public static func queryProperties( with date : Date , period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
400+ public static func queryProperties( period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
374401 return [ " max " : String ( maxCount) ]
375402 }
376403
0 commit comments