Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 9a7a368

Browse files
committed
Adding StatsFileDownloadsTimeIntervalData to contain File Download data.
1 parent 02bcec0 commit 9a7a368

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)