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/telemetry transport update with beacon #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/WebTelemetryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
WebTelemetryTransport,
KVDataItem,
} from './types';
import { WebTelemetryTransportDebug, WebTelemetryTransportDefault } from './WebTelemetryTransport';
import { WebTelemetryTransportDefault } from './transport/WebTelemetryTransport';
import { globalSessionId } from './constants';
import { stringifyCircularObj } from './helpers';
import { WebTelemetryTransportDebug } from './transport/WebTelemetryTransportDebug';

/**
* Базовая имплементация класса для работы с телеметрией
Expand Down
31 changes: 0 additions & 31 deletions src/WebTelemetryTransport.ts

This file was deleted.

8 changes: 7 additions & 1 deletion src/presets/WebTelemetryResources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SALUTE_EYE_URL } from '../transport/WebTelemetryTransport';
import { WebTelemetryResourcesConfig, WebTelemetryTransport } from '../types';
import { WebTelemetryBase } from '../WebTelemetryBase';

Expand Down Expand Up @@ -81,7 +82,12 @@ export class WebTelemetryResources extends WebTelemetryBase<WebTelemetryResource
* на отправку данных в бекенд. Если этого не сделать, то шедулер будет
* бесконечно планировать отправку данных после любой отправки данных
*/
if (res.name && this.config.endpoint && res.name.includes(this.config.endpoint)) {
if (
res.name &&
(this.config.endpoint || SALUTE_EYE_URL) &&
[this.config.endpoint, SALUTE_EYE_URL].some((url) => res.name.includes(url))
) {
//
// eslint-disable-next-line no-continue
continue;
}
Expand Down
40 changes: 40 additions & 0 deletions src/transport/WebTelemetryTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface WebTelemetryTransport {
send(body: string): void;
}

export const SALUTE_EYE_URL = 'https://cbdv.sberdevices.ru/amplitude/telemetry/';
const SALUTE_EYE_API_KEY = '68bfaed5a9b7c036c54cb6a217df1b84';

function getLastPartAfterSlash(url: string): string {
const parts = url.split('/');
return parts[parts.length - 1];
}

export class WebTelemetryTransportDefault implements WebTelemetryTransport {
private url: string;

constructor(url: string) {
this.url = url;
}

send(body: string) {
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': SALUTE_EYE_API_KEY,
};

if (navigator.sendBeacon) {
navigator.sendBeacon(this.url, body);
const blob = new Blob([body], { type: headers['Content-Type'] });

navigator.sendBeacon(SALUTE_EYE_URL + getLastPartAfterSlash(this.url), blob);
} else {
fetch(this.url, { body, method: 'POST' });
fetch(SALUTE_EYE_URL + getLastPartAfterSlash(this.url), {
method: 'POST',
headers: headers,
body: body,
});
}
}
}
13 changes: 13 additions & 0 deletions src/transport/WebTelemetryTransportDebug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WebTelemetryTransport } from '../types';

export class WebTelemetryTransportDebug implements WebTelemetryTransport {
send(body: string) {
try {
// eslint-disable-next-line no-console
console.debug('WebTelemetry sent:\n', JSON.parse(body));
} catch {
// eslint-disable-next-line no-console
console.debug(`WebTelemetry sent:\n${body}`);
}
}
}
Loading