Skip to content

Commit

Permalink
Creating a struct to hold the compilation result.
Browse files Browse the repository at this point in the history
  • Loading branch information
studiosutara committed Dec 4, 2024
1 parent 5a83635 commit 8c2d168
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
Logger.contentBlocking.debug("Failed to complete task \(task.rulesList.name, privacy: .public)")
return nil
}

Check failure on line 376 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
let rules = Rules(compilationResult: result)

let diff: ContentBlockerRulesIdentifier.Difference
if let id = _currentRules.first(where: {$0.name == task.rulesList.name })?.identifier {
if let id = _currentRules.first(where: { $0.name == task.rulesList.name })?.identifier {
diff = id.compare(with: result.model.rulesIdentifier)
} else {
diff = result.model.rulesIdentifier.compare(with: ContentBlockerRulesIdentifier(name: task.rulesList.name,
Expand All @@ -386,14 +387,12 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
unprotectedSitesHash: nil))
}

if let compilationTime = result.compilationTime {

// todo: map broken sources to iteration count
let iteration = task.sourceManager.brokenSources

// todo: need to change this to the updated format with time range and iteration
self.errorReporting?.fire(.contentBlockingCompilationTime, parameters: ["compilationTime": String(compilationTime)])
}

Check failure on line 390 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 390 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
// if let compilationTime = result.compilationTime {
//
// // todo: need to change this to the updated format with time range and iteration
// self.errorReporting?.fire(.contentBlockingCompilationTime, parameters: ["compilationTime": String(compilationTime)])
// }

changes[task.rulesList.name] = diff
return rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,25 @@ import TrackerRadarKit
import os.log

extension ContentBlockerRulesManager {
typealias CompilationResult = (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel, compilationTime: TimeInterval?)


Check failure on line 26 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
internal struct CompilationResult {
let compiledRulesList: WKContentRuleList
let model: ContentBlockerRulesSourceModel
let resultType: ResultType
let performanceInfo: PerformanceInfo?

Check failure on line 31 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 32 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
struct PerformanceInfo {
let compilationTime: TimeInterval?
let iterationCount: Int?
}

Check failure on line 37 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
enum ResultType {
case cacheLookup
case rulesCompilation
}
}

Check failure on line 43 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 44 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
/**
Encapsulates compilation steps for a single Task
*/
Expand Down Expand Up @@ -74,7 +91,10 @@ extension ContentBlockerRulesManager {
WKContentRuleListStore.default()?.lookUpContentRuleList(forIdentifier: identifier) { ruleList, _ in
if let ruleList = ruleList {
Logger.contentBlocking.log("🟢 CBR loaded from cache: \(self.rulesList.name, privacy: .public)")
self.compilationSucceeded(with: ruleList, model: model, completionHandler: completionHandler)
self.compilationSucceeded(with: ruleList,
model: model,
resultType: .cacheLookup,
completionHandler: completionHandler)
} else {
self.workQueue.async {
self.compile(model: model, completionHandler: completionHandler)
Expand All @@ -87,11 +107,14 @@ extension ContentBlockerRulesManager {

private func compilationSucceeded(with compiledRulesList: WKContentRuleList,
model: ContentBlockerRulesSourceModel,
resultType: CompilationResult.ResultType,
completionHandler: @escaping Completion) {

Check failure on line 112 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
self.result = self.getCompilationResult(ruleList: compiledRulesList,
model: model,
resultType: resultType)

workQueue.async {
let compilationTime = self.compilationStartTime.map { start in CACurrentMediaTime() - start }

self.result = (compiledRulesList, model, compilationTime)
completionHandler(self, true)
}
}
Expand Down Expand Up @@ -139,7 +162,10 @@ extension ContentBlockerRulesManager {

if let ruleList = ruleList {
Logger.contentBlocking.log("🟢 CBR compilation for \(self.rulesList.name, privacy: .public) succeeded")
self.compilationSucceeded(with: ruleList, model: model, completionHandler: completionHandler)
self.compilationSucceeded(with: ruleList,
model: model,
resultType: .rulesCompilation,
completionHandler: completionHandler)
} else if let error = error {
self.compilationFailed(for: model, with: error, completionHandler: completionHandler)
} else {
Expand All @@ -148,6 +174,35 @@ extension ContentBlockerRulesManager {
}
}
}

func getCompilationResult(ruleList: WKContentRuleList,
model: ContentBlockerRulesSourceModel,
resultType: CompilationResult.ResultType) -> CompilationResult {
let compilationTime = self.compilationStartTime.map { start in CACurrentMediaTime() - start }
let perfInfo = CompilationResult.PerformanceInfo(compilationTime: compilationTime, iterationCount: getCompilationIterationCount())

return CompilationResult(compiledRulesList: ruleList,
model: model,
resultType: resultType,
performanceInfo: perfInfo)

}

func getCompilationIterationCount() -> Int {
guard let brokenSources = sourceManager.brokenSources else {
return 0
}

let identifiers = [
brokenSources.allowListIdentifier,
brokenSources.tempListIdentifier,
brokenSources.unprotectedSitesIdentifier,
brokenSources.tdsIdentifier
]

return identifiers.compactMap { $0 }.count
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ extension ContentBlockerRulesManager {

private let sourceManagers: [ContentBlockerRulesSourceManager]

// todo: how to get around naming this compilation result when in lookuptask?
public private(set) var result: [CompilationResult]?

init(sourceManagers: [ContentBlockerRulesSourceManager]) {
Expand All @@ -51,7 +50,10 @@ extension ContentBlockerRulesManager {
throw WKError(.contentRuleListStoreLookUpFailed)
}

result.append((ruleList, model, nil))
result.append(CompilationResult(compiledRulesList: ruleList,
model: model,
resultType: .cacheLookup,
performanceInfo: nil))
}
self.result = result
}
Expand Down

0 comments on commit 8c2d168

Please sign in to comment.