Skip to content

Commit

Permalink
constraining to a much simpler path through logger, experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Dec 15, 2023
1 parent 2737cf8 commit c6d32ec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 87 deletions.
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,6 @@
"version" : "1.1.1"
}
},
{
"identity" : "spisearchresult",
"kind" : "remoteSourceControl",
"location" : "https://github.com/heckj/SPISearchResult.git",
"state" : {
"revision" : "29381418b804b43494ab8689e07f31aa66d66349",
"version" : "0.5.1"
}
},
{
"identity" : "sql-kit",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ let package = Package(
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/jwt-kit", from: "4.13.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/heckj/SPISearchResult.git", from: "0.5.1"),
],
targets: [
.executableTarget(name: "Run", dependencies: ["App"]),
Expand All @@ -71,7 +70,6 @@ let package = Package(
.product(name: "SwiftPMPackageCollections", package: "swift-package-manager"),
.product(name: "Vapor", package: "vapor"),
.product(name: "VaporToOpenAPI", package: "VaporToOpenAPI"),
.product(name: "SPISearchResult", package: "SPISearchResult"),
],
linkerSettings: [.unsafeFlags(["-Xlinker", "-interposable"],
.when(platforms: [.macOS],
Expand Down
1 change: 1 addition & 0 deletions Sources/App/Core/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ enum Search {
.mapEachCompact(Result.init)
.map { results in
SearchLogger.log(query: sanitizedTerms.joined(separator: " "), results: results)

let hasMoreResults = results.filter(\.isPackage).count > pageSize
// first page has non-package results prepended, extend prefix for them
let keep = (page == 1)
Expand Down
121 changes: 45 additions & 76 deletions Sources/App/Core/SearchLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,59 @@
// limitations under the License.

import Foundation
import SPISearchResult

enum SearchLogger {
static var storedSearchesFileURL: URL? {
#if os(macOS)
let local = Foundation.FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("searches", conformingTo: .plainText)
return local
#endif
#if os(Linux)
return URL(fileURLWithPath: "/var/log/searches.txt")
#endif
}
// static var storedSearchesFileURL: URL? {
// #if os(macOS)
// let local = Foundation.FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("searches", conformingTo: .plainText)
// return local
// #endif
// #if os(Linux)
// return URL(fileURLWithPath: "/var/log/searches.txt")
// #endif
// }

static func log(query: String, results: [Search.Result]) {
var authors: [String] = []
var keywords: [String] = []
var packages: [SearchResult.Package] = []

for result in results {
switch result {
case let .author(authorResult):
authors.append(authorResult.name)
case let .keyword(keywordResult):
keywords.append(keywordResult.keyword)
case let .package(pkgResult):
let pkg = SearchResult.Package(
id: .init(owner: pkgResult.repositoryOwner, repository: pkgResult.repositoryName),
name: pkgResult.packageName,
package_keywords: pkgResult.keywords ?? [],
summary: pkgResult.summary,
stars: pkgResult.stars ?? 0,
has_docs: pkgResult.hasDocs,
last_activity: pkgResult.lastActivityAt
)
packages.append(pkg)
}
struct SearchFragment {
let searchID: UUID
let query: String
let result: Search.Result
}
let combinedResult = SearchResult(timestamp: Date.now, query: query, keywords: keywords, authors: authors, packages: packages)

let uniqueSearchID = UUID()

let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .iso8601
jsonEncoder.outputFormatting = .sortedKeys

guard let storedSearchesFileURL else {
return
}
let path = storedSearchesFileURL.path()
if !Foundation.FileManager.default.fileExists(atPath: path) {
// file doesn't exist - create it and write to it
if !Foundation.FileManager.default.createFile(
atPath: path,
contents: Data("# Search Requests\n".utf8), attributes: nil
) {
fatalError("Failed to create file at \(path)")
}
for result in results {
let fragment = SearchFragment(searchID: uniqueSearchID, query: query, result: result)
AppEnvironment.logger.info("searchfragment: \(fragment)")
}

let fileHandle: FileHandle?
do {
fileHandle = try FileHandle(forWritingTo: storedSearchesFileURL)
} catch {
// couldn't open file handle
print("Error: \(error.localizedDescription)")
fileHandle = nil
}
guard let fileHandle else {
print("Unable to access fileHandle for \(storedSearchesFileURL)")
return
}
fileHandle.seekToEndOfFile()
// var authors: [String] = []
// var keywords: [String] = []
// var packages: [SearchResult.Package] = []
//
// for result in results {
// switch result {
// case let .author(authorResult):
// authors.append(authorResult.name)
// case let .keyword(keywordResult):
// keywords.append(keywordResult.keyword)
// case let .package(pkgResult):
// let pkg = SearchResult.Package(
// id: .init(owner: pkgResult.repositoryOwner, repository: pkgResult.repositoryName),
// name: pkgResult.packageName,
// package_keywords: pkgResult.keywords ?? [],
// summary: pkgResult.summary,
// stars: pkgResult.stars ?? 0,
// has_docs: pkgResult.hasDocs,
// last_activity: pkgResult.lastActivityAt
// )
// packages.append(pkg)
// }
// }
// let combinedResult = SearchResult(timestamp: Date.now, query: query, keywords: keywords, authors: authors, packages: packages)

let encodedSearchResult: Data?
do {
encodedSearchResult = try jsonEncoder.encode(combinedResult)
} catch {
print("Failed to encode search result: \(error.localizedDescription)")
encodedSearchResult = nil
}
if let encodedSearchResult,
let newlineAsData = "\n".data(using: .utf8)
{
fileHandle.write(encodedSearchResult)
fileHandle.write(newlineAsData)
}
fileHandle.closeFile()
// let jsonEncoder = JSONEncoder()
// jsonEncoder.dateEncodingStrategy = .iso8601
// jsonEncoder.outputFormatting = .sortedKeys
}
}

0 comments on commit c6d32ec

Please sign in to comment.