Skip to content

Commit

Permalink
on my way
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Dec 5, 2024
1 parent 40f59dc commit 813780f
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 48 deletions.
17 changes: 17 additions & 0 deletions web/src/core/adapters/s3Client/s3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,23 @@ export function createS3Client(
);

return downloadUrl;
},

headObject: async ({ path }) => {
const { bucketName, objectName } = bucketNameAndObjectNameFromS3Path(path);

const { getAwsS3Client } = await prApi;

const { awsS3Client } = await getAwsS3Client();

const head = await awsS3Client.send(
new (await import("@aws-sdk/client-s3")).HeadObjectCommand({
Bucket: bucketName,
Key: objectName
})
);

return { contentType: head.ContentType, metadata: head.Metadata };
}

// "getPresignedUploadUrl": async ({ path, validityDurationSecond }) => {
Expand Down
5 changes: 5 additions & 0 deletions web/src/core/ports/S3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export type S3Client = {
validityDurationSecond: number;
}) => Promise<string>;

headObject: (params: { path: string }) => Promise<{
contentType: string | undefined;
metadata: Record<string, string> | undefined;
}>;

// getPresignedUploadUrl: (params: {
// path: string;
// validityDurationSecond: number;
Expand Down
1 change: 1 addition & 0 deletions web/src/core/usecases/dataExplorer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type State = {
rows: any[];
rowCount: number | undefined;
fileDownloadUrl: string;
// fileType: "parquet" | "csv" | "json";
}
| undefined;
};
Expand Down
120 changes: 89 additions & 31 deletions web/src/core/usecases/dataExplorer/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@ import { assert } from "tsafe/assert";
import * as s3ConfigManagement from "core/usecases/s3ConfigManagement";

const privateThunks = {
getFileDonwloadUrl:
(params: { sourceUrl: string }) =>
async (...args) => {
const [dispatch, , { oidc }] = args;

const { sourceUrl } = params;

const fileDownloadUrl = await (async () => {
if (sourceUrl.startsWith("https://")) {
return sourceUrl;
}

const s3path = sourceUrl.replace(/^s3:\/\//, "/");
assert(s3path !== sourceUrl, "Unsupported protocol");

if (!oidc.isUserLoggedIn) {
oidc.login({ doesCurrentHrefRequiresAuth: true });
await new Promise(() => {});
}

const s3Client = (
await dispatch(
s3ConfigManagement.protectedThunks.getS3ConfigAndClientForExplorer()
)
)?.s3Client;

if (s3Client === undefined) {
alert("No S3 client available");
await new Promise<never>(() => {});
assert(false);
}

return s3Client.getFileDownloadUrl({
path: s3path,
validityDurationSecond: 3600 * 6
});
})();

return fileDownloadUrl;
},
performQuery:
(params: {
queryParams: {
Expand All @@ -28,7 +68,7 @@ const privateThunks = {

dispatch(actions.queryStarted({ queryParams }));

const { sqlOlap, oidc } = rootContext;
const { sqlOlap } = rootContext;

const getIsActive = () => same(getState()[name].queryParams, queryParams);

Expand All @@ -49,36 +89,11 @@ const privateThunks = {
};
}

const fileDownloadUrl = await (async () => {
if (sourceUrl.startsWith("https://")) {
return sourceUrl;
}

const s3path = sourceUrl.replace(/^s3:\/\//, "/");
assert(s3path !== sourceUrl, "Unsupported protocol");

if (!oidc.isUserLoggedIn) {
oidc.login({ doesCurrentHrefRequiresAuth: true });
await new Promise(() => {});
}

const s3Client = (
await dispatch(
s3ConfigManagement.protectedThunks.getS3ConfigAndClientForExplorer()
)
)?.s3Client;

if (s3Client === undefined) {
alert("No S3 client available");
await new Promise<never>(() => {});
assert(false);
}

return s3Client.getFileDownloadUrl({
path: s3path,
validityDurationSecond: 3600 * 6
});
})();
const fileDownloadUrl = await dispatch(
privateThunks.getFileDonwloadUrl({
sourceUrl
})
);

const rowCountOrErrorMessage = await sqlOlap
.getRowCount(sourceUrl)
Expand Down Expand Up @@ -132,6 +147,49 @@ const privateThunks = {
fileDownloadUrl
})
);
},
detectFileType:
(params: { sourceUrl: string }) =>
async (...args) => {
const { sourceUrl } = params;
const [dispatch] = args;

const extension = (() => {
const validExtensions = ["parquet", "csv", "json"] as const;
type ValidExtension = (typeof validExtensions)[number];

const isValidExtension = (ext: string): ext is ValidExtension =>
validExtensions.includes(ext as ValidExtension);

let pathname: string;

try {
pathname = new URL(sourceUrl).pathname;
} catch {
return undefined;
}
const match = pathname.match(/\.(\w+)$/);

if (match === null) {
return undefined;
}

const [, extension] = match;

return isValidExtension(extension) ? extension : undefined;
})();

if (extension) {
return extension;
}

const contentType = await (async () => {
const fileDownloadUrl = await dispatch(
privateThunks.getFileDonwloadUrl({
sourceUrl
})
);
})();
}
} satisfies Thunks;

Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/de.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@ Fühlen Sie sich frei, Ihre Kubernetes-Bereitstellungen zu erkunden und die Kont
"resize table": undefined
},
UrlInput: {
load: "Laden"
load: "Laden",
reset: "Leeren"
},
CommandBar: {
ok: "Ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/en.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,8 @@ Feel free to explore and take charge of your Kubernetes deployments!
"resize table": "Resize"
},
UrlInput: {
load: "Load"
load: "Load",
reset: "Reset"
},
CommandBar: {
ok: "Ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/es.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ export const translations: Translations<"en"> = {
"resize table": "Redimensionar"
},
UrlInput: {
load: "Cargar"
load: "Cargar",
reset: "Vaciar"
},
CommandBar: {
ok: "Aceptar"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/fi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,8 @@ Tutustu vapaasti ja ota hallintaan Kubernetes-julkaisusi!
"resize table": "Muuta taulukon kokoa"
},
UrlInput: {
load: "Lataa"
load: "Lataa",
reset: "Tyhjennä"
},
CommandBar: {
ok: "ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/fr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,8 @@ N'hésitez pas à explorer et à prendre en main vos déploiements Kubernetes !
"resize table": "Redimensionner"
},
UrlInput: {
load: "Charger"
load: "Charger",
reset: "Vider"
},
CommandBar: {
ok: "ok"
Expand Down
8 changes: 5 additions & 3 deletions web/src/ui/i18n/resources/it.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ export const translations: Translations<"it"> = {
la nostra documentazione
</MuiLink>
. &nbsp;
<MuiLink {...accountTabLink}>Configurare il tuo Vault CLI locale</MuiLink>
.
<MuiLink {...accountTabLink}>
Configurare il tuo Vault CLI locale
</MuiLink>.
</>
)
},
Expand Down Expand Up @@ -967,7 +968,8 @@ Sentiti libero di esplorare e prendere il controllo dei tuoi deployment Kubernet
"resize table": "Ridimensiona"
},
UrlInput: {
load: "Carica"
load: "Carica",
reset: "Svuotare"
},
CommandBar: {
ok: "ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/nl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@ Voel je vrij om te verkennen en de controle over je Kubernetes-implementaties te
"resize table": "Formaat wijzigen"
},
UrlInput: {
load: "Laden"
load: "Laden",
reset: "Leegmaken"
},
CommandBar: {
ok: "ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/no.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,8 @@ Utforsk gjerne og ta kontroll over tjenestene du kjører på Kubernetes!
"resize table": "Endre størrelse"
},
UrlInput: {
load: "Last"
load: "Last",
reset: "Tøm"
},
CommandBar: {
ok: "ok"
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui/i18n/resources/zh-CN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,8 @@ ${
"resize table": "调整大小"
},
UrlInput: {
load: "加载"
load: "加载",
reset: "清空"
},
CommandBar: {
ok: "是"
Expand Down
4 changes: 2 additions & 2 deletions web/src/ui/pages/dataExplorer/DataExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { routes } from "ui/routes";
import { useCore, useCoreState } from "core";
import { Alert } from "onyxia-ui/Alert";
import { CircularProgress } from "onyxia-ui/CircularProgress";
import { assert, type Equals } from "tsafe/assert";
import { assert } from "tsafe/assert";
import { UrlInput } from "./UrlInput";
import { PageHeader } from "onyxia-ui/PageHeader";
import { getIconUrlByName } from "lazy-icons";
Expand Down Expand Up @@ -198,7 +198,7 @@ export default function DataExplorer(props: Props) {
disableColumnMenu
loading={isQuerying}
paginationMode="server"
rowCount={rowCount ?? 999999999}
rowCount={rowCount}
pageSizeOptions={(() => {
const pageSizeOptions = [25, 50, 100];

Expand Down
13 changes: 9 additions & 4 deletions web/src/ui/pages/dataExplorer/UrlInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tss } from "tss";
import { getIconUrlByName } from "lazy-icons";
import { SearchBar } from "onyxia-ui/SearchBar";
import { useEffectOnValueChange } from "powerhooks/useEffectOnValueChange";
import { declareComponentKeys } from "ui/i18n";
import { declareComponentKeys, useTranslation } from "ui/i18n";

type Props = {
className?: string;
Expand All @@ -16,6 +16,7 @@ type Props = {
export function UrlInput(props: Props) {
const { className, url, onUrlChange, getIsValidUrl } = props;

const { t } = useTranslation({ UrlInput });
const [urlBeingTyped, setUrlBeingTyped] = useState(url);

const isLoadable = urlBeingTyped !== url && getIsValidUrl(urlBeingTyped);
Expand Down Expand Up @@ -51,10 +52,14 @@ export function UrlInput(props: Props) {
</div>
<Button
className={classes.loadButton}
startIcon={getIconUrlByName("CloudDownload")}
startIcon={
urlBeingTyped === ""
? getIconUrlByName("Clear")
: getIconUrlByName("CloudDownload")
}
onClick={onButtonClick}
>
Load
{urlBeingTyped === "" ? t("reset") : t("load")}
</Button>
</div>
);
Expand All @@ -76,5 +81,5 @@ const useStyles = tss
}
}));

const { i18n } = declareComponentKeys<"load">()({ UrlInput });
const { i18n } = declareComponentKeys<"load" | "reset">()({ UrlInput });
export type I18n = typeof i18n;
1 change: 1 addition & 0 deletions web/src/ui/pages/myFiles/MyFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function MyFiles(props: Props) {
);

const onOpenFile = useConstCallback<ExplorerProps["onOpenFile"]>(({ basename }) => {
//TODO use dataExplorer thunk
if (basename.endsWith(".parquet") || basename.endsWith(".csv")) {
const { path } = route.params;

Expand Down

0 comments on commit 813780f

Please sign in to comment.