-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DBP: Implement stats pixels #2812
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// DataBrokerProtectionPixelsUtilities.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Frequency: Int { | ||
case daily = 1 | ||
case weekly = 7 | ||
case monthly = 28 | ||
} | ||
|
||
final class DataBrokerProtectionPixelsUtilities { | ||
private static let calendar = Calendar.current | ||
|
||
static func shouldWeFirePixel(startDate: Date, endDate: Date, daysDifference: Frequency) -> Bool { | ||
if let differenceBetweenDates = numberOfDaysFrom(startDate: startDate, endDate: endDate) { | ||
return differenceBetweenDates >= daysDifference.rawValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be the definition of rolling windows (comparing to last sent pixel), but for these stats we're requested to send them using fixed windows from the beginning: every X days from the beginning - if one is missed then send the next one asap and then calculate the windows from the beginning again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @q71114 We have two things here: the date from when we calculated the stats and the date from when we fired the pixel. This one is only to calculate when we should fire the pixel; we use the date from the beginning to calculate the stats. For example, We fire the weekly pixel (calculating the stats since the beginning), save the date when it was fired, and when seven or more days pass, we fire it again, always calculating the stats from the beginning. |
||
} | ||
|
||
return false | ||
} | ||
|
||
static func numberOfDaysFrom(startDate: Date, endDate: Date) -> Int? { | ||
let components = calendar.dateComponents([.day], from: startDate, to: endDate) | ||
|
||
return components.day | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ I changed this because we were fetching the extracted profiles from the database inside the loop. This improves how many times we go to the database.