Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/autokitteh
Submodule autokitteh updated 46 files
+18 −53 .github/workflows/docker-main.yml
+0 −25 .github/workflows/docker-pr.yml
+67 −0 .github/workflows/docker.yml
+1 −1 .github/workflows/go.yml
+32 −36 .github/workflows/manual-docker-image.yml
+19 −47 .github/workflows/release.yml
+36 −1 .goreleaser.yaml
+2 −1 Dockerfile
+54 −0 cmd/ak/cmd/sessions/download_logs.go
+67 −12 cmd/ak/cmd/sessions/prints.go
+1 −0 cmd/ak/cmd/sessions/session.go
+20 −3 integrations/oauth/configs.go
+127 −0 internal/backend/sessions/sessiondownloadlogs_test.go
+79 −0 internal/backend/sessions/sessions.go
+20 −0 internal/backend/sessionsgrpcsvc/svc.go
+9 −0 proto/autokitteh/sessions/v1/svc.proto
+34 −7 proto/gen/go/autokitteh/sessions/v1/sessionsv1connect/svc.connect.go
+276 −140 proto/gen/go/autokitteh/sessions/v1/svc.pb.go
+2 −2 proto/gen/py/autokitteh_pb/sessions/v1/__init__.py
+19 −13 proto/gen/py/autokitteh_pb/sessions/v1/svc_pb2.py
+12 −0 proto/gen/py/autokitteh_pb/sessions/v1/svc_pb2.pyi
+33 −0 proto/gen/py/autokitteh_pb/sessions/v1/svc_pb2_grpc.py
+10 −1 proto/gen/ts/autokitteh/sessions/v1/svc_connect.ts
+74 −0 proto/gen/ts/autokitteh/sessions/v1/svc_pb.ts
+24 −0 runtimes/pythonrt/py-sdk/autokitteh/linear.py
+20 −1 runtimes/pythonrt/runner/main.py
+10 −0 sdk/sdkclients/sdksessionsclient/client.go
+1 −0 sdk/sdkservices/sessions.go
+9 −1 tests/sessions/python/import_error.txtar
+1 −0 tests/system/ak_test.go
+41 −0 tests/system/checks.go
+5 −1 tests/system/parse.go
+17 −1 tests/system/testdata/builds/build.txtar
+2 −4 tests/system/testdata/deployments/state.txtar
+1 −1 tests/system/testdata/end2end/builtin_funcs.txtar
+27 −0 tests/system/testdata/end2end/download_logs.txtar
+33 −0 tests/system/testdata/end2end/download_logs_with_path.txtar
+11 −1 tests/system/testdata/end2end/pyhttp.txtar
+0 −4 tests/system/testdata/schedule/redefine.txtar
+9 −13 tests/system/testdata/sessions/events_python_.txtar
+5 −4 tests/system/testdata/sessions/events_starlark.txtar
+1 −1 tests/system/testdata/sessions/start.txtar
+1 −3 tests/system/testdata/sessions/start_in_project.txtar
+9 −3 tests/system/testdata/sessions/stop.txtar
+0 −43 tests/system/testdata/sessions/term.txtar.disabled
+2 −1 web/webplatform/VERSION
144 changes: 36 additions & 108 deletions src/components/organisms/deployments/sessions/viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ import {
dateTimeFormat,
defaultSessionsPageSize,
defaultSessionTab,
maxLogsPageSize,
namespaces,
sessionLogRowHeight,
sessionTabs,
timeFormat,
} from "@constants";
import { LoggerService } from "@services/index";
import { SessionsService } from "@services/sessions.service";
import { EventListenerName, SessionLogType, SessionState } from "@src/enums";
import { EventListenerName, SessionState } from "@src/enums";
import { triggerEvent, useEventListener } from "@src/hooks";
import { SessionOutputLog, ViewerSession } from "@src/interfaces/models/session.interface";
import { convertSessionLogProtoToViewerOutput } from "@src/models";
import { ViewerSession } from "@src/interfaces/models/session.interface";
import { useActivitiesCacheStore, useOutputsCacheStore, useToastStore } from "@src/store";
import { copyToClipboard } from "@src/utilities";

Expand Down Expand Up @@ -52,115 +50,45 @@ export const SessionViewer = () => {
const [isLoading, setIsLoading] = useState(false);
const [isInitialLoad, setIsInitialLoad] = useState(true);
const addToast = useToastStore((state) => state.addToast);
const [isFetchingAllSessionPrints, setIsFetchingAllSessionPrints] = useState<"copy" | "download">();
const [isCopyingLogs, setIsCopyingLogs] = useState(false);
const [isDownloadingLogs, setIsDownloadingLogs] = useState(false);

const { loading: loadingOutputs, loadLogs: loadOutputs } = useOutputsCacheStore();
const { loading: loadingOutputs, loadLogs: loadOutputs, sessions: outputsSessions } = useOutputsCacheStore();
const { loading: loadingActivities, loadLogs: loadActivities, sessions } = useActivitiesCacheStore();

const getAllSessionLogs = async (pageToken: string): Promise<SessionOutputLog[]> => {
if (!sessionId) return [];

const { data, error } = await SessionsService.getOutputsBySessionId(sessionId, pageToken, maxLogsPageSize);

if (error || !data) {
throw new Error(t("errorFetchingLogs"));
}

const logs = [...data.logs];

if (data.nextPageToken) {
const nextLogs = await getAllSessionLogs(data.nextPageToken);
logs.push(...nextLogs);
}

const { data: sessionStateRecords, error: sessionStateRequestError } =
await SessionsService.getLogRecordsBySessionId(sessionId, undefined, 1, SessionLogType.State);
if (sessionStateRequestError) {
addToast({ message: t("activityLogsFetchError"), type: "error" });
}
const lastSessionState = convertSessionLogProtoToViewerOutput(sessionStateRecords?.records?.[0]);

if (lastSessionState) {
logs.unshift(lastSessionState);
}

return logs;
};

const copySessionLogs = async () => {
await handleSessionLogs(
async (logContent) => {
if (!logContent) return;
const { isError, message } = await copyToClipboard(logContent);
addToast({
message: message,
type: isError ? "error" : "success",
});
},
t("errorCopyingLogs"),
t("noLogsToDownload"),
"copy"
if (!sessionId) return;
setIsCopyingLogs(true);
const { isError, message } = await copyToClipboard(
outputsSessions[sessionId].outputs.map((log) => `[${log.time}]: ${log.print}`).join("\n")
);
addToast({
message: message,
type: isError ? "error" : "success",
});
setIsCopyingLogs(false);
};

const downloadSessionLogs = async () => {
await handleSessionLogs(
(logContent) => {
if (!logContent) return;
const blob = new Blob([logContent], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const dateTime = dayjs().format(dateTimeFormat);
const fileName = `${sessionInfo?.sourceType?.toLowerCase() || "session"}-${dateTime}.log`;

const link = Object.assign(document.createElement("a"), {
href: url,
download: fileName,
});

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
},
t("errorDownloadingLogs"),
t("noLogsToDownload"),
"download"
);
};

const handleSessionLogs = async (
callback: (logContent: string) => Promise<void> | void,
errorMessage: string,
noLogsMessage: string,
action: "copy" | "download"
) => {
if (!sessionId || !sessionInfo) return;
setIsFetchingAllSessionPrints(action);

try {
const logContent = (await getAllSessionLogs(""))
.reverse()
.map((log) => `[${log.time}]: ${log.print}`)
.join("\n");

if (!logContent.length) {
addToast({
message: noLogsMessage,
type: "error",
});
setIsFetchingAllSessionPrints(undefined);
return;
}

await callback(logContent);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
addToast({
message: errorMessage,
type: "error",
});
if (!sessionId) return;
setIsDownloadingLogs(true);
const { data: logContent } = await SessionsService.downloadLogs(sessionId);
if (!logContent) {
addToast({ message: t("noLogsFound"), type: "error" });
return;
}
setIsFetchingAllSessionPrints(undefined);
const blob = new Blob([logContent], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
if (!sessionInfo) return;
const timestamp = dayjs(sessionInfo.createdAt).format("YY-MM-DD_HH-mm");
link.download = `${timestamp}_${sessionId}.log`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
setIsDownloadingLogs(false);
};

const closeEditor = useCallback(() => {
Expand Down Expand Up @@ -373,10 +301,10 @@ export const SessionViewer = () => {
<Tooltip content={t("copy")} position="bottom">
<Button
className="group py-2 pl-2 text-white disabled:cursor-not-allowed disabled:opacity-50"
disabled={isFetchingAllSessionPrints === "copy"}
disabled={isCopyingLogs}
onClick={copySessionLogs}
>
{isFetchingAllSessionPrints === "copy" ? (
{isCopyingLogs ? (
<div className="flex size-4 items-center">
<Loader size="sm" />
</div>
Expand All @@ -388,10 +316,10 @@ export const SessionViewer = () => {
<Tooltip content={t("download")} position="bottom">
<Button
className="group py-2 pl-2 text-white disabled:cursor-not-allowed disabled:opacity-50"
disabled={isFetchingAllSessionPrints === "download"}
disabled={isDownloadingLogs}
onClick={downloadSessionLogs}
>
{isFetchingAllSessionPrints === "download" ? (
{isDownloadingLogs ? (
<div className="flex size-4 items-center">
<Loader size="sm" />
</div>
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/errors/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"failedRemoveSessionExtended": "Failed to remove session, ID: {{sessionId}}",
"failedStopSession": "Failed to stop session",
"failedStopSessionExtended": "Failed to stop session ID: {{sessionId}}, error: {{error}}",
"sessionDownloadLogsFailedExtended": "Failed to download session logs, ID: {{sessionId}}, error: {{error}}",
"fileAddFailed": "Failed to create file: {{fileName}}",
"fileAddFailedExtended": "Failed to create a file. Project ID: {{projectId}}, File name: {{fileName}}, Error: {{error}}",
"fileAddFailedExtendedNoError": "Failed to create a file. Project ID: {{projectId}}, File name: {{fileName}}",
Expand Down
17 changes: 17 additions & 0 deletions src/services/sessions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,21 @@ export class SessionsService {
return { data: undefined, error };
}
}

static async downloadLogs(sessionId: string): Promise<ServiceResponse<Uint8Array<ArrayBuffer>>> {
try {
const { data: logs } = await sessionsClient.downloadLogs({ sessionId });

return { data: logs, error: undefined };
} catch (error) {
const log = t("sessionDownloadLogsFailedExtended", {
sessionId,
error,
ns: "errors",
});
LoggerService.error(namespaces.sessionsService, log);

return { data: undefined, error };
}
}
}
1 change: 1 addition & 0 deletions src/utilities/copyToClipboard.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const retryCopyToClipboard = (text: string): { isError: boolean; message: string

export const copyToClipboard = async (text: string): Promise<{ isError: boolean; message: string }> => {
try {
await new Promise((resolve) => setTimeout(resolve, 0));
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
} else {
Expand Down