Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Implement activity tracking for k8s
Browse files Browse the repository at this point in the history
Signed-off-by: Vladyslav Zhukovskyi <[email protected]>
  • Loading branch information
vzhukovs committed Nov 2, 2021
1 parent a02e225 commit cf11b14
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export const HttpService = Symbol('HttpService');

export interface HttpService {
get(url: string): Promise<string | undefined>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
post(url: string, data?: any): Promise<string | undefined>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class CheServerHttpServiceImpl implements HttpService {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async post(uri: string, data?: any): Promise<string | undefined> {
throw new Error('httpsService.post() not supported');
}

/**
* Use proxy and/or certificates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ export class K8SHttpServiceImpl implements HttpService {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async post(uri: string, data?: any): Promise<string | undefined> {
const axiosInstance = await this.getAxiosInstance(uri);
try {
const response = await axiosInstance.post(uri, data, {
transformResponse: [responseData => responseData],
responseType: 'text',
});
return response.data;
} catch (error) {
// not found then we return undefined
if (error.response && error.response.status === 404) {
return undefined;
}
throw error;
}
}

/**
* Use proxy and/or certificates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { inject, injectable } from 'inversify';

import { DevfileService } from '@eclipse-che/theia-remote-api/lib/common/devfile-service';
import { EndpointService } from '@eclipse-che/theia-remote-api/lib/common/endpoint-service';
import { HttpService } from '@eclipse-che/theia-remote-api/lib/common/http-service';
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';

@injectable()
Expand All @@ -27,6 +29,12 @@ export class K8sWorkspaceServiceImpl implements WorkspaceService {
@inject(K8sDevWorkspaceEnvVariables)
private env: K8sDevWorkspaceEnvVariables;

@inject(EndpointService)
private endpointService: EndpointService;

@inject(HttpService)
private httpService: HttpService;

public async getCurrentNamespace(): Promise<string> {
return this.env.getWorkspaceNamespace();
}
Expand Down Expand Up @@ -62,7 +70,15 @@ export class K8sWorkspaceServiceImpl implements WorkspaceService {
}

public async updateWorkspaceActivity(): Promise<void> {
throw new Error('workspaceService.updateWorkspaceActivity() not supported');
const endpoints = await this.endpointService.getEndpointsByType('collocated-terminal');
const machineExecEndpoint = endpoints.find(endpoint => endpoint.name === 'terminal');

if (machineExecEndpoint === undefined || machineExecEndpoint.targetPort === undefined) {
throw new Error('Endpoint for machine-exec did not found.');
}

const requestUrl = `http://127.0.0.1:${machineExecEndpoint.targetPort}/activity/tick`;
await this.httpService.post(requestUrl);
}

public async stop(): Promise<void> {
Expand Down

0 comments on commit cf11b14

Please sign in to comment.