Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

backgroundActivitySettings.ts

283 lines11.4 KB

1import {
2 type BackgroundActivityProfile,
3 type BackgroundActivitySettings,
4 DEFAULT_BACKGROUND_ACTIVITY_PROFILE,
5 DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL,
6 DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL,
7 DEFAULT_PULL_REQUEST_LOOKUP_INTERVAL,
8 type ServerSettings,
9} from "@t3tools/contracts";
10import * as Duration from "effect/Duration";
11​
12export interface ResolvedBackgroundActivitySettings {
13 readonly profile: BackgroundActivityProfile;
14 readonly automaticGitFetchInterval: Duration.Duration;
15 readonly providerHealthRefreshInterval: Duration.Duration;
16 /** How long a branch keeps its last pull request lookup before the hosting CLI is asked again. */
17 readonly pullRequestLookupInterval: Duration.Duration;
18 readonly hostPowerMonitorActiveInterval: Duration.Duration;
19 readonly hostPowerMonitorIdleInterval: Duration.Duration;
20 readonly idleClientTtl: Duration.Duration;
21 readonly pauseWhenHostLocked: boolean;
22 readonly pauseWhenHostLowPower: boolean;
23 readonly pauseWhenClientLowPower: boolean;
24 readonly pauseWhenOnBattery: boolean;
26​
27const PRESET_SETTINGS: Record<BackgroundActivityProfile, ResolvedBackgroundActivitySettings> = {
28 performance: {
29 profile: "performance",
30 automaticGitFetchInterval: Duration.seconds(15),
31 providerHealthRefreshInterval: Duration.minutes(1),
32 pullRequestLookupInterval: Duration.minutes(1),
33 hostPowerMonitorActiveInterval: Duration.seconds(30),
34 hostPowerMonitorIdleInterval: Duration.minutes(2),
35 idleClientTtl: Duration.seconds(45),
36 pauseWhenHostLocked: true,
37 pauseWhenHostLowPower: false,
38 pauseWhenClientLowPower: false,
39 pauseWhenOnBattery: false,
40 },
41 balanced: {
42 profile: "balanced",
43 automaticGitFetchInterval: DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL,
44 providerHealthRefreshInterval: DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL,
45 pullRequestLookupInterval: DEFAULT_PULL_REQUEST_LOOKUP_INTERVAL,
46 hostPowerMonitorActiveInterval: Duration.seconds(30),
47 hostPowerMonitorIdleInterval: Duration.minutes(5),
48 idleClientTtl: Duration.seconds(45),
49 pauseWhenHostLocked: true,
50 pauseWhenHostLowPower: true,
51 pauseWhenClientLowPower: true,
52 pauseWhenOnBattery: false,
53 },
54 "battery-saver": {
55 profile: "battery-saver",
56 automaticGitFetchInterval: Duration.seconds(0),
57 providerHealthRefreshInterval: Duration.minutes(15),
58 pullRequestLookupInterval: Duration.minutes(15),
59 hostPowerMonitorActiveInterval: Duration.minutes(1),
60 hostPowerMonitorIdleInterval: Duration.minutes(10),
61 idleClientTtl: Duration.seconds(45),
62 pauseWhenHostLocked: true,
63 pauseWhenHostLowPower: true,
64 pauseWhenClientLowPower: true,
65 pauseWhenOnBattery: true,
66 },
67};
68​
69export function getBackgroundActivityPresetSettings(
70 profile: BackgroundActivityProfile,
71): ResolvedBackgroundActivitySettings {
72 return PRESET_SETTINGS[profile];
74​
75export function getBackgroundActivityBaseProfile(
76 backgroundActivity: BackgroundActivitySettings,
77): BackgroundActivityProfile {
78 if (backgroundActivity.profile === "custom") {
79 return backgroundActivity.baseProfile ?? DEFAULT_BACKGROUND_ACTIVITY_PROFILE;
80 }
81 return backgroundActivity.profile;
83​
84export function resolveBackgroundActivitySettings(
85 backgroundActivity: BackgroundActivitySettings,
86): ResolvedBackgroundActivitySettings {
87 const baseProfile = getBackgroundActivityBaseProfile(backgroundActivity);
88 const preset = PRESET_SETTINGS[baseProfile];
89 const overrides = backgroundActivity.profile === "custom" ? backgroundActivity.overrides : {};
90 return {
91 profile: baseProfile,
92 automaticGitFetchInterval:
93 overrides.automaticGitFetchInterval ?? preset.automaticGitFetchInterval,
94 providerHealthRefreshInterval:
95 overrides.providerHealthRefreshInterval ?? preset.providerHealthRefreshInterval,
96 pullRequestLookupInterval:
97 overrides.pullRequestLookupInterval ?? preset.pullRequestLookupInterval,
98 hostPowerMonitorActiveInterval:
99 overrides.hostPowerMonitorActiveInterval ?? preset.hostPowerMonitorActiveInterval,
100 hostPowerMonitorIdleInterval:
101 overrides.hostPowerMonitorIdleInterval ?? preset.hostPowerMonitorIdleInterval,
102 idleClientTtl: overrides.idleClientTtl ?? preset.idleClientTtl,
103 pauseWhenHostLocked: overrides.pauseWhenHostLocked ?? preset.pauseWhenHostLocked,
104 pauseWhenHostLowPower: overrides.pauseWhenHostLowPower ?? preset.pauseWhenHostLowPower,
105 pauseWhenClientLowPower: overrides.pauseWhenClientLowPower ?? preset.pauseWhenClientLowPower,
106 pauseWhenOnBattery: overrides.pauseWhenOnBattery ?? preset.pauseWhenOnBattery,
107 };
109​
110function durationsEqual(a: Duration.Duration, b: Duration.Duration): boolean {
111 return Duration.toMillis(a) === Duration.toMillis(b);
113​
114function resolvedSettingsEqual(
115 a: ResolvedBackgroundActivitySettings,
116 b: ResolvedBackgroundActivitySettings,
117): boolean {
118 return (
119 durationsEqual(a.automaticGitFetchInterval, b.automaticGitFetchInterval) &&
120 durationsEqual(a.providerHealthRefreshInterval, b.providerHealthRefreshInterval) &&
121 durationsEqual(a.pullRequestLookupInterval, b.pullRequestLookupInterval) &&
122 durationsEqual(a.hostPowerMonitorActiveInterval, b.hostPowerMonitorActiveInterval) &&
123 durationsEqual(a.hostPowerMonitorIdleInterval, b.hostPowerMonitorIdleInterval) &&
124 durationsEqual(a.idleClientTtl, b.idleClientTtl) &&
125 a.pauseWhenHostLocked === b.pauseWhenHostLocked &&
126 a.pauseWhenHostLowPower === b.pauseWhenHostLowPower &&
127 a.pauseWhenClientLowPower === b.pauseWhenClientLowPower &&
128 a.pauseWhenOnBattery === b.pauseWhenOnBattery
129 );
131​
132export function normalizeBackgroundActivitySettings(
133 backgroundActivity: BackgroundActivitySettings,
134): BackgroundActivitySettings {
135 if (backgroundActivity.profile !== "custom") {
136 return {
137 schemaVersion: 1,
138 profile: backgroundActivity.profile,
139 overrides: {},
140 };
141 }
142​
143 const resolved = resolveBackgroundActivitySettings(backgroundActivity);
144 const profiles: ReadonlyArray<BackgroundActivityProfile> = [
145 getBackgroundActivityBaseProfile(backgroundActivity),
146 "balanced",
147 "performance",
148 "battery-saver",
149 ];
150 for (const profile of profiles) {
151 if (resolvedSettingsEqual(resolved, PRESET_SETTINGS[profile])) {
152 return {
153 schemaVersion: 1,
154 profile,
155 overrides: {},
156 };
157 }
158 }
159​
160 const baseProfile = getBackgroundActivityBaseProfile(backgroundActivity);
161 const preset = PRESET_SETTINGS[baseProfile];
162 const overrides: BackgroundActivitySettings["overrides"] = {
163 ...(!durationsEqual(resolved.automaticGitFetchInterval, preset.automaticGitFetchInterval)
164 ? { automaticGitFetchInterval: resolved.automaticGitFetchInterval }
165 : {}),
166 ...(!durationsEqual(
167 resolved.providerHealthRefreshInterval,
168 preset.providerHealthRefreshInterval,
169 )
170 ? { providerHealthRefreshInterval: resolved.providerHealthRefreshInterval }
171 : {}),
172 ...(!durationsEqual(resolved.pullRequestLookupInterval, preset.pullRequestLookupInterval)
173 ? { pullRequestLookupInterval: resolved.pullRequestLookupInterval }
174 : {}),
175 ...(!durationsEqual(
176 resolved.hostPowerMonitorActiveInterval,
177 preset.hostPowerMonitorActiveInterval,
178 )
179 ? { hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval }
180 : {}),
181 ...(!durationsEqual(resolved.hostPowerMonitorIdleInterval, preset.hostPowerMonitorIdleInterval)
182 ? { hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval }
183 : {}),
184 ...(!durationsEqual(resolved.idleClientTtl, preset.idleClientTtl)
185 ? { idleClientTtl: resolved.idleClientTtl }
186 : {}),
187 ...(resolved.pauseWhenHostLocked !== preset.pauseWhenHostLocked
188 ? { pauseWhenHostLocked: resolved.pauseWhenHostLocked }
189 : {}),
190 ...(resolved.pauseWhenHostLowPower !== preset.pauseWhenHostLowPower
191 ? { pauseWhenHostLowPower: resolved.pauseWhenHostLowPower }
192 : {}),
193 ...(resolved.pauseWhenClientLowPower !== preset.pauseWhenClientLowPower
194 ? { pauseWhenClientLowPower: resolved.pauseWhenClientLowPower }
195 : {}),
196 ...(resolved.pauseWhenOnBattery !== preset.pauseWhenOnBattery
197 ? { pauseWhenOnBattery: resolved.pauseWhenOnBattery }
198 : {}),
199 };
200​
201 return {
202 schemaVersion: 1,
203 profile: "custom",
204 baseProfile,
205 overrides,
206 };
208​
209export function resolveServerBackgroundActivitySettings(
210 settings: ServerSettings,
211): ResolvedBackgroundActivitySettings {
212 const defaultBackgroundActivity: BackgroundActivitySettings = {
213 schemaVersion: 1,
214 profile: DEFAULT_BACKGROUND_ACTIVITY_PROFILE,
215 overrides: {},
216 };
217 const backgroundActivityIsDefault =
218 settings.backgroundActivity.profile === defaultBackgroundActivity.profile &&
219 settings.backgroundActivity.baseProfile === undefined &&
220 Object.keys(settings.backgroundActivity.overrides).length === 0;
221 const legacyProfile = settings.backgroundActivityProfile;
222 const hasLegacyOverrides =
223 legacyProfile !== DEFAULT_BACKGROUND_ACTIVITY_PROFILE ||
224 Duration.toMillis(settings.automaticGitFetchInterval) !==
225 Duration.toMillis(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL) ||
226 Duration.toMillis(settings.providerHealthRefreshInterval) !==
227 Duration.toMillis(DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL);
228 if (backgroundActivityIsDefault && hasLegacyOverrides) {
229 return resolveBackgroundActivitySettings({
230 schemaVersion: 1,
231 profile:
232 Duration.toMillis(settings.automaticGitFetchInterval) ===
233 Duration.toMillis(
234 getBackgroundActivityPresetSettings(legacyProfile).automaticGitFetchInterval,
235 ) &&
236 Duration.toMillis(settings.providerHealthRefreshInterval) ===
237 Duration.toMillis(
238 getBackgroundActivityPresetSettings(legacyProfile).providerHealthRefreshInterval,
239 )
240 ? legacyProfile
241 : "custom",
242 baseProfile: legacyProfile,
243 overrides: {
244 ...(Duration.toMillis(settings.automaticGitFetchInterval) !==
245 Duration.toMillis(
246 getBackgroundActivityPresetSettings(legacyProfile).automaticGitFetchInterval,
247 )
248 ? { automaticGitFetchInterval: settings.automaticGitFetchInterval }
249 : {}),
250 ...(Duration.toMillis(settings.providerHealthRefreshInterval) !==
251 Duration.toMillis(
252 getBackgroundActivityPresetSettings(legacyProfile).providerHealthRefreshInterval,
253 )
254 ? { providerHealthRefreshInterval: settings.providerHealthRefreshInterval }
255 : {}),
256 },
257 });
258 }
259 return resolveBackgroundActivitySettings(settings.backgroundActivity);
261​
262export function normalizeServerBackgroundActivitySettings(
263 settings: ServerSettings,
264): BackgroundActivitySettings {
265 const resolved = resolveServerBackgroundActivitySettings(settings);
266 return normalizeBackgroundActivitySettings({
267 schemaVersion: 1,
268 profile: "custom",
269 baseProfile: resolved.profile,
270 overrides: {
271 automaticGitFetchInterval: resolved.automaticGitFetchInterval,
272 providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
273 pullRequestLookupInterval: resolved.pullRequestLookupInterval,
274 hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
275 hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
276 idleClientTtl: resolved.idleClientTtl,
277 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
278 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
279 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
280 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
281 },
282 });