Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 12 additions & 5 deletions apps/mobile/src/features/usage/UsageRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
makeWindow,
} from "@t3tools/shared/usageFormat";
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
import { Platform, Pressable, RefreshControl, ScrollView, View } from "react-native";
import { Alert, Platform, Pressable, RefreshControl, ScrollView, View } from "react-native";
import Animated, { Easing, FadeIn, LinearTransition, ReduceMotion } from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";

Expand Down Expand Up @@ -129,10 +129,17 @@ export function UsageRouteScreen() {
}
refreshingRef.current = true;
setRefreshingUsage(true);
void refresh(nextWindow).finally(() => {
refreshingRef.current = false;
setRefreshingUsage(false);
});
void refresh(nextWindow)
.catch((error: unknown) => {
Alert.alert(
"Could not refresh usage",
error instanceof Error ? error.message : "Try again.",
);
})
.finally(() => {
refreshingRef.current = false;
setRefreshingUsage(false);
});
};

const showEnvironmentFilter = environments.length > 0 || selectedEnvironmentIds !== null;
Expand Down
20 changes: 17 additions & 3 deletions apps/mobile/src/state/usage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { uuidv4 } from "../lib/uuid";
/**
* Multi-environment usage state.
*
Expand All @@ -17,10 +18,16 @@ import {
type UsageSummaryInput,
} from "@t3tools/contracts";
import { refreshUsage } from "@t3tools/client-runtime/state/usage";
import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge";
import {
mergeUsage,
retainUsageStatuses,
type SettledUsageStatuses,
type EnvironmentUsage,
type MergedUsage,
} from "@t3tools/shared/usageMerge";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { useCallback, useMemo } from "react";
import { useCallback, useEffect, useMemo, useRef } from "react";

import { appAtomRegistry } from "./atom-registry";
import { environmentPresentations } from "./presentation";
Expand Down Expand Up @@ -102,7 +109,13 @@ export function useUsage(
],
);
const atom = usageByWindowAtom(windowKey);
const environments = useAtomValue(atom);
const currentEnvironments = useAtomValue(atom);
const settledStatuses = useRef<SettledUsageStatuses<EnvironmentUsageStatus> | null>(null);
const retained = retainUsageStatuses(windowKey, currentEnvironments, settledStatuses.current);
useEffect(() => {
settledStatuses.current = retained.settled;
}, [retained.settled]);
const environments = retained.visible;
const selectedEnvironments = useMemo(
() =>
selectedEnvironmentIds === null
Expand All @@ -115,6 +128,7 @@ export function useUsage(
(nextInput?: UsageSummaryInput) =>
refreshUsage({
registry: appAtomRegistry,
refreshToken: uuidv4(),
server: serverEnvironment,
presentations: environmentPresentations,
environmentIds: selectedEnvironments.map(({ environmentId }) => environmentId),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const RPC_REQUIRED_SCOPES = {
[WS_METHODS.serverGetResourceTelemetryHistory]: AuthOrchestrationReadScope,
[WS_METHODS.serverRetryResourceTelemetry]: AuthOrchestrationOperateScope,
[WS_METHODS.serverGetUsageSummary]: AuthOrchestrationReadScope,
[WS_METHODS.serverGetUsageThreadBreakdown]: AuthOrchestrationReadScope,
[WS_METHODS.serverRefreshUsageRates]: AuthOrchestrationReadScope,
[WS_METHODS.serverSignalProcess]: AuthOrchestrationOperateScope,
[WS_METHODS.serverReportClientActivity]: AuthOrchestrationReadScope,
Expand Down
11 changes: 10 additions & 1 deletion apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import * as ExternalLauncher from "./process/externalLauncher.ts";
import { pullRequestHttpApiLayer } from "./pullRequest/http.ts";
import * as PullRequestProviderRegistry from "./pullRequest/PullRequestProviderRegistry.ts";
import * as PullRequestService from "./pullRequest/PullRequestService.ts";
import { ProjectionProjectRepositoryLive } from "./persistence/Layers/ProjectionProjects.ts";
import { ProjectionThreadRepositoryLive } from "./persistence/Layers/ProjectionThreads.ts";
import { layerConfig as SqlitePersistenceLayerLive } from "./persistence/Layers/Sqlite.ts";
import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
Expand Down Expand Up @@ -202,7 +204,14 @@ const BackgroundLayerLive = BackgroundPolicy.layer.pipe(
Layer.provideMerge(ServerSettingsLayerLive),
);

const UsageLayerLive = UsageService.layer.pipe(Layer.provide(ServerSettingsLayerLive));
const UsageLayerLive = UsageService.layer.pipe(
// Projects resolve each session's cwd to the project it ran in; threads and
// resume cursors attribute sessions to threads for the drill-down.
Layer.provide(ProjectionProjectRepositoryLive),
Layer.provide(ProjectionThreadRepositoryLive),
Layer.provide(ProviderSessionRuntime.layer),
Layer.provide(ServerSettingsLayerLive),
);

const ResourceDiagnosticsLayerLive = Layer.mergeAll(
HostResources.layer,
Expand Down
Loading
Loading