Skip to content

Commit

Permalink
feat: add node type columns
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector committed Dec 1, 2023
1 parent d0bbe5d commit 6ea0e28
Show file tree
Hide file tree
Showing 22 changed files with 550 additions and 458 deletions.
10 changes: 5 additions & 5 deletions packages/secretnote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"dependencies": {
"@antv/s2": "^1.52.0",
"@codemirror/lang-python": "^6.1.2",
"@difizen/libro-jupyter": "0.0.2-alpha.0",
"@difizen/mana-app": "0.0.2-alpha.1",
"@difizen/mana-common": "0.0.2-alpha.1",
"@difizen/mana-l10n": "0.0.2-alpha.1",
"@difizen/mana-react": "0.0.2-alpha.1",
"@difizen/libro-jupyter": "^0.1.0",
"@difizen/mana-app": "^0.1.2",
"@difizen/mana-common": "^0.1.2",
"@difizen/mana-l10n": "^0.1.2",
"@difizen/mana-react": "^0.1.2",
"@kanaries/web-data-loader": "^0.1.7",
"@lumino/polling": "^1.11.4",
"@rjsf/antd": "^5.7.3",
Expand Down
5 changes: 4 additions & 1 deletion packages/secretnote/src/lang/bundle.l10n.en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,8 @@
"密码": "Password",
"请输入密码": "Password is required",
"数据库": "Database",
"请输入数据库名称": "Database is required"
"请输入数据库名称": "Database is required",
"类型": "Type",
"请选择节点类型": "Please select a node type",
"通用": "Common"
}
5 changes: 4 additions & 1 deletion packages/secretnote/src/lang/bundle.l10n.zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,8 @@
"密码": "密码",
"请输入密码": "请输入密码",
"数据库": "数据库",
"请输入数据库名称": "请输入数据库名称"
"请输入数据库名称": "请输入数据库名称",
"类型": "类型",
"请选择节点类型": "请选择节点类型",
"通用": "通用"
}
14 changes: 8 additions & 6 deletions packages/secretnote/src/modules/file/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
font-size: 12px;

.ant-tree-treenode {
&::before {
top: -3px !important;
}

&:hover::before {
background-color: var(--mana-secretnote-li-hover-background) !important;
}

.ant-tree-switcher {
margin-top: 2px;
display: flex;
align-items: center;
justify-content: center;

&.ant-tree-switcher_close {
transform: rotate(-90deg);
}
}
}

Expand All @@ -26,7 +28,7 @@
display: flex;
align-items: center;
justify-content: space-between;
padding-right: 4px;
padding: 1px 4px 1px 1px;
color: var(--mana-secretnote-text-color);

.ant-dropdown-trigger {
Expand Down
22 changes: 11 additions & 11 deletions packages/secretnote/src/modules/file/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inject, prop, singleton } from '@difizen/mana-app';
import type { DataNode } from 'antd/es/tree';

import { downloadFileByUrl as download, ERROR_CODE } from '@/utils';
import { getServerUrl } from '@/utils';

import type { IServer } from '../server';
import { SecretNoteServerManager } from '../server';
Expand Down Expand Up @@ -43,7 +44,7 @@ export class FileService {

try {
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: this.getBaseUrl(server),
baseUrl: getServerUrl(server).baseUrl,
content: true,
});

Expand All @@ -63,7 +64,8 @@ export class FileService {
serverNode.children = sortedFileNodeList;
fileTree.push(serverNode);
} catch (err) {
// pass
// eslint-disable-next-line no-console
console.log(err);
}
}

Expand All @@ -79,7 +81,7 @@ export class FileService {
return false;
}
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: this.getBaseUrl(server),
baseUrl: getServerUrl(server).baseUrl,
content: true,
});

Expand All @@ -92,7 +94,7 @@ export class FileService {
if (!server) {
return ERROR_CODE.SERVER_NOT_FOUND;
}
const baseUrl = this.getBaseUrl(server);
const baseUrl = getServerUrl(server).baseUrl;
const path = `${BASE_PATH}/${name}`;
await this.contentsManager.save(path, {
content,
Expand All @@ -111,7 +113,7 @@ export class FileService {
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
const data = await this.contentsManager.getDownloadUrl(path, {
baseUrl: this.getBaseUrl(server),
baseUrl: getServerUrl(server).baseUrl,
});
download(data, nodeData.title as string);
}
Expand All @@ -124,7 +126,9 @@ export class FileService {
const { serverId, path } = this.parseNodeKey(nodeData.key as string);
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
await this.contentsManager.delete(path, { baseUrl: this.getBaseUrl(server) });
await this.contentsManager.delete(path, {
baseUrl: getServerUrl(server).baseUrl,
});
await this.getFileTree();
}
}
Expand All @@ -134,7 +138,7 @@ export class FileService {
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
const data = await this.contentsManager.get(decodedPath, {
baseUrl: this.getBaseUrl(server),
baseUrl: getServerUrl(server).baseUrl,
content: true,
});
return data;
Expand Down Expand Up @@ -180,10 +184,6 @@ export class FileService {
return FILE_EXTS.includes(`.${ext}`);
}

private getBaseUrl(server: IServer) {
return this.serverManager.getServerSettings(server).baseUrl;
}

private onServerChanged() {
this.getFileTree();
}
Expand Down
12 changes: 5 additions & 7 deletions packages/secretnote/src/modules/kernel/kernel-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { inject, singleton, StorageService } from '@difizen/mana-app';

import { SecretNoteServerManager } from '@/modules/server';
import type { IServer } from '@/modules/server';
import { getServerUrl } from '@/utils';

interface StoredSessionInfo {
sessionId: string;
Expand Down Expand Up @@ -72,7 +73,7 @@ export class SecretNoteKernelManager {
if (isAlive) {
connection = await this.connectToKernel({
...options,
serverSettings: this.serverManager.getServerSettings(s),
serverSettings: getServerUrl(s),
});
} else {
await this.removeStoredSession(fileInfo, hit);
Expand Down Expand Up @@ -168,7 +169,7 @@ export class SecretNoteKernelManager {
path: fileInfo.path,
type: fileInfo.type,
} as ISessionOptions,
this.serverManager.getServerSettings(server),
getServerUrl(server),
);

if (!newSession || !newSession.kernel) {
Expand All @@ -190,7 +191,7 @@ export class SecretNoteKernelManager {

const kernelConnection = await this.connectToKernel({
...options,
serverSettings: this.serverManager.getServerSettings(server),
serverSettings: getServerUrl(server),
});

return kernelConnection;
Expand All @@ -203,10 +204,7 @@ export class SecretNoteKernelManager {

protected async isKernelAlive(id: string, server: IServer): Promise<boolean> {
try {
const data = await this.kernelRestAPI.getKernelModel(
id,
this.serverManager.getServerSettings(server),
);
const data = await this.kernelRestAPI.getKernelModel(id, getServerUrl(server));
return !!data;
} catch {
return false;
Expand Down
90 changes: 37 additions & 53 deletions packages/secretnote/src/modules/metrics/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Poll } from '@lumino/polling';
import { SecretNoteKernelManager } from '@/modules/kernel';
import { SecretNoteServerManager } from '@/modules/server';
import type { IServer } from '@/modules/server';
import { RequestService } from '@/utils';

import { NotebookFileService } from '../notebook';

Expand Down Expand Up @@ -44,6 +45,7 @@ export class MetricsService {
protected readonly kernelManager: SecretNoteKernelManager;
protected readonly serverConnection: ServerConnection;
protected readonly notebookFileService: NotebookFileService;
protected readonly requestService: RequestService;

@prop()
metrics: MetricsItem[] = [];
Expand All @@ -56,11 +58,13 @@ export class MetricsService {
@inject(SecretNoteKernelManager) kernelManager: SecretNoteKernelManager,
@inject(ServerConnection) serverConnection: ServerConnection,
@inject(NotebookFileService) notebookFileService: NotebookFileService,
@inject(RequestService) requestService: RequestService,
) {
this.serverManager = serverManager;
this.kernelManager = kernelManager;
this.serverConnection = serverConnection;
this.notebookFileService = notebookFileService;
this.requestService = requestService;
this.notebookFileService.onNotebookFileChanged(() => {
this.refresh();
});
Expand All @@ -69,7 +73,7 @@ export class MetricsService {
auto: false,
factory: () => this.getMetrics(),
frequency: {
interval: 2 * 1000,
interval: 4 * 1000,
backoff: true,
max: 300 * 1000,
},
Expand Down Expand Up @@ -171,22 +175,20 @@ export class MetricsService {
}

async getServerStatus(server: IServer): Promise<ServerStatus> {
const settings = this.getSettings(server);
const url = this.getUrl(settings.baseUrl, '/api/metrics/v1');
const init = { method: 'GET' };
try {
const response = await this.serverConnection.makeRequest(url, init, settings);
if (response.status === 200) {
const data = await response.json();
return {
cpu: data.cpu_percent,
memory: data.rss,
cpuText: `${data.cpu_percent} %`,
memoryText: this.humanFileSize(data.rss),
};
}
const url = '/api/metrics/v1';
const init = { method: 'GET' };
const data = await this.requestService.request(url, init, server);

return {
cpu: data.cpu_percent,
memory: data.rss,
cpuText: `${data.cpu_percent} %`,
memoryText: this.humanFileSize(data.rss),
};
} catch (e) {
// pass
// eslint-disable-next-line no-console
console.log(e);
}
return {
cpu: 0,
Expand All @@ -203,36 +205,29 @@ export class MetricsService {
const { model, status } = kernelConnection;
const { id, name } = model;
const options = server.kernelspec ? Object.keys(server.kernelspec.kernelspecs) : [];
const init = { method: 'GET' };
const settings = this.getSettings(server);
const url = this.getUrl(
settings.baseUrl,
'/api/metrics/v1/kernel_usage/get_usage',
id,
);
try {
const response = await this.serverConnection.makeRequest(url, init, settings);
if (response.status === 200) {
const data = await response.json();
const { cpu, memory, pid, cpuText, memoryText } = this.parseKernelStatus(data);
const { color, text_zh } = kernelStatus[status];
const url = '/api/metrics/v1/kernel_usage/get_usage/' + id;
const init = { method: 'GET' };
const data = await this.requestService.request(url, init, server);
const { cpu, memory, pid, cpuText, memoryText } = this.parseKernelStatus(data);
const { color, text_zh } = kernelStatus[status];

return {
id,
name,
pid,
status,
statusText: text_zh,
statusColor: color,
cpu,
memory,
options,
cpuText,
memoryText,
};
}
return {
id,
name,
pid,
status,
statusText: text_zh,
statusColor: color,
cpu,
memory,
options,
cpuText,
memoryText,
};
} catch (e) {
// pass
// eslint-disable-next-line no-console
console.log(e);
}
return {
id,
Expand All @@ -249,17 +244,6 @@ export class MetricsService {
};
}

private getUrl(baseUrl: string, url: string, ...args: string[]) {
const parts = args.map((path) => URL.encodeParts(path));
return URL.join(baseUrl, url, ...parts);
}

private getSettings(server: IServer) {
const settings = { ...this.serverConnection.settings };
settings.baseUrl = this.serverManager.getServerSettings(server).baseUrl;
return settings;
}

private humanFileSize(size: number) {
const i = Math.floor(Math.log(size) / Math.log(1024));
return (
Expand Down
1 change: 0 additions & 1 deletion packages/secretnote/src/modules/node/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

.secretnote-add-node {
width: 400px;
height: 145px;

label {
color: var(--mana-secretnote-text-color) !important;
Expand Down
Loading

0 comments on commit 6ea0e28

Please sign in to comment.