Skip to content

Commit

Permalink
Display OptOutAttemptDB in DB Browser (#3555)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/72649045549333/1208768738130147/f
Tech Design URL:
CC:

**Description**:

Shows OptOutAttemptDB in DB Browser

**Steps to test this PR**:
1. Check DB browser
2. You should be able to view OptOutAttemptDB contents

<!--
Tagging instructions
If this PR isn't ready to be merged for whatever reason it should be
marked with the `DO NOT MERGE` label (particularly if it's a draft)
If it's pending Product Review/PFR, please add the `Pending Product
Review` label.

If at any point it isn't actively being worked on/ready for
review/otherwise moving forward (besides the above PR/PFR exception)
strongly consider closing it (or not opening it in the first place). If
you decide not to close it, make sure it's labelled to make it clear the
PRs state and comment with more information.
-->

**Definition of Done**:

* [ ] Does this PR satisfy our [Definition of
Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)?

---
###### Internal references:
[Pull Request Review
Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f)
[Software Engineering
Expectations](https://app.asana.com/0/59792373528535/199064865822552)
[Technical Design
Template](https://app.asana.com/0/59792373528535/184709971311943)
[Pull Request
Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
  • Loading branch information
quanganhdo authored Nov 18, 2024
1 parent f7a2402 commit 1dddb47
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public protocol DataBrokerProtectionRepository {
func fetchOptOutHistoryEvents(brokerId: Int64, profileQueryId: Int64, extractedProfileId: Int64) throws -> [HistoryEvent]
func hasMatches() throws -> Bool

func fetchAllAttempts() throws -> [AttemptInformation]
func fetchAttemptInformation(for extractedProfileId: Int64) throws -> AttemptInformation?
func addAttempt(extractedProfileId: Int64, attemptUUID: UUID, dataBroker: String, lastStageDate: Date, startTime: Date) throws
}
Expand Down Expand Up @@ -487,6 +488,17 @@ final class DataBrokerProtectionDatabase: DataBrokerProtectionRepository {
}
}

func fetchAllAttempts() throws -> [AttemptInformation] {
do {
let vault = try self.vault ?? DataBrokerProtectionSecureVaultFactory.makeVault(reporter: secureVaultErrorReporter)
return try vault.fetchAllAttempts()
} catch {
Logger.dataBrokerProtection.error("Database error: fetchAllAttempts, error: \(error.localizedDescription, privacy: .public)")
pixelHandler.fire(.generalError(error: error, functionOccurredIn: "DataBrokerProtectionDatabase.fetchAllAttempts"))
throw error
}
}

func fetchAttemptInformation(for extractedProfileId: Int64) throws -> AttemptInformation? {
do {
let vault = try self.vault ?? DataBrokerProtectionSecureVaultFactory.makeVault(reporter: secureVaultErrorReporter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ final class DataBrokerDatabaseBrowserViewModel: ObservableObject {
guard let dataManager = self.dataManager else { return }

Task {
guard let data = try? dataManager.fetchBrokerProfileQueryData(ignoresCache: true) else {
guard let data = try? dataManager.fetchBrokerProfileQueryData(ignoresCache: true),
let attempts = try? dataManager.fetchAllOptOutAttempts() else {
assertionFailure("DataManager error during DataBrokerDatavaseBrowserViewModel.updateTables")
return
}
Expand All @@ -68,9 +69,10 @@ final class DataBrokerDatabaseBrowserViewModel: ObservableObject {
let optOutsTable = createTable(using: optOutJobs, tableName: "OptOutOperation")
let extractedProfilesTable = createTable(using: extractedProfiles, tableName: "ExtractedProfile")
let eventsTable = createTable(using: events.sorted(by: { $0.date < $1.date }), tableName: "Events")
let attemptsTable = createTable(using: attempts.sorted(by: <), tableName: "OptOutAttempts")

DispatchQueue.main.async {
self.tables = [brokersTable, profileQueriesTable, scansTable, optOutsTable, extractedProfilesTable, eventsTable]
self.tables = [brokersTable, profileQueriesTable, scansTable, optOutsTable, extractedProfilesTable, eventsTable, attemptsTable]
}
}
}
Expand Down Expand Up @@ -136,3 +138,25 @@ struct DataBrokerDatabaseBrowserData {
}

}

extension DataBrokerProtectionDataManager {
func fetchAllOptOutAttempts() throws -> [AttemptInformation] {
try database.fetchAllAttempts()
}
}

extension AttemptInformation: Comparable {
public static func < (lhs: AttemptInformation, rhs: AttemptInformation) -> Bool {
if lhs.extractedProfileId != rhs.extractedProfileId {
return lhs.extractedProfileId < rhs.extractedProfileId
} else if lhs.dataBroker != rhs.dataBroker {
return lhs.dataBroker < rhs.dataBroker
} else {
return lhs.startDate < rhs.startDate
}
}

public static func == (lhs: AttemptInformation, rhs: AttemptInformation) -> Bool {
lhs.attemptId == rhs.attemptId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protocol DataBrokerProtectionDatabaseProvider: SecureStorageDatabaseProvider {

func hasMatches() throws -> Bool

func fetchAllAttempts() throws -> [OptOutAttemptDB]
func fetchAttemptInformation(for extractedProfileId: Int64) throws -> OptOutAttemptDB?
func save(_ optOutAttemptDB: OptOutAttemptDB) throws
}
Expand Down Expand Up @@ -619,6 +620,12 @@ final class DefaultDataBrokerProtectionDatabaseProvider: GRDBSecureStorageDataba
}
}

func fetchAllAttempts() throws -> [OptOutAttemptDB] {
try db.read { db in
return try OptOutAttemptDB.fetchAll(db)
}
}

func fetchAttemptInformation(for extractedProfileId: Int64) throws -> OptOutAttemptDB? {
try db.read { db in
return try OptOutAttemptDB.fetchOne(db, key: extractedProfileId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ protocol DataBrokerProtectionSecureVault: SecureVault {

func hasMatches() throws -> Bool

func fetchAllAttempts() throws -> [AttemptInformation]
func fetchAttemptInformation(for extractedProfileId: Int64) throws -> AttemptInformation?
func save(extractedProfileId: Int64, attemptUUID: UUID, dataBroker: String, lastStageDate: Date, startTime: Date) throws
}
Expand Down Expand Up @@ -445,6 +446,11 @@ final class DefaultDataBrokerProtectionSecureVault<T: DataBrokerProtectionDataba
try self.providers.database.hasMatches()
}

func fetchAllAttempts() throws -> [AttemptInformation] {
let mapper = MapperToModel(mechanism: l2Decrypt(data:))
return try self.providers.database.fetchAllAttempts().map(mapper.mapToModel(_:))
}

func fetchAttemptInformation(for extractedProfileId: Int64) throws -> AttemptInformation? {
let mapper = MapperToModel(mechanism: l2Decrypt(data:))
if let attemptDB = try self.providers.database.fetchAttemptInformation(for: extractedProfileId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ final class DataBrokerProtectionSecureVaultMock: DataBrokerProtectionSecureVault
return 1
}

func fetchAllAttempts() throws -> [AttemptInformation] {
[]
}

func fetchAttemptInformation(for extractedProfileId: Int64) throws -> AttemptInformation? {
return nil
}
Expand Down Expand Up @@ -990,6 +994,10 @@ final class MockDatabase: DataBrokerProtectionRepository {
return extractedProfilesFromBroker
}

func fetchAllAttempts() throws -> [AttemptInformation] {
[attemptInformation].compactMap { $0 }
}

func fetchAttemptInformation(for extractedProfileId: Int64) -> AttemptInformation? {
return attemptInformation
}
Expand Down

0 comments on commit 1dddb47

Please sign in to comment.