Skip to content

Commit cf2f7b4

Browse files
XiaowenVCspinach
authored andcommitted
Aggregate facets_stats (#522)
* Aggregate facets_stats * Change loop index. facets_stats starts from 0, disjunctiveFacets starts from 1. * Remove duplicated code * Fix bug: when i == 0, result["facets"] is nil, so only do this when i >= 1 * After rebase: put back unit test for facet_stats * Fix memory leak * Reverse 965204c * Fix memory leak (this time it works) * Fix UT
1 parent 6dc2c8c commit cf2f7b4

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

Sources/AlgoliaSearch-Client/AbstractClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ internal struct HostStatus {
270270
// WARNING: `didSet` not called during initialization => we need to update the headers manually here.
271271
updateHeadersFromAPIKey()
272272
}
273+
274+
deinit {
275+
session.finishTasksAndInvalidate()
276+
}
273277

274278
/// Set read and write hosts to the same value (convenience method).
275279
///

Sources/AlgoliaSearch-Client/Helpers/DisjunctiveFaceting.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,22 @@ internal class DisjunctiveFaceting {
9696
}
9797
// Following answers are just used for their facet counts.
9898
var disjunctiveFacetCounts = [String: Any]()
99-
for i in 1..<results.count { // for each answer (= each disjunctive facet)
100-
guard let result = results[i] as? [String: Any], let allFacetCounts = result["facets"] as? [String: [String: Any]] else {
99+
var facetsStats = [String: Any]()
100+
for i in 0..<results.count { // for each answer (= each disjunctive facet)
101+
guard let result = results[i] as? [String: Any] else {
102+
throw InvalidJSONError(description: "Invalid results in response")
103+
}
104+
// Facets stats, starts from element 0
105+
if let allStats = result["facets_stats"] as? [String: [String: Any]] {
106+
for (facetName, stats) in allStats {
107+
facetsStats[facetName] = stats
108+
}
109+
}
110+
// Disjunctive facet should start from element 1
111+
if i <= 0 {
112+
continue
113+
}
114+
guard let allFacetCounts = result["facets"] as? [String: [String: Any]] else {
101115
throw InvalidJSONError(description: "Invalid results in response")
102116
}
103117
// NOTE: Iterating, but there should be just one item.
@@ -125,6 +139,7 @@ internal class DisjunctiveFaceting {
125139
}
126140
}
127141
mainContent["disjunctiveFacets"] = disjunctiveFacetCounts
142+
mainContent["facets_stats"] = facetsStats
128143
return mainContent
129144
}
130145

Sources/AlgoliaSearch-Client/Network.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal enum HTTPMethod: String {
3535
/// Only for the sake of unit tests.
3636
internal protocol URLSession {
3737
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
38+
func finishTasksAndInvalidate()
3839
}
3940

4041
// Convince the compiler that NSURLSession does implements our custom protocol.
@@ -131,6 +132,10 @@ internal class URLSessionLogger: NSObject, URLSession {
131132
return task
132133
}
133134

135+
func finishTasksAndInvalidate() {
136+
session.finishTasksAndInvalidate()
137+
}
138+
134139
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
135140
if let task = object as? URLSessionTask {
136141
if keyPath == "state" {

Tests/Helpers/MockURLSession.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public struct MockResponse {
7474
/// A replacement for `NSURLSession` used for mocking network requests.
7575
///
7676
public class MockURLSession: AlgoliaSearch.URLSession {
77+
7778
/// Predefined set of responses for the specified URLs.
7879
public var responses: [String: MockResponse] = [String: MockResponse]()
7980

@@ -88,6 +89,10 @@ public class MockURLSession: AlgoliaSearch.URLSession {
8889
task.cancellable = self.cancellable
8990
return task
9091
}
92+
93+
public func finishTasksAndInvalidate() {
94+
// do nothing
95+
}
9196
}
9297

9398
/// A mock replacement for `NSURLSessionDataTask`.

Tests/Integration Tests/IndexTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,10 @@ class IndexTests: OnlineTestCase {
868868
XCTAssertEqual(brandFacetCounts!["Apple"] as? Int, 2)
869869
XCTAssertEqual(brandFacetCounts!["Samsung"] as? Int, 1)
870870
XCTAssertEqual(brandFacetCounts!["Whatever"] as? Int, 1)
871+
let facetsStats = content["facets_stats"] as? [String: [String: Any]]
872+
let starStats = facetsStats!["stars"]
873+
XCTAssertEqual(starStats!["max"] as? Int, 5)
874+
XCTAssertEqual(starStats!["min"] as? Int, 4)
871875
}
872876

873877
self.waitForExpectations(timeout: expectationTimeout, handler: nil)

0 commit comments

Comments
 (0)