Skip to content

Commit

Permalink
Use sites scanned by broker instead of number of scans in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed Nov 29, 2023
1 parent 558c748 commit 70265ed
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,29 @@ struct MapperToUI {
}

func initialScanState(_ brokerProfileQueryData: [BrokerProfileQueryData]) -> DBPUIInitialScanState {
let totalScans = brokerProfileQueryData.reduce(0) { accumulator, element in
return accumulator + element.totalScans
// Total and current scans are misleading. The UI are counting this per broker and
// not by the total real cans that the app is doing.
let profileQueriesGroupedByBroker = Dictionary.init(grouping: brokerProfileQueryData, by: { $0.dataBroker.name })

let totalScans = profileQueriesGroupedByBroker.reduce(0) { accumulator, element in
return accumulator + element.value.totalScans
}
let currentScans = brokerProfileQueryData.reduce(0) { accumulator, element in
return accumulator + element.currentScans
let currentScans = profileQueriesGroupedByBroker.reduce(0) { accumulator, element in
return accumulator + element.value.currentScans
}

let scanProgress = DBPUIScanProgress(currentScans: currentScans, totalScans: totalScans)
let matches = mapMatchesToUI(brokerProfileQueryData)

return .init(resultsFound: matches, scanProgress: scanProgress)
}

private func mapMatchesToUI(_ brokerProfileQueryData: [BrokerProfileQueryData]) -> [DBPUIDataBrokerProfileMatch] {
let matches = brokerProfileQueryData.compactMap {
return brokerProfileQueryData.flatMap {
var profiles = [DBPUIDataBrokerProfileMatch]()
for extractedProfile in $0.extractedProfiles where !$0.profileQuery.deprecated {
var profiles = [mapToUI($0.dataBroker, extractedProfile: extractedProfile)]
profiles.append(mapToUI($0.dataBroker, extractedProfile: extractedProfile))

if !$0.dataBroker.mirrorSites.isEmpty {
let mirrorSitesMatches = $0.dataBroker.mirrorSites.compactMap { mirrorSite in
if mirrorSite.shouldWeIncludeMirrorSite() {
Expand All @@ -75,14 +82,10 @@ struct MapperToUI {
}
profiles.append(contentsOf: mirrorSitesMatches)
}

return profiles
}

return nil
return profiles
}

return matches.flatMap { $0 }
}

func maintenanceScanState(_ brokerProfileQueryData: [BrokerProfileQueryData]) -> DBPUIScanAndOptOutMaintenanceState {
Expand Down Expand Up @@ -206,22 +209,6 @@ extension String {

fileprivate extension BrokerProfileQueryData {

var totalScans: Int {
if profileQuery.deprecated {
return 0
} else {
return 1 + dataBroker.mirrorSites.filter { $0.shouldWeIncludeMirrorSite() }.count
}
}

var currentScans: Int {
if scanOperationData.lastRunDate != nil && !profileQuery.deprecated {
return 1 + dataBroker.mirrorSites.filter { $0.shouldWeIncludeMirrorSite() }.count
} else {
return 0
}
}

var sitesScanned: [String] {
if scanOperationData.lastRunDate != nil {
let scanEvents = scanOperationData.scanStartedEvents()
Expand All @@ -244,6 +231,34 @@ fileprivate extension BrokerProfileQueryData {
}
}

fileprivate extension Array where Element == BrokerProfileQueryData {

var totalScans: Int {
guard let broker = self.first?.dataBroker else { return 0 }

let areAllQueriesDeprecated = allSatisfy { $0.profileQuery.deprecated }

if areAllQueriesDeprecated {
return 0
} else {
return 1 + broker.mirrorSites.filter { $0.shouldWeIncludeMirrorSite() }.count
}
}

var currentScans: Int {
guard let broker = self.first?.dataBroker else { return 0 }

let areAllQueriesDeprecated = allSatisfy { $0.profileQuery.deprecated }
let areAllQueriesNotStartedScanning = allSatisfy { $0.scanOperationData.lastRunDate == nil }

if areAllQueriesDeprecated || areAllQueriesNotStartedScanning {
return 0
} else {
return 1 + broker.mirrorSites.filter { $0.shouldWeIncludeMirrorSite() }.count
}
}
}

fileprivate extension MirrorSite {

func shouldWeIncludeMirrorSite(for date: Date = Date()) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,61 @@ final class MapperToUITests: XCTestCase {

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, brokerProfileQueryData.count)
XCTAssertEqual(result.scanProgress.currentScans, 0)
XCTAssertTrue(result.resultsFound.isEmpty)
}

func testWhenAScanRan_thenCurrentScansGetsUpdated() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), .mock(lastRunDate: Date())]
func testWhenBrokerHasMoreThanOneProfileQuery_thenIsCountedAsOneInTotalScans() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [
.mock(dataBrokerName: "Broker #1"),
.mock(dataBrokerName: "Broker #1"),
.mock(dataBrokerName: "Broker #2")
]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, 2)
}

func testWhenAScanRanOnOneBroker_thenCurrentScansReflectsThatScansWereDoneOnThatBroker() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [
.mock(dataBrokerName: "Broker #1"),
.mock(dataBrokerName: "Broker #1", lastRunDate: Date()),
.mock(dataBrokerName: "Broker #2")
]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, brokerProfileQueryData.count)
XCTAssertEqual(result.scanProgress.currentScans, 1)
XCTAssertTrue(result.resultsFound.isEmpty)
}

func testWhenAScanRanAndHasAMatch_thenResultsFoundIsUpdated() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), .mock(lastRunDate: Date(), extractedProfile: .mockWithRemovedDate)]
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), .mock(lastRunDate: Date(), extractedProfile: .mockWithoutRemovedDate)]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, brokerProfileQueryData.count)
XCTAssertEqual(result.scanProgress.currentScans, 1)
XCTAssertEqual(result.resultsFound.count, 1)
}

func testWhenAScanRanAndHasAMatchForTheSameBroker_thenMatchesReflectsTheCorrectValue() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [
.mock(dataBrokerName: "Broker #1"),
.mock(dataBrokerName: "Broker #1", lastRunDate: Date(), extractedProfile: .mockWithoutRemovedDate),
.mock(dataBrokerName: "Broker #1", lastRunDate: Date(), extractedProfile: .mockWithoutRemovedDate)
]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.resultsFound.count, 2)
}

func testWhenAllScansRan_thenCurrentScansEqualsTotalScans() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(lastRunDate: Date()), .mock(lastRunDate: Date()), .mock(lastRunDate: Date())]
let brokerProfileQueryData: [BrokerProfileQueryData] = [
.mock(dataBrokerName: "Broker #1", lastRunDate: Date()),
.mock(dataBrokerName: "Broker #1", lastRunDate: Date()),
.mock(dataBrokerName: "Broker #2", lastRunDate: Date())
]

let result = sut.initialScanState(brokerProfileQueryData)

Expand All @@ -64,9 +92,10 @@ final class MapperToUITests: XCTestCase {

func testWhenScansHaveDeprecatedProfileQueries_thenThoseAreNotTakenIntoAccount() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [
.mock(lastRunDate: Date(), extractedProfile: .mockWithRemovedDate),
.mock(lastRunDate: Date()),
.mock(lastRunDate: Date(), extractedProfile: .mockWithRemovedDate, deprecated: true)
.mock(dataBrokerName: "Broker #1", lastRunDate: Date(), extractedProfile: .mockWithRemovedDate),
.mock(dataBrokerName: "Broker #1", lastRunDate: Date()),
.mock(dataBrokerName: "Broker #2", lastRunDate: Date()),
.mock(dataBrokerName: "Broker #3", lastRunDate: Date(), extractedProfile: .mockWithRemovedDate, deprecated: true)
]

let result = sut.initialScanState(brokerProfileQueryData)
Expand Down Expand Up @@ -136,28 +165,38 @@ final class MapperToUITests: XCTestCase {
}

func testWhenMirrorSiteIsNotInRemovedPeriod_thenItShouldBeAddedToTotalScans() {
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), .mock(mirrorSites: [.init(name: "mirror", addedAt: Date(), removedAt: nil)])]
let brokerProfileQueryWithMirrorSite: BrokerProfileQueryData = .mock(dataBrokerName: "Broker #1", mirrorSites: [.init(name: "mirror", addedAt: Date(), removedAt: nil)])
let brokerProfileQueryData: [BrokerProfileQueryData] = [
brokerProfileQueryWithMirrorSite,
brokerProfileQueryWithMirrorSite,
brokerProfileQueryWithMirrorSite
]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, brokerProfileQueryData.count + 1)
XCTAssertEqual(result.scanProgress.totalScans, 2)
}

func testWhenMirrorSiteIsInRemovedPeriod_thenItShouldNotBeAddedToTotalScans() {
let brokerWithMirrorSiteThatWasRemoved = BrokerProfileQueryData.mock(mirrorSites: [.init(name: "mirror", addedAt: Date(), removedAt: Date().yesterday)])
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), brokerWithMirrorSiteThatWasRemoved]
let brokerWithMirrorSiteThatWasRemoved = BrokerProfileQueryData.mock(dataBrokerName: "Broker #1", mirrorSites: [.init(name: "mirror", addedAt: Date(), removedAt: Date().yesterday)])
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(dataBrokerName: "Broker #1"), brokerWithMirrorSiteThatWasRemoved, .mock(dataBrokerName: "Broker #2")]

let result = sut.initialScanState(brokerProfileQueryData)

XCTAssertEqual(result.scanProgress.totalScans, brokerProfileQueryData.count)
XCTAssertEqual(result.scanProgress.totalScans, 2)
}

func testWhenMirrorSiteIsNotInRemovedPeriod_thenItShouldBeAddedToCurrentScans() {
let brokerWithMirrorSiteNotRemovedAndWithScan = BrokerProfileQueryData.mock(
dataBrokerName: "Broker #1",
lastRunDate: Date(),
mirrorSites: [.init(name: "mirror", addedAt: Date(), removedAt: nil)]
)
let brokerProfileQueryData: [BrokerProfileQueryData] = [.mock(), .mock(), brokerWithMirrorSiteNotRemovedAndWithScan]
let brokerProfileQueryData: [BrokerProfileQueryData] = [
brokerWithMirrorSiteNotRemovedAndWithScan,
brokerWithMirrorSiteNotRemovedAndWithScan,
.mock(dataBrokerName: "Broker #2")
]

let result = sut.initialScanState(brokerProfileQueryData)

Expand Down

0 comments on commit 70265ed

Please sign in to comment.