Skip to content

Commit

Permalink
[OPIK-902] share config building logic (#1168)
Browse files Browse the repository at this point in the history
* [OPIK-902] share config building logic

* [OPIK-902] build URL via constructor

---------

Co-authored-by: Yaroslav Boiko <[email protected]>
  • Loading branch information
awkoy and Yaroslav Boiko authored Jan 30, 2025
1 parent 309aa4f commit ea154af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { BASE_API_URL } from "@/api/api";
import CodeHighlighter from "@/components/shared/CodeHighlighter/CodeHighlighter";
import { maskAPIKey } from "@/lib/utils";
import { OPIK_URL_OVERRIDE_CONFIG } from "@/constants/shared";
import { buildApiKeyConfig, buildWorkspaceNameConfig } from "@/lib/utils";
import useAppStore from "@/store/AppStore";

const getConfigCode = (
workspaceName: string,
apiKey?: string,
shouldMaskApiKey = false,
) => {
if (!apiKey)
return `os.environ["OPIK_URL_OVERRIDE"] = "${window.location.origin}${BASE_API_URL}"`;
if (!apiKey) return OPIK_URL_OVERRIDE_CONFIG;

const apiKeyConfig = `os.environ["OPIK_API_KEY"] = "${
shouldMaskApiKey ? maskAPIKey(apiKey) : apiKey
}"`;
const workspaceConfig = `os.environ["OPIK_WORKSPACE"] = "${workspaceName}"`;
const apiKeyConfig = buildApiKeyConfig(apiKey, shouldMaskApiKey);
const workspaceConfig = buildWorkspaceNameConfig(workspaceName);

return `${apiKeyConfig} \n${workspaceConfig}`;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import CodeHighlighter from "@/components/shared/CodeHighlighter/CodeHighlighter";
import useAppStore from "@/store/AppStore";
import { BASE_API_URL, CODE_EXECUTOR_SERVICE_URL } from "@/api/api";
import { maskAPIKey } from "@/lib/utils";
import { CODE_EXECUTOR_SERVICE_URL } from "@/api/api";
import { buildApiKeyConfig, buildWorkspaceNameConfig } from "@/lib/utils";
import CodeExecutor from "../CodeExecutor/CodeExecutor";
import { OPIK_URL_OVERRIDE_CONFIG } from "@/constants/shared";

const CODE_BLOCK_1 = "pip install opik";

Expand All @@ -22,18 +23,16 @@ const putConfigInCode = ({
maskApiKey,
}: PutConfigInCodeArgs): string => {
if (apiKey) {
const apiKeyConfig = buildApiKeyConfig(apiKey, maskApiKey);
const workspaceConfig = buildWorkspaceNameConfig(workspaceName);

return code.replace(
OPIK_API_KEY_TEMPLATE,
`os.environ["OPIK_API_KEY"] = "${
maskApiKey ? maskAPIKey(apiKey) : apiKey
}"\nos.environ["OPIK_WORKSPACE"] = "${workspaceName}"`,
`${apiKeyConfig}\n${workspaceConfig}`,
);
}

return code.replace(
OPIK_API_KEY_TEMPLATE,
`os.environ["OPIK_URL_OVERRIDE"] = "${window.location.origin}${BASE_API_URL}"`,
);
return code.replace(OPIK_API_KEY_TEMPLATE, OPIK_URL_OVERRIDE_CONFIG);
};

type IntegrationTemplateProps = {
Expand Down
6 changes: 6 additions & 0 deletions apps/opik-frontend/src/constants/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BASE_API_URL } from "@/api/api";
import {
CELL_VERTICAL_ALIGNMENT,
COLUMN_TYPE,
Expand Down Expand Up @@ -32,3 +33,8 @@ export const CELL_HORIZONTAL_ALIGNMENT_MAP: Record<COLUMN_TYPE, string> = {
[COLUMN_TYPE.dictionary]: "justify-start",
[COLUMN_TYPE.numberDictionary]: "justify-start",
};

export const OPIK_URL_OVERRIDE_CONFIG = `os.environ["OPIK_URL_OVERRIDE"] = "${new URL(
BASE_API_URL,
window.location.origin,
).toString()}"`;
5 changes: 5 additions & 0 deletions apps/opik-frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ export const extractIdFromLocation = (location: string) =>

export const formatNumericData = (value: number, precision = 3) =>
String(round(value, precision));

export const buildApiKeyConfig = (apiKey: string, masked = false) =>
`os.environ["OPIK_API_KEY"] = "${masked ? maskAPIKey(apiKey) : apiKey}"`;
export const buildWorkspaceNameConfig = (workspaceName: string) =>
`os.environ["OPIK_WORKSPACE"] = "${workspaceName}"`;

0 comments on commit ea154af

Please sign in to comment.