Skip to content

Commit

Permalink
Merge pull request #365 from metrico/fix/datasource_detection
Browse files Browse the repository at this point in the history
fix: datasource Self-Provisioning #363
  • Loading branch information
jacovinus authored Nov 6, 2023
2 parents aa583eb + 3c91805 commit f0fd41a
Showing 1 changed file with 75 additions and 39 deletions.
114 changes: 75 additions & 39 deletions src/views/Main/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { getDsHeaders } from "../../components/QueryBuilder/Operations/helpers";
import setDataSources from "../DataSources/store/setDataSources";
import { setShowDataSourceSetting } from "./setShowDataSourceSetting";


// updateDataSources:

export function updateDataSourcesWithUrl(
Expand Down Expand Up @@ -156,74 +155,111 @@ export async function checkLocalAPI(
opts.auth = auth;
}

return new Promise(async (resolve, rej) => {
return new Promise(async (resolve) => {
try {
let res = await getReadyResponse(url, opts, response);

response = res;
} catch (e: any) {
rej(false);
resolve(false);
} finally {
if (
response &&
response?.status === 200 &&
(response?.contentType.includes("application/json") ||
response?.contentLength === "0")
) {
if (response && response?.status === 200) {
resolve(true);
} else {
rej(false);
resolve(false);
}
}
});
}

export async function updateDataSourcesFromLocalUrl(
dataSources: any,
dispatch: Function,
navigate: Function
) {
const location = window.location.origin;
const logsDs = dataSources.find((f: any) => f.type === "logs");
const isBasicAuth = logsDs?.auth?.basicAuth?.value;
let auth = { username: "", password: "" };
let basicAuthFields = logsDs?.auth?.fields?.basicAuth;
// provision the basic auth fields data
export function basicAuthChecker(auth: any) {
let authParams = { username: "", password: "" };

const isBasicAuth = auth?.basicAuth?.value;

const basicAuthFields = auth?.fields?.basicAuth;

const isBasicAuthFields = basicAuthFields?.length > 0;

if (isBasicAuth && isBasicAuthFields) {
for (let field of basicAuthFields) {
if (field?.name === "user") {
auth.username = field?.value || "";
authParams.username = field?.value || "";
}
if (field?.name === "password") {
auth.password = field?.value || "";
authParams.password = field?.value || "";
}
}
}

return {
isBasicAuth,
auth: authParams,
};
}

export function setLocalDataSources(datasources: any) {
// we could check datasources when typed in here
localStorage.setItem("dataSources", JSON.stringify(datasources));
}

export function updateDataSourcesUrl(cb: Function, prevData: any, url: any) {
// 1- take datasources
const dsCP = [...prevData];

// 2 - copy as previous
const prevDs = JSON.parse(JSON.stringify(dsCP));

// 3- update datasources value with new source
const newDs = prevDs?.map((m: any) => ({
...m,
url,
}));

// update localstorage datasources
setLocalDataSources(newDs);
// update datasources at store
cb(setDataSources(newDs));
}

export async function updateDataSourcesFromLocalUrl(
dataSources: any,
dispatch: Function,
navigate: Function
) {
// current location
const location = window.location.origin;

let dsReady = false;

let isLocalReady = false;
const logsDs = dataSources.find((f: any) => f.type === "logs");

if (logsDs?.url !== "") {
const {
isBasicAuth, // check if it has a basic auth
auth, // provisions auth params
} = basicAuthChecker(logsDs?.auth);

if (logsDs?.url === "") {
dsReady = await checkLocalAPI(logsDs.url, logsDs, auth, isBasicAuth); // add the auth in here



if (dsReady) {
updateDataSourcesUrl(dispatch, dataSources, location);
} else {
navigate("datasources");
}
} else {
let dsReady = await checkLocalAPI(
logsDs.url,
logsDs,
auth,
isBasicAuth
); // add the auth in here

if (!dsReady) {
isLocalReady = await checkLocalAPI(location, logsDs);

if (isLocalReady && !dsReady) {
const dsCP = [...dataSources];
const prevDs = JSON.parse(JSON.stringify(dsCP));

const newDs = prevDs?.map((m: any) => ({
...m,
url: location,
}));
localStorage.setItem("dataSources", JSON.stringify(newDs));
dispatch(setDataSources(newDs));
} else if (!dsReady && !isLocalReady) {
navigate("datasources");
}
navigate("datasources");
}
}
}

0 comments on commit f0fd41a

Please sign in to comment.