Skip to content
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

task: Do not call history sync API for sensor without measurement #2165 #2167

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,47 @@ public final class RuuviServiceCloudSyncImpl: RuuviServiceCloudSync {

ruuviStorage.readAll().on(success: { [weak self] localSensors in
guard let sSelf = self else { return }
let sensorsWithHistory = localSensors.filter { sensor in

// Read the latest record for each sensor asynchronously, filter sensors with history.
let sensorHistoryFutures = localSensors.compactMap {
sensor -> Future<(AnyRuuviTagSensor, RuuviTagSensorRecord?), RuuviServiceError>? in

// Sensor should be a cloud sensor with allow max history days greater than 0.
// Also, the sensor should already have a measurement on the latest measurement
// table from database.
guard sensor.isCloud,
let maxHistoryDays = sensor.maxHistoryDays
else {
return false
let maxHistoryDays = sensor.maxHistoryDays,
maxHistoryDays > 0 else {
return nil
}
return maxHistoryDays > 0

// Read the latest record for the sensor
return sSelf.ruuviStorage.readLatest(sensor).map { record in
(sensor, record)
}.mapError({ error in
return .ruuviStorage(error)
})
}
let syncHistory = sensorsWithHistory.map { sSelf.sync(sensor: $0) }
Future.zip(syncHistory).on(success: { _ in
promise.succeed(value: true)

Future.zip(sensorHistoryFutures).on(success: { sensorRecords in
// Filter sensors that have no records, then sync
let sensorsToSync = sensorRecords
.filter { $0.1 != nil } // Only include sensors that have a valid record
.map { $0.0 } // Extract the sensors

let syncHistoryFutures = sensorsToSync.map { sSelf.sync(sensor: $0) }

// Zip all sync operations together and handle the final result
Future.zip(syncHistoryFutures).on(success: { _ in
promise.succeed(value: true)
}, failure: { error in
promise.fail(error: error)
})
}, failure: { error in
promise.fail(error: error)
})
})

return promise.future
}

Expand Down
Loading