Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create pod with sample app and model services after build is completed (#90) #100

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
526 changes: 503 additions & 23 deletions packages/backend/src/managers/applicationManager.spec.ts

Large diffs are not rendered by default.

453 changes: 341 additions & 112 deletions packages/backend/src/managers/applicationManager.ts

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ beforeEach(() => {
vi.resetAllMocks();
});

const dirent = [
{
isDirectory: () => true,
path: '/home/user/appstudio-dir',
name: 'model-id-1',
},
{
isDirectory: () => true,
path: '/home/user/appstudio-dir',
name: 'model-id-2',
},
{
isDirectory: () => false,
path: '/home/user/appstudio-dir',
name: 'other-file-should-be-ignored.txt',
},
] as fs.Dirent[];

function mockFiles(now: Date) {
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
const existsSyncSpy = vi.spyOn(fs, 'existsSync');
Expand All @@ -36,23 +54,7 @@ function mockFiles(now: Date) {
const base = path.basename(dir);
return [base + '-model'];
} else {
return [
{
isDirectory: () => true,
path: '/home/user/appstudio-dir',
name: 'model-id-1',
},
{
isDirectory: () => true,
path: '/home/user/appstudio-dir',
name: 'model-id-2',
},
{
isDirectory: () => false,
path: '/home/user/appstudio-dir',
name: 'other-file-should-be-ignored.txt',
},
] as fs.Dirent[];
return dirent;
}
});
}
Expand All @@ -68,12 +70,14 @@ test('getLocalModelsFromDisk should get models in local directory', () => {
file: 'model-id-1-model',
size: 32000,
creation: now,
path: path.resolve(dirent[0].path, dirent[0].name, 'model-id-1-model'),
},
{
id: 'model-id-2',
file: 'model-id-2-model',
size: 32000,
creation: now,
path: path.resolve(dirent[1].path, dirent[1].name, 'model-id-2-model'),
},
]);
});
Expand Down Expand Up @@ -133,6 +137,7 @@ test('loadLocalModels should post a message with the message on disk and on cata
file: 'model-id-1-model',
id: 'model-id-1',
size: 32000,
path: path.resolve(dirent[0].path, dirent[0].name, 'model-id-1-model'),
},
id: 'model-id-1',
},
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ export class ModelsManager {
continue;
}
const modelFile = modelEntries[0];
const info = fs.statSync(path.resolve(d.path, d.name, modelFile));
const fullPath = path.resolve(d.path, d.name, modelFile);
const info = fs.statSync(fullPath);
result.set(d.name, {
id: d.name,
file: modelFile,
path: fullPath,
size: info.size,
creation: info.mtime,
});
Expand Down
5 changes: 5 additions & 0 deletions packages/backend/src/models/AIConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface AIConfig {
};
}

export interface AIConfigFile {
aiConfig: AIConfig;
path: string;
}

export function isString(value: unknown): value is string {
return (!!value && typeof value === 'string') || value instanceof String;
}
Expand Down
75 changes: 75 additions & 0 deletions packages/backend/src/utils/ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,78 @@ export function isFreePort(port: number): Promise<boolean> {
.listen(port, '127.0.0.1'),
);
}

export async function getPortsInfo(portDescriptor: string): Promise<string | undefined> {
// check if portDescriptor is a range of ports
if (portDescriptor.includes('-')) {
return await getPortRange(portDescriptor);
} else {
const localPort = await getPort(portDescriptor);
if (!localPort) {
return undefined;
}
return `${localPort}`;
}
}

/**
* return a range of the same length as portDescriptor containing free ports
* undefined if the portDescriptor range is not valid
* e.g 5000:5001 -> 9000:9001
*/
async function getPortRange(portDescriptor: string): Promise<string | undefined> {
const rangeValues = getStartEndRange(portDescriptor);
if (!rangeValues) {
return Promise.resolve(undefined);
}

const rangeSize = rangeValues.endRange + 1 - rangeValues.startRange;
try {
// if free port range fails, return undefined
return await getFreePortRange(rangeSize);
} catch (e) {
console.error(e);
return undefined;
}
}

async function getPort(portDescriptor: string): Promise<number | undefined> {
let port: number;
if (portDescriptor.endsWith('/tcp') || portDescriptor.endsWith('/udp')) {
port = parseInt(portDescriptor.substring(0, portDescriptor.length - 4));
} else {
port = parseInt(portDescriptor);
}
// invalid port
if (isNaN(port)) {
return Promise.resolve(undefined);
}
try {
// if getFreePort fails, it returns undefined
return await getFreePort(port);
} catch (e) {
console.error(e);
return undefined;
}
}

function getStartEndRange(range: string) {
if (range.endsWith('/tcp') || range.endsWith('/udp')) {
range = range.substring(0, range.length - 4);
}

const rangeValues = range.split('-');
if (rangeValues.length !== 2) {
return undefined;
}
const startRange = parseInt(rangeValues[0]);
const endRange = parseInt(rangeValues[1]);

if (isNaN(startRange) || isNaN(endRange)) {
return undefined;
}
return {
startRange,
endRange,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test('Expect simple column styling', async () => {
file: 'file',
creation: d,
size: 1000,
path: 'path',
},
};
render(ModelColumnCreation, { object });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test('Expect simple column styling', async () => {
file: 'file',
creation: new Date(),
size: 1000,
path: 'path',
},
};
render(ModelColumnSize, { object });
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/models/ILocalModelInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface LocalModelInfo {
id: string;
file: string;
path: string;
size: number;
creation: Date;
}
Loading