mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-25 07:47:18 +00:00
89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
/*
|
|
* Copyright 2016 balena.io
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import * as _ from 'lodash';
|
|
import { Client, createClient, createNoopClient } from 'analytics-client';
|
|
import * as SentryRenderer from '@sentry/electron/renderer';
|
|
import * as settings from '../models/settings';
|
|
import { store } from '../models/store';
|
|
import * as packageJSON from '../../../../package.json';
|
|
|
|
let analyticsClient: Client;
|
|
/**
|
|
* @summary Init analytics configurations
|
|
*/
|
|
export const initAnalytics = _.once(() => {
|
|
const dsn =
|
|
settings.getSync('analyticsSentryToken') ||
|
|
_.get(packageJSON, ['analytics', 'sentry', 'token']);
|
|
SentryRenderer.init({ dsn });
|
|
|
|
const projectName =
|
|
settings.getSync('analyticsAmplitudeToken') ||
|
|
_.get(packageJSON, ['analytics', 'amplitude', 'token']);
|
|
|
|
const clientConfig = {
|
|
projectName,
|
|
endpoint: 'data.balena-staging.com',
|
|
componentName: 'etcher',
|
|
componentVersion: packageJSON.version,
|
|
};
|
|
analyticsClient = projectName
|
|
? createClient(clientConfig)
|
|
: createNoopClient();
|
|
});
|
|
|
|
function reportAnalytics(message: string, data: _.Dictionary<any> = {}) {
|
|
const { applicationSessionUuid, flashingWorkflowUuid } = store
|
|
.getState()
|
|
.toJS();
|
|
|
|
analyticsClient.track(message, {
|
|
...data,
|
|
applicationSessionUuid,
|
|
flashingWorkflowUuid,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @summary Log an event
|
|
*
|
|
* @description
|
|
* This function sends the debug message to product analytics services.
|
|
*/
|
|
export async function logEvent(message: string, data: _.Dictionary<any> = {}) {
|
|
const shouldReportAnalytics = await settings.get('errorReporting');
|
|
if (shouldReportAnalytics) {
|
|
initAnalytics();
|
|
reportAnalytics(message, data);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @summary Log an exception
|
|
*
|
|
* @description
|
|
* This function logs an exception to error reporting services.
|
|
*/
|
|
export function logException(error: any) {
|
|
const shouldReportErrors = settings.getSync('errorReporting');
|
|
if (shouldReportErrors) {
|
|
initAnalytics();
|
|
console.error(error);
|
|
SentryRenderer.captureException(error);
|
|
}
|
|
}
|