@@ -35,26 +35,23 @@ public struct StatsSummaryData {
3535 public let period : StatsPeriodUnit
3636 public let periodStartDate : Date
3737
38- public let viewsCount : Int ?
39- public let visitorsCount : Int ?
40- public let likesCount : Int ?
41- public let commentsCount : Int ?
42- public let postsCount : Int ?
38+ public let viewsCount : Int
39+ public let visitorsCount : Int
40+ public let likesCount : Int
41+ public let commentsCount : Int
4342
4443 public init ( period: StatsPeriodUnit ,
4544 periodStartDate: Date ,
46- viewsCount: Int ? ,
47- visitorsCount: Int ? ,
48- likesCount: Int ? ,
49- commentsCount: Int ? ,
50- postsCount: Int ? ) {
45+ viewsCount: Int ,
46+ visitorsCount: Int ,
47+ likesCount: Int ,
48+ commentsCount: Int ) {
5149 self . period = period
5250 self . periodStartDate = periodStartDate
5351 self . viewsCount = viewsCount
5452 self . visitorsCount = visitorsCount
5553 self . likesCount = likesCount
5654 self . commentsCount = commentsCount
57- self . postsCount = postsCount
5855 }
5956}
6057
@@ -63,7 +60,7 @@ extension StatsSummaryTimeIntervalData: StatsTimeIntervalData {
6360 return " stats/visits "
6461 }
6562
66- public static func queryProperties( period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
63+ public static func queryProperties( with date : Date , period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
6764 return [ " unit " : period. stringValue,
6865 " quantity " : String ( maxCount) ,
6966 " stat_fields " : " views,visitors,comments,likes " ]
@@ -91,79 +88,106 @@ extension StatsSummaryTimeIntervalData: StatsTimeIntervalData {
9188 // [["2019-01-01", 9001, 1234], ["2019-02-01", 1234, 1234]], where the first object in the "inner" array
9289 // is the `period`, second is `views`, etc.
9390
94- guard let periodIndex = fieldsArray. firstIndex ( of: " period " ) else {
95- return nil
91+ guard
92+ let periodIndex = fieldsArray. firstIndex ( of: " period " ) ,
93+ let viewsIndex = fieldsArray. firstIndex ( of: " views " ) ,
94+ let visitorsIndex = fieldsArray. firstIndex ( of: " visitors " ) ,
95+ let commentsIndex = fieldsArray. firstIndex ( of: " comments " ) ,
96+ let likesIndex = fieldsArray. firstIndex ( of: " likes " )
97+ else {
98+ return nil
9699 }
97100
98101 self . period = period
99102 self . unit = unit
100103 self . periodEndDate = date
101- self . summaryData = data. compactMap {
102- StatsSummaryData (
103- dataArray: $0,
104- period: unit ?? period,
105- periodIndex: periodIndex,
106- viewsIndex: fieldsArray. firstIndex ( of: " views " ) ,
107- visitorsIndex: fieldsArray. firstIndex ( of: " visitors " ) ,
108- likesIndex: fieldsArray. firstIndex ( of: " likes " ) ,
109- commentsIndex: fieldsArray. firstIndex ( of: " comments " ) ,
110- postsIndex: fieldsArray. firstIndex ( of: " posts " )
111- )
112- }
104+ self . summaryData = data. compactMap { StatsSummaryData ( dataArray: $0,
105+ period: unit ?? period,
106+ periodIndex: periodIndex,
107+ viewsIndex: viewsIndex,
108+ visitorsIndex: visitorsIndex,
109+ likesIndex: likesIndex,
110+ commentsIndex: commentsIndex) }
113111 }
114112}
115113
116114private extension StatsSummaryData {
117- init ? (
118- dataArray: [ Any ] ,
119- period: StatsPeriodUnit ,
120- periodIndex: Int ,
121- viewsIndex: Int ? ,
122- visitorsIndex: Int ? ,
123- likesIndex: Int ? ,
124- commentsIndex: Int ? ,
125- postsIndex: Int ?
126- ) {
115+ init ? ( dataArray: [ Any ] ,
116+ period: StatsPeriodUnit ,
117+ periodIndex: Int ,
118+ viewsIndex: Int ? ,
119+ visitorsIndex: Int ? ,
120+ likesIndex: Int ? ,
121+ commentsIndex: Int ? ) {
122+
127123 guard
128124 let periodString = dataArray [ periodIndex] as? String ,
129125 let periodStart = type ( of: self ) . parsedDate ( from: periodString, for: period) else {
130126 return nil
131127 }
132128
133- func getValue( at index: Int ? ) -> Int ? {
134- guard let index else { return nil }
135- return dataArray [ index] as? Int
129+ let viewsCount : Int
130+ let visitorsCount : Int
131+ let likesCount : Int
132+ let commentsCount : Int
133+
134+ if let viewsIndex = viewsIndex {
135+ guard let count = dataArray [ viewsIndex] as? Int else {
136+ return nil
137+ }
138+ viewsCount = count
139+ } else {
140+ viewsCount = 0
141+ }
142+
143+ if let visitorsIndex = visitorsIndex {
144+ guard let count = dataArray [ visitorsIndex] as? Int else {
145+ return nil
146+ }
147+ visitorsCount = count
148+ } else {
149+ visitorsCount = 0
150+ }
151+
152+ if let likesIndex = likesIndex {
153+ guard let count = dataArray [ likesIndex] as? Int else {
154+ return nil
155+ }
156+ likesCount = count
157+ } else {
158+ likesCount = 0
159+ }
160+
161+ if let commentsIndex = commentsIndex {
162+ guard let count = dataArray [ commentsIndex] as? Int else {
163+ return nil
164+ }
165+ commentsCount = count
166+ } else {
167+ commentsCount = 0
136168 }
137169
138170 self . period = period
139171 self . periodStartDate = periodStart
140172
141- self . viewsCount = getValue ( at: viewsIndex)
142- self . visitorsCount = getValue ( at: visitorsIndex)
143- self . likesCount = getValue ( at: likesIndex)
144- self . commentsCount = getValue ( at: commentsIndex)
145- self . postsCount = getValue ( at: postsIndex)
173+ self . viewsCount = viewsCount
174+ self . visitorsCount = visitorsCount
175+ self . likesCount = likesCount
176+ self . commentsCount = commentsCount
146177 }
147178
148179 static func parsedDate( from dateString: String , for period: StatsPeriodUnit ) -> Date ? {
149180 switch period {
150181 case . hour:
151- // Example: "2025-07-17 09:00:00" (in a site timezone )
152- return self . hourlyDateFormatter . date ( from : dateString )
182+ assertionFailure ( " Unsupported time period " )
183+ return nil
153184 case . week:
154185 return self . weeksDateFormatter. date ( from: dateString)
155186 case . day, . month, . year:
156187 return self . regularDateFormatter. date ( from: dateString)
157188 }
158189 }
159190
160- static var hourlyDateFormatter : DateFormatter {
161- let df = DateFormatter ( )
162- df. locale = Locale ( identifier: " en_US_POSIX " )
163- df. dateFormat = " yyyy-MM-dd HH:mm:ss "
164- return df
165- }
166-
167191 static var regularDateFormatter : DateFormatter {
168192 let df = DateFormatter ( )
169193 df. locale = Locale ( identifier: " en_US_POS " )
@@ -214,7 +238,7 @@ extension StatsLikesSummaryTimeIntervalData: StatsTimeIntervalData {
214238 return " stats/visits "
215239 }
216240
217- public static func queryProperties( period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
241+ public static func queryProperties( with date : Date , period: StatsPeriodUnit , maxCount: Int ) -> [ String : String ] {
218242 return [ " unit " : period. stringValue,
219243 " quantity " : String ( maxCount) ,
220244 " stat_fields " : " likes " ]
@@ -240,16 +264,12 @@ extension StatsLikesSummaryTimeIntervalData: StatsTimeIntervalData {
240264
241265 self . period = period
242266 self . periodEndDate = date
243- self . summaryData = data. compactMap {
244- StatsSummaryData (
245- dataArray: $0,
246- period: unit ?? period,
247- periodIndex: periodIndex,
248- viewsIndex: nil ,
249- visitorsIndex: nil ,
250- likesIndex: likesIndex,
251- commentsIndex: nil , postsIndex: nil
252- )
253- }
267+ self . summaryData = data. compactMap { StatsSummaryData ( dataArray: $0,
268+ period: unit ?? period,
269+ periodIndex: periodIndex,
270+ viewsIndex: nil ,
271+ visitorsIndex: nil ,
272+ likesIndex: likesIndex,
273+ commentsIndex: nil ) }
254274 }
255275}
0 commit comments