Device Activity Report Per hour Screen-time of apps for Graph

Hi everyone, I need to display a Graph based on Screen-time of apps per hour, from 12Am to 11PM. Am able to get the screen-time data for whole day with each app's total screen-time value.

Am kind of confused how can I get the per hour screen-time of apps. I have applied filter of day

DeviceActivityFilter(
        segment: .daily(
            during: Calendar.current.dateInterval(
                of: .day, for: .now
            )!
        ),
        users: .all,
        devices: .init([.iPhone, .iPad])
    )

Am also using this data to display apps with their usage just like Apple's Screen_time in settings.

I need to display exact same graph just like Apple's screen in phone settings for a Day.

After spending some time I been able to resolve by myself. Filters played it's part

  1. To fetch daily per hour screen-time data, we need to use this filter
DeviceActivityFilter(
        segment: .hourly(during: Calendar.current.dateInterval(of: .day, for: .now)!),
        users: .all,
        devices: .init([.iPhone, .iPad])
    )

Hourly segment will ensure that data DeviceActivityResults will have activitySegments on per hour basis and .day dateInterval will give whole day's data.

  1. Now you can configure your data models accordingly for example:
    func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> ActivityReport {
        for await d in data {
            // activitySegments are per hour data because we have applied hourly filter

            for await a in d.activitySegments {
// Here you can check the associated hour to the segment
              let hour = a.dateInterval.start

                for await c in a.categories {
                    for await app in c.applications {
// Now this app's data will be on the basis of per hour 
                logger.log("Apps usage segment dateInterval: \(hour, privacy: .public)")                    }
                }
            }
        }
}
Device Activity Report Per hour Screen-time of apps for Graph
 
 
Q