|
| 1 | +public struct StatsFileDownloadsTimeIntervalData { |
| 2 | + public let period: StatsPeriodUnit |
| 3 | + public let periodEndDate: Date |
| 4 | + |
| 5 | + public let totalDownloadsCount: Int |
| 6 | + public let otherDownloadsCount: Int |
| 7 | + public let fileDownloads: [StatsFileDownload] |
| 8 | + |
| 9 | + public init(period: StatsPeriodUnit, |
| 10 | + periodEndDate: Date, |
| 11 | + fileDownloads: [StatsFileDownload], |
| 12 | + totalDownloadsCount: Int, |
| 13 | + otherDownloadsCount: Int) { |
| 14 | + self.period = period |
| 15 | + self.periodEndDate = periodEndDate |
| 16 | + self.fileDownloads = fileDownloads |
| 17 | + self.totalDownloadsCount = totalDownloadsCount |
| 18 | + self.otherDownloadsCount = otherDownloadsCount |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +public struct StatsFileDownload { |
| 23 | + public let file: String |
| 24 | + public let downloadCount: Int |
| 25 | + |
| 26 | + public init(file: String, |
| 27 | + downloadCount: Int) { |
| 28 | + self.file = file |
| 29 | + self.downloadCount = downloadCount |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension StatsFileDownloadsTimeIntervalData: StatsTimeIntervalData { |
| 34 | + public static var pathComponent: String { |
| 35 | + return "stats/file-downloads" |
| 36 | + } |
| 37 | + |
| 38 | + public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] { |
| 39 | + // num = number of periods to include in the query. default: 1. |
| 40 | + return ["num": String(maxCount)] |
| 41 | + } |
| 42 | + |
| 43 | + public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String : AnyObject]) { |
| 44 | + guard |
| 45 | + let unwrappedDays = type(of: self).unwrapDaysDictionary(jsonDictionary: jsonDictionary), |
| 46 | + let fileDownloadsDict = unwrappedDays["files"] as? [[String: AnyObject]] |
| 47 | + else { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + let fileDownloads: [StatsFileDownload] = fileDownloadsDict.compactMap { |
| 52 | + // TODO: these keys are a total guess. Verify/update when the endpoint is actually live. |
| 53 | + guard let file = $0["file"] as? String, let downloads = $0["downloads"] as? Int else { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + return StatsFileDownload(file: file, downloadCount: downloads) |
| 58 | + } |
| 59 | + |
| 60 | + self.periodEndDate = date |
| 61 | + self.period = period |
| 62 | + self.fileDownloads = fileDownloads |
| 63 | + self.totalDownloadsCount = unwrappedDays["total_downloads"] as? Int ?? 0 |
| 64 | + self.otherDownloadsCount = unwrappedDays["other_downloads"] as? Int ?? 0 |
| 65 | + } |
| 66 | +} |
0 commit comments