Skip to content

Commit

Permalink
add duration to model.download and recipe.pull telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Feb 1, 2024
1 parent b36fff8 commit 853e2ee
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ describe('pullApplication', () => {
Running: true,
},
});
vi.spyOn(utils, 'getDurationSecondsSince').mockReturnValue(99);
await manager.pullApplication(recipe, model);
const gitCloneOptions = {
repository: 'repo',
Expand All @@ -222,10 +223,14 @@ describe('pullApplication', () => {
}
expect(doDownloadModelWrapperSpy).toHaveBeenCalledOnce();
expect(mocks.builImageMock).toHaveBeenCalledOnce();
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.download', { 'model.id': 'model1' });
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.download', {
'model.id': 'model1',
durationSeconds: 99,
});
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(2, 'recipe.pull', {
'recipe.id': 'recipe1',
'recipe.name': 'Recipe 1',
durationSeconds: 99,
});
});
test('pullApplication should not clone repository if folder already exists locally', async () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type { ModelInfo } from '@shared/src/models/IModelInfo';
import type { ModelsManager } from './modelsManager';
import { getPortsInfo } from '../utils/ports';
import { goarch } from '../utils/arch';
import { isEndpointAlive, timeout } from '../utils/utils';
import { getDurationSecondsSince, isEndpointAlive, timeout } from '../utils/utils';

export const CONFIG_FILENAME = 'ai-studio.yaml';

Expand Down Expand Up @@ -70,6 +70,7 @@ export class ApplicationManager {
) {}

async pullApplication(recipe: Recipe, model: ModelInfo) {
const startTime = performance.now();
// Create a TaskUtils object to help us
const taskUtil = new RecipeStatusUtils(recipe.id, this.recipeStatusRegistry);

Expand Down Expand Up @@ -101,7 +102,8 @@ export class ApplicationManager {
const podInfo = await this.createApplicationPod(images, modelPath, taskUtil);

await this.runApplication(podInfo, taskUtil);
this.telemetry.logUsage('recipe.pull', { 'recipe.id': recipe.id, 'recipe.name': recipe.name });
const durationSeconds = getDurationSecondsSince(startTime);
this.telemetry.logUsage('recipe.pull', { 'recipe.id': recipe.id, 'recipe.name': recipe.name, durationSeconds });
}

async runApplication(podInfo: PodInfo, taskUtil: RecipeStatusUtils) {
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import { RecipeStatusUtils } from '../utils/recipeStatusUtils';
import type { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import * as utils from '../utils/utils';

const mocks = vi.hoisted(() => {
return {
Expand Down Expand Up @@ -356,6 +357,7 @@ describe('downloadModel', () => {
.mockImplementation((_modelId: string, _url: string, _taskUtil: RecipeStatusUtils, _destFileName?: string) => {
return Promise.resolve('');
});
vi.spyOn(utils, 'getDurationSecondsSince').mockReturnValue(99);
await manager.downloadModel(
{
id: 'id',
Expand All @@ -373,7 +375,7 @@ describe('downloadModel', () => {
},
state: 'loading',
});
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.download', { 'model.id': 'id' });
expect(mocks.logUsageMock).toHaveBeenNthCalledWith(1, 'model.download', { 'model.id': 'id', durationSeconds: 99 });
});
test('retrieve model path if already on disk', async () => {
vi.spyOn(manager, 'isModelOnDisk').mockReturnValue(true);
Expand Down
5 changes: 4 additions & 1 deletion packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import * as podmanDesktopApi from '@podman-desktop/api';
import type { RecipeStatusUtils } from '../utils/recipeStatusUtils';
import { getDurationSecondsSince } from '../utils/utils';

export type DownloadModelResult = DownloadModelSuccessfulResult | DownloadModelFailureResult;

Expand Down Expand Up @@ -180,8 +181,10 @@ export class ModelsManager {
});

try {
const startTime = performance.now();
const result = await this.doDownloadModelWrapper(model.id, model.url, taskUtil);
this.telemetry.logUsage('model.download', { 'model.id': model.id });
const durationSeconds = getDurationSecondsSince(startTime);
this.telemetry.logUsage('model.download', { 'model.id': model.id, durationSeconds });
return result;
} catch (e) {
console.error(e);
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ export async function isEndpointAlive(endPoint: string): Promise<boolean> {
});
});
}

export function getDurationSecondsSince(startTimeMs: number) {
return Math.round((performance.now() - startTimeMs) / 1000);
}

0 comments on commit 853e2ee

Please sign in to comment.