Skip to content

Commit

Permalink
add LoadBalancerBaseURl gloabl variable
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianWhitneyAI committed Nov 14, 2024
1 parent 3e9a0eb commit 31d4abf
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 51 deletions.
17 changes: 13 additions & 4 deletions packages/core/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import GlobalActionButtonRow from "./components/GlobalActionButtonRow";
import StatusMessage from "./components/StatusMessage";
import TutorialTooltip from "./components/TutorialTooltip";
import QuerySidebar from "./components/QuerySidebar";
import { FileExplorerServiceBaseUrl } from "./constants";
import { AicsLoadBalancerBaseUrl, FileExplorerServiceBaseUrl } from "./constants";
import { interaction, selection } from "./state";
import useLayoutMeasurements from "./hooks/useLayoutMeasurements";

Expand All @@ -39,11 +39,15 @@ interface AppProps {
// Localhost: "https://localhost:9081"
// Stage: "http://stg-aics-api.corp.alleninstitute.org"
// From the web (behind load balancer): "/"
aicsLoadBalancerBaseUrl?: string;
fileExplorerServiceBaseUrl?: string;
}

export default function App(props: AppProps) {
const { fileExplorerServiceBaseUrl = FileExplorerServiceBaseUrl.PRODUCTION } = props;
const {
aicsLoadBalancerBaseUrl = AicsLoadBalancerBaseUrl.PRODUCTION,
fileExplorerServiceBaseUrl = FileExplorerServiceBaseUrl.PRODUCTION,
} = props;

const dispatch = useDispatch();
const hasQuerySelected = useSelector(selection.selectors.hasQuerySelected);
Expand Down Expand Up @@ -80,8 +84,13 @@ export default function App(props: AppProps) {

// Set data source base urls
React.useEffect(() => {
dispatch(interaction.actions.initializeApp(fileExplorerServiceBaseUrl));
}, [dispatch, fileExplorerServiceBaseUrl]);
dispatch(
interaction.actions.initializeApp({
aicsLoadBalancerBaseUrl,
fileExplorerServiceBaseUrl,
})
);
}, [dispatch, aicsLoadBalancerBaseUrl, fileExplorerServiceBaseUrl]);

// Respond to screen size changes
React.useEffect(() => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export enum FileExplorerServiceBaseUrl {
PRODUCTION = "https://production.int.allencell.org",
}

export enum AicsLoadBalancerBaseUrl {
LOCALHOST = "http://localhost:8080",
STAGING = "http://stg-aics.corp.alleninstitute.org",
PRODUCTION = "http://aics.corp.alleninstitute.org",
}

export const TOP_LEVEL_FILE_ANNOTATIONS = [
new Annotation({
annotationDisplayName: "File ID",
Expand Down
13 changes: 2 additions & 11 deletions packages/core/entity/FileDetail/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AnnotationName from "../Annotation/AnnotationName";
import { FileExplorerServiceBaseUrl } from "../../constants";
import { FmsFileAnnotation } from "../../services/FileService";
import { renderZarrThumbnailURL } from "./RenderZarrThumbnailURL";

Expand Down Expand Up @@ -188,21 +187,13 @@ export default class FileDetail {
return this.thumbnail;
}

public getLinkToPlateUI(baseURL: string): string | undefined {
public getLinkToPlateUI(labkeyHost: string): string | undefined {
// Grabbing plate barcode
const platebarcode = this.getFirstAnnotationValue(AnnotationName.PLATE_BARCODE);

if (!platebarcode) {
return undefined;
}

let labkeyHost = "localhost:9081";
if (baseURL === FileExplorerServiceBaseUrl.PRODUCTION) {
labkeyHost = "aics.corp.alleninstitute.org";
} else if (baseURL === FileExplorerServiceBaseUrl.STAGING) {
labkeyHost = "stg-aics.corp.alleninstitute.org";
}
return `http://${labkeyHost}/labkey/aics_microscopy/AICS/editPlate.view?Barcode=${platebarcode}`;
return `${labkeyHost}/labkey/aics_microscopy/AICS/editPlate.view?Barcode=${platebarcode}`;
}

public getAnnotationNameToLinkMap(): { [annotationName: string]: string } {
Expand Down
6 changes: 2 additions & 4 deletions packages/core/hooks/useOpenWithMenuItems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ export default (fileDetails?: FileDetail, filters?: FileFilter[]): IContextualMe
const annotationNameToAnnotationMap = useSelector(
metadata.selectors.getAnnotationNameToAnnotationMap
);
const fileExplorerServiceBaseUrl = useSelector(
interaction.selectors.getFileExplorerServiceBaseUrl
);
const aicsLoadBalancerBaseUrl = useSelector(interaction.selectors.getAicsLoadBalancerBaseUrl);

const plateLink = fileDetails?.getLinkToPlateUI(fileExplorerServiceBaseUrl);
const plateLink = fileDetails?.getLinkToPlateUI(aicsLoadBalancerBaseUrl);
const annotationNameToLinkMap = React.useMemo(
() =>
fileDetails?.annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class HttpFileService extends HttpServiceBase implements FileServ
public async cacheFiles(
fileIds: string[]
): Promise<{ cacheFileStatuses: { [fileId: string]: string } }> {
const requestUrl = `${this.baseUrl}/${HttpFileService.BASE_FILE_CACHE_URL}${this.pathSuffix}`;
const requestUrl = `${this.aicsLoadBalancerBaseUrl}/${HttpFileService.BASE_FILE_CACHE_URL}${this.pathSuffix}`;
const requestBody = JSON.stringify({ fileIds });

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import FileDownloadServiceNoop from "../../../FileDownloadService/FileDownloadSe

describe("HttpFileService", () => {
const baseUrl = "test";
const aicsLoadBalancerBaseUrlMock = "http://loadbalancer-test.aics.corp.alleninstitute.org";
const fileIds = ["abc123", "def456", "ghi789", "jkl012"];
const files = fileIds.map((file_id) => ({
file_id,
Expand All @@ -28,7 +29,7 @@ describe("HttpFileService", () => {

it("issues request for files that match given parameters", async () => {
const httpFileService = new HttpFileService({
baseUrl,
baseUrl: baseUrl,
httpClient,
downloadService: new FileDownloadServiceNoop(),
});
Expand Down Expand Up @@ -80,7 +81,7 @@ describe("HttpFileService", () => {

describe("cacheFiles", () => {
const httpClient = createMockHttpClient({
when: `${baseUrl}/${HttpFileService.BASE_FILE_CACHE_URL}`,
when: `${aicsLoadBalancerBaseUrlMock}/${HttpFileService.BASE_FILE_CACHE_URL}`,
respondWith: {
data: {
cacheFileStatuses: {
Expand All @@ -94,7 +95,7 @@ describe("HttpFileService", () => {
it("sends file IDs to be cached and returns their statuses", async () => {
// Arrange
const fileService = new HttpFileService({
baseUrl,
aicsLoadBalancerBaseUrl: aicsLoadBalancerBaseUrlMock,
httpClient,
downloadService: new FileDownloadServiceNoop(),
});
Expand Down
22 changes: 21 additions & 1 deletion packages/core/services/HttpServiceBase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import axios, { AxiosInstance } from "axios";
import { Policy } from "cockatiel";
import LRUCache from "lru-cache";

import { FileExplorerServiceBaseUrl } from "../../constants";
import { AicsLoadBalancerBaseUrl, FileExplorerServiceBaseUrl } from "../../constants";
import RestServiceResponse from "../../entity/RestServiceResponse";

export interface ConnectionConfig {
aicsLoadBalancerBaseUrl?: string | keyof typeof AicsLoadBalancerBaseUrl;
applicationVersion?: string;
baseUrl?: string | keyof typeof FileExplorerServiceBaseUrl;
httpClient?: AxiosInstance;
Expand All @@ -14,6 +15,7 @@ export interface ConnectionConfig {
}

export const DEFAULT_CONNECTION_CONFIG = {
aicsLoadBalancerBaseUrl: AicsLoadBalancerBaseUrl.PRODUCTION,
baseUrl: FileExplorerServiceBaseUrl.PRODUCTION,
httpClient: axios.create(),
};
Expand Down Expand Up @@ -97,15 +99,22 @@ export default class HttpServiceBase {
.join("");
}

public aicsLoadBalancerBaseUrl: string | keyof typeof AicsLoadBalancerBaseUrl =
DEFAULT_CONNECTION_CONFIG.aicsLoadBalancerBaseUrl;
public baseUrl: string | keyof typeof FileExplorerServiceBaseUrl =
DEFAULT_CONNECTION_CONFIG.baseUrl;

protected httpClient = DEFAULT_CONNECTION_CONFIG.httpClient;
private applicationVersion = "NOT SET";
private userName?: string;
protected readonly pathSuffix: string = "";
private readonly urlToResponseDataCache = new LRUCache<string, any>({ max: MAX_CACHE_SIZE });

constructor(config: ConnectionConfig = {}) {
if (config.aicsLoadBalancerBaseUrl) {
this.setAicsLoadBalancerBaseUrl(config.aicsLoadBalancerBaseUrl);
}

if (config.applicationVersion) {
this.setApplicationVersion(config.applicationVersion);
}
Expand Down Expand Up @@ -234,6 +243,17 @@ export default class HttpServiceBase {
return new RestServiceResponse(response.data);
}

public setAicsLoadBalancerBaseUrl(
aicsLoadBalancerBaseUrl: string | keyof typeof AicsLoadBalancerBaseUrl
) {
if (this.aicsLoadBalancerBaseUrl !== aicsLoadBalancerBaseUrl) {
// bust cache when base url changes
this.urlToResponseDataCache.reset();
}

this.aicsLoadBalancerBaseUrl = aicsLoadBalancerBaseUrl;
}

public setApplicationVersion(applicationVersion: string) {
this.applicationVersion = applicationVersion;
this.setHeaders();
Expand Down
13 changes: 7 additions & 6 deletions packages/core/state/interaction/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,13 @@ export interface InitializeApp {
payload: string;
}

export function initializeApp(baseUrl: string): InitializeApp {
return {
type: INITIALIZE_APP,
payload: baseUrl,
};
}
export const initializeApp = (payload: {
aicsLoadBalancerBaseUrl: string;
fileExplorerServiceBaseUrl: string;
}) => ({
type: INITIALIZE_APP,
payload,
});

/**
* PROCESS AND STATUS RELATED ENUMS, INTERFACES, ETC.
Expand Down
5 changes: 4 additions & 1 deletion packages/core/state/interaction/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import DatabaseServiceNoop from "../../services/DatabaseService/DatabaseServiceN
import PublicDataset from "../../../web/src/entity/PublicDataset";

export interface InteractionStateBranch {
aicsLoadBalancerBaseUrl: string;
applicationVersion?: string;
contextMenuIsVisible: boolean;
contextMenuItems: ContextMenuItem[];
Expand All @@ -67,6 +68,7 @@ export interface InteractionStateBranch {
}

export const initialState: InteractionStateBranch = {
aicsLoadBalancerBaseUrl: DEFAULT_CONNECTION_CONFIG.aicsLoadBalancerBaseUrl,
contextMenuIsVisible: false,
contextMenuItems: [],
// Passed to `ContextualMenu` as `target`. From the "@fluentui/react" docs:
Expand Down Expand Up @@ -166,7 +168,8 @@ export default makeReducer<InteractionStateBranch>(
}),
[INITIALIZE_APP]: (state, action) => ({
...state,
fileExplorerServiceBaseUrl: action.payload,
aicsLoadBalancerBaseUrl: action.payload.aicsLoadBalancerBaseUrl,
fileExplorerServiceBaseUrl: action.payload.fileExplorerServiceBaseUrl,
}),
[SET_VISIBLE_MODAL]: (state, action) => ({
...state,
Expand Down
26 changes: 23 additions & 3 deletions packages/core/state/interaction/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ModalType } from "../../components/Modal";
import { AICS_FMS_DATA_SOURCE_NAME } from "../../constants";

// BASIC SELECTORS
export const getAicsLoadBalancerBaseUrl = (state: State) =>
state.interaction.aicsLoadBalancerBaseUrl;
export const getContextMenuVisibility = (state: State) => state.interaction.contextMenuIsVisible;
export const getContextMenuItems = (state: State) => state.interaction.contextMenuItems;
export const getContextMenuPositionReference = (state: State) =>
Expand Down Expand Up @@ -102,14 +104,22 @@ export const getHttpFileService = createSelector(
[
getApplicationVersion,
getUserName,
getAicsLoadBalancerBaseUrl,
getFileExplorerServiceBaseUrl,
getPlatformDependentServices,
getRefreshKey,
],
(applicationVersion, userName, fileExplorerBaseUrl, platformDependentServices) =>
(
applicationVersion,
userName,
aicsLoadBalancerBaseUrl,
fileExplorerBaseUrl,
platformDependentServices
) =>
new HttpFileService({
applicationVersion,
userName,
aicsLoadBalancerBaseUrl: aicsLoadBalancerBaseUrl,
baseUrl: fileExplorerBaseUrl,
downloadService: platformDependentServices.fileDownloadService,
})
Expand Down Expand Up @@ -153,6 +163,7 @@ export const getAnnotationService = createSelector(
[
getApplicationVersion,
getUserName,
getAicsLoadBalancerBaseUrl,
getFileExplorerServiceBaseUrl,
getSelectedDataSources,
getPlatformDependentServices,
Expand All @@ -161,6 +172,7 @@ export const getAnnotationService = createSelector(
(
applicationVersion,
userName,
aicsLoadBalancerBaseUrl,
fileExplorerBaseUrl,
dataSources,
platformDependentServices
Expand All @@ -174,17 +186,25 @@ export const getAnnotationService = createSelector(
return new HttpAnnotationService({
applicationVersion,
userName,
aicsLoadBalancerBaseUrl: aicsLoadBalancerBaseUrl,
baseUrl: fileExplorerBaseUrl,
});
}
);

export const getDatasetService = createSelector(
[getApplicationVersion, getUserName, getFileExplorerServiceBaseUrl, getRefreshKey],
(applicationVersion, userName, fileExplorerBaseUrl) =>
[
getApplicationVersion,
getUserName,
getAicsLoadBalancerBaseUrl,
getFileExplorerServiceBaseUrl,
getRefreshKey,
],
(applicationVersion, userName, aicsLoadBalancerBaseUrl, fileExplorerBaseUrl) =>
new DatasetService({
applicationVersion,
userName,
aicsLoadBalancerBaseUrl: aicsLoadBalancerBaseUrl,
baseUrl: fileExplorerBaseUrl,
})
);
Expand Down
5 changes: 4 additions & 1 deletion packages/core/state/selection/test/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { DataSource } from "../../../services/DataSourceService";
describe("Selection reducer", () => {
[
selection.actions.setAnnotationHierarchy([]),
interaction.actions.initializeApp("base"),
interaction.actions.initializeApp({
fileExplorerServiceBaseUrl: "base",
aicsLoadBalancerBaseUrl: "loadBalancerBaseUrl",
}),
].forEach((expectedAction) =>
it(`clears selected file state when ${expectedAction.type} is fired`, () => {
// arrange
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/main/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*eslint no-var: "off"*/
// necessary in order to do: global.fileExplorerServiceBaseUrl = "..."
declare var fileDownloadServiceBaseUrl: string;
declare var aicsLoadBalancerBaseUrl: string;
declare var fileExplorerServiceBaseUrl: string;
Loading

0 comments on commit 31d4abf

Please sign in to comment.