-
Notifications
You must be signed in to change notification settings - Fork 766
feat: add claudePeakHoursEnabled setting and integrate into UsageMenu… #611
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
Open
hello-amed
wants to merge
5
commits into
steipete:main
Choose a base branch
from
hello-amed:feature/claude-peak-hours-indicator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b314126
feat: add claudePeakHoursEnabled setting and integrate into UsageMenu…
hello-amed 09a4420
Resolved PR feedback:
hello-amed 702c0a3
Update ProvidersPane to include claudePeakHoursEnabled in UsageMenuCa…
hello-amed ccf1c46
Refine subtitle for peak hours indicator in ClaudeProviderImplementation
hello-amed 761febd
Refactor peak hours calculation in ClaudePeakHours to improve accurac…
hello-amed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
Sources/CodexBarCore/Providers/Claude/ClaudePeakHours.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import Foundation | ||
|
|
||
| public enum ClaudePeakHours: Sendable { | ||
| private static let peakTimeZone = TimeZone(identifier: "America/New_York")! | ||
| private static let peakStartHour = 8 | ||
| private static let peakEndHour = 14 | ||
|
|
||
| public struct Status: Sendable, Equatable { | ||
| public let isPeak: Bool | ||
| public let label: String | ||
| } | ||
|
|
||
| public static func status(at date: Date) -> Status { | ||
| let calendar = self.calendar() | ||
| let components = calendar.dateComponents([.hour, .minute, .weekday], from: date) | ||
|
|
||
| guard let hour = components.hour, | ||
| let minute = components.minute, | ||
| let weekday = components.weekday | ||
| else { | ||
| return Status(isPeak: false, label: "Off-peak") | ||
| } | ||
|
|
||
| let isWeekday = weekday >= 2 && weekday <= 6 | ||
| let nowMinutes = hour * 60 + minute | ||
| let peakStartMinutes = self.peakStartHour * 60 | ||
| let peakEndMinutes = self.peakEndHour * 60 | ||
| let isInPeakWindow = nowMinutes >= peakStartMinutes && nowMinutes < peakEndMinutes | ||
|
|
||
| if isWeekday && isInPeakWindow { | ||
| let remaining = peakEndMinutes - nowMinutes | ||
| return Status( | ||
| isPeak: true, | ||
| label: "Peak · ends in \(self.formatDuration(minutes: remaining))") | ||
| } | ||
|
|
||
| let nextPeak = self.nextPeakStart(after: date, calendar: calendar) | ||
| let seconds = nextPeak.timeIntervalSince(date) | ||
| let minutes = max(Int(seconds / 60), 0) | ||
| return Status( | ||
| isPeak: false, | ||
| label: "Off-peak · peak in \(self.formatDuration(minutes: minutes))") | ||
| } | ||
|
|
||
| private static func nextPeakStart(after date: Date, calendar: Calendar) -> Date { | ||
| guard let todayPeak = calendar.date( | ||
| bySettingHour: self.peakStartHour, | ||
| minute: 0, | ||
| second: 0, | ||
| of: date) else { return date } | ||
|
|
||
| let anchor = todayPeak > date ? todayPeak : calendar.date(byAdding: .day, value: 1, to: todayPeak) ?? date | ||
| let weekday = calendar.component(.weekday, from: anchor) | ||
|
|
||
| let skip: Int | ||
| switch weekday { | ||
| case 1: skip = 1 | ||
| case 7: skip = 2 | ||
| default: skip = 0 | ||
| } | ||
|
|
||
| if skip == 0 { return anchor } | ||
| return calendar.date(byAdding: .day, value: skip, to: anchor) ?? anchor | ||
| } | ||
|
|
||
| private static func formatDuration(minutes: Int) -> String { | ||
| let h = minutes / 60 | ||
| let m = minutes % 60 | ||
| if h == 0 { | ||
| return "\(m)m" | ||
| } | ||
| if m == 0 { | ||
| return "\(h)h" | ||
| } | ||
| return "\(h)h \(m)m" | ||
| } | ||
|
|
||
| private static func calendar() -> Calendar { | ||
| var cal = Calendar(identifier: .gregorian) | ||
| cal.timeZone = self.peakTimeZone | ||
| return cal | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| struct ClaudePeakHoursTests { | ||
| private static let eastern = TimeZone(identifier: "America/New_York")! | ||
|
|
||
| private func date( | ||
| year: Int = 2026, | ||
| month: Int = 3, | ||
| day: Int, | ||
| hour: Int, | ||
| minute: Int = 0 | ||
| ) -> Date { | ||
| var cal = Calendar(identifier: .gregorian) | ||
| cal.timeZone = Self.eastern | ||
| return cal.date(from: DateComponents( | ||
| year: year, month: month, day: day, | ||
| hour: hour, minute: minute))! | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayMorningBeforePeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 7)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 1h") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayJustBeforePeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 7, minute: 45)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 15m") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayPeakStart() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 8)) | ||
| #expect(status.isPeak) | ||
| #expect(status.label == "Peak · ends in 6h") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayMidPeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 11, minute: 30)) | ||
| #expect(status.isPeak) | ||
| #expect(status.label == "Peak · ends in 2h 30m") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayPeakEndBoundary() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 13, minute: 59)) | ||
| #expect(status.isPeak) | ||
| #expect(status.label == "Peak · ends in 1m") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayAfterPeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 14)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 18h") | ||
| } | ||
|
|
||
| @Test | ||
| func weekdayLateEvening() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 26, hour: 23)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 9h") | ||
| } | ||
|
|
||
| @Test | ||
| func saturdayMorning() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 28, hour: 10)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 46h") | ||
| } | ||
|
|
||
| @Test | ||
| func sundayEvening() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 29, hour: 21)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 11h") | ||
| } | ||
|
|
||
| @Test | ||
| func fridayAfterPeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 27, hour: 15)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 65h") | ||
| } | ||
|
|
||
| @Test | ||
| func fridayPeak() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 27, hour: 12)) | ||
| #expect(status.isPeak) | ||
| #expect(status.label == "Peak · ends in 2h") | ||
| } | ||
|
|
||
| @Test | ||
| func springForwardWeekend() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 7, hour: 10)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 45h") | ||
| } | ||
|
|
||
| @Test | ||
| func mondayMidnight() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 23, hour: 0)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 8h") | ||
| } | ||
|
|
||
| @Test | ||
| func peakWithMinuteGranularity() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 25, hour: 12, minute: 15)) | ||
| #expect(status.isPeak) | ||
| #expect(status.label == "Peak · ends in 1h 45m") | ||
| } | ||
|
|
||
| @Test | ||
| func saturdayMidnight() { | ||
| let status = ClaudePeakHours.status(at: self.date(day: 28, hour: 0)) | ||
| #expect(!status.isPeak) | ||
| #expect(status.label == "Off-peak · peak in 56h") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.