Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Merged
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
31 changes: 31 additions & 0 deletions WordPressKit/Insights/StatsPostingStreakInsight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public struct StatsPostingStreakInsight {
public struct PostingStreakEvent {
public let date: Date
public let postCount: Int

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

extension StatsPostingStreakInsight: InsightProtocol {
Expand All @@ -21,6 +26,32 @@ extension StatsPostingStreakInsight: InsightProtocol {
return "stats/streak"
}

// Some heavy-traffic sites can have A LOT of posts and the default query parameters wouldn't
// return all the relevant streak data, so we manualy override the `max` and `startDate``/endDate`
// parameters to hopefully get all.
public static var queryProperties: [String: String] {
let today = Date()

let numberOfDaysInCurrentMonth = Calendar.autoupdatingCurrent.range(of: .day, in: .month, for: today)

guard
let firstDayIndex = numberOfDaysInCurrentMonth?.first,
let lastDayIndex = numberOfDaysInCurrentMonth?.last,
let lastDayOfMonth = Calendar.autoupdatingCurrent.date(bySetting: .day, value: lastDayIndex, of: today),
let firstDayOfMonth = Calendar.autoupdatingCurrent.date(bySetting: .day, value: firstDayIndex, of: today),
let yearAgo = Calendar.autoupdatingCurrent.date(byAdding: .year, value: -1, to: firstDayOfMonth)
else {
return [:]
}

let firstDayString = self.dateFormatter.string(from: yearAgo)
let lastDayString = self.dateFormatter.string(from: lastDayOfMonth)

return ["startDate": "\(firstDayString)",
"endDate": "\(lastDayString)",
"max": "5000"]
}

public init?(jsonDictionary: [String: AnyObject]) {
guard
let postsData = jsonDictionary["data"] as? [String: AnyObject],
Expand Down