Skip to content

Commit 182f952

Browse files
Merge pull request #2786 from heckj/savesearches
Save the searches
2 parents d424351 + 45d024b commit 182f952

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

Sources/App/Core/Search+Result.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Vapor
1616

1717

1818
extension Search {
19-
enum Result: Codable, Equatable {
19+
enum Result: Codable, Hashable, Equatable {
2020
case author(AuthorResult)
2121
case keyword(KeywordResult)
2222
case package(PackageResult)
@@ -84,15 +84,15 @@ extension Search {
8484
}
8585
}
8686

87-
struct AuthorResult: Codable, Equatable {
87+
struct AuthorResult: Codable, Hashable, Equatable {
8888
var name: String
8989
}
9090

91-
struct KeywordResult: Codable, Equatable {
91+
struct KeywordResult: Codable, Hashable, Equatable {
9292
var keyword: String
9393
}
9494

95-
struct PackageResult: Codable, Equatable {
95+
struct PackageResult: Codable, Hashable, Equatable {
9696
var packageId: Package.Id
9797
var packageName: String?
9898
var packageURL: String

Sources/App/Core/Search.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ enum Search {
390390
return query.all(decoding: DBRecord.self)
391391
.mapEachCompact(Result.init)
392392
.map { results in
393+
SearchLogger.log(query: sanitizedTerms.joined(separator: " "), results: Array(results.prefix(10)))
394+
393395
let hasMoreResults = results.filter(\.isPackage).count > pageSize
394396
// first page has non-package results prepended, extend prefix for them
395397
let keep = (page == 1)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
struct SearchQuery: Hashable, Codable {
18+
let searchID: UUID
19+
let query: String
20+
}
21+
22+
struct SearchResultFragment: Hashable, Codable {
23+
let searchID: UUID
24+
let result: Search.Result?
25+
26+
enum CodingKeys: String, CodingKey {
27+
case searchID = "id"
28+
case result = "r"
29+
}
30+
}
31+
32+
enum SearchLogger {
33+
static func log(query: String, results: [Search.Result]) {
34+
guard Current.environment() != .production else { return }
35+
let uniqueSearchID = UUID()
36+
let jsonEncoder = JSONEncoder()
37+
jsonEncoder.dateEncodingStrategy = .iso8601
38+
jsonEncoder.outputFormatting = .sortedKeys
39+
40+
let baseQuery = SearchQuery(searchID: uniqueSearchID, query: query)
41+
do {
42+
let stringdata = String(decoding: try jsonEncoder.encode(baseQuery), as: UTF8.self)
43+
AppEnvironment.logger.info("search: \(stringdata)")
44+
} catch {
45+
AppEnvironment.logger.warning("unable to encode search query: \(error)")
46+
}
47+
48+
for (idx, result) in results.enumerated() {
49+
let fragment = SearchResultFragment(searchID: uniqueSearchID, result: result)
50+
do {
51+
let stringdata = String(decoding: try jsonEncoder.encode(fragment), as: UTF8.self)
52+
AppEnvironment.logger.info("searchresult[\(idx)]: \(stringdata)")
53+
} catch {
54+
AppEnvironment.logger.warning("unable to encode search fragment: \(error)")
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)