From 3c10495a4d8a3b9de6d68deaaa80aee0bf6d6bdc Mon Sep 17 00:00:00 2001 From: Mikhail Kasianov <96941969+kasianov-mikhail@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:16:18 +0300 Subject: [PATCH] Refactor StatProvider to improve data fetching logic and enhance documentation --- Sources/Scout/UI/StatProvider.swift | 57 +++++++++++++++++++---------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/Sources/Scout/UI/StatProvider.swift b/Sources/Scout/UI/StatProvider.swift index c33ca99..dec97cc 100644 --- a/Sources/Scout/UI/StatProvider.swift +++ b/Sources/Scout/UI/StatProvider.swift @@ -21,58 +21,75 @@ import CloudKit self.eventName = eventName } - /// Fetches the statistical data if it has not been fetched yet. - /// - Parameter container: The CloudKit container from which to fetch the data. + /// Fetches data if needed from the provided database. + /// + /// This asynchronous function checks if the data needs to be fetched and performs the fetch operation + /// using the given `DatabaseController` instance. + /// + /// - Parameter database: The `DatabaseController` instance used to fetch the data. + /// - Returns: An asynchronous operation that fetches the data if needed. /// func fetchIfNeeded(in database: DatabaseController) async { if data == nil { await fetch(in: database) } } +} + +// MARK: - Fetching Data - /// Fetches the statistical data from the CloudKit container. - /// - Parameter container: The CloudKit container from which to fetch the data. +extension StatProvider { + + /// Fetches data from the specified database asynchronously. + /// + /// - Parameter database: The `DatabaseController` instance from which to fetch data. /// private func fetch(in database: DatabaseController) async { let today = Calendar(identifier: .iso8601).startOfDay(for: Date()) let yearAgo = today.addingYear(-1).addingWeek(-1) - let predicate = NSPredicate( - format: "date >= %@ AND name == %@", - yearAgo as NSDate, - eventName - ) - - let query = CKQuery( - recordType: "DateIntMatrix", - predicate: predicate - ) - do { let records = try await database.allRecords( - matching: query, + matching: query(from: yearAgo), desiredKeys: nil ) - let points = try records.map(Matrix.init) + let rawPoints = try records.map(Matrix.init) .mergeDuplicates() .flatMap(ChartPoint.fromIntMatrix) - let series = RawPointData( + let rawData = RawPointData( from: yearAgo, to: today.addingDay(), - points: points + points: rawPoints ) - data = series.chartData(for: StatPeriod.components) + data = rawData.chartData(for: StatPeriod.components) } catch { print(error.localizedDescription) data = nil } } + + private func query(from date: Date) async throws -> CKQuery { + let predicate = NSPredicate( + format: "date >= %@ AND name == %@", + date as NSDate, + eventName + ) + + let query = CKQuery( + recordType: "DateIntMatrix", + predicate: predicate + ) + + return query + } } +// MARK: - Calendar Components + extension StatPeriod { /// A set of calendar components used for date calculations.