-
Notifications
You must be signed in to change notification settings - Fork 534
/
getStandardData.ts
67 lines (62 loc) · 1.95 KB
/
getStandardData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as version from '../version';
import { v4 as uuidv4 } from 'uuid';
import * as os from 'os';
import * as crypto from 'crypto';
import { isCI } from '../is-ci';
import {
getIntegrationName,
getIntegrationVersion,
getIntegrationEnvironment,
getIntegrationEnvironmentVersion,
} from './sources';
import { StandardAnalyticsData } from './types';
import { MetricsCollector } from '../metrics';
import * as createDebug from 'debug';
import { ArgsOptions } from '../../cli/args';
const osName = require('os-name');
const debug = createDebug('snyk');
const START_TIME = Date.now();
function getMetrics(durationMs: number): any[] | undefined {
try {
const networkTime = MetricsCollector.NETWORK_TIME.getTotal();
const cpuTime = durationMs - networkTime;
MetricsCollector.CPU_TIME.createInstance().setValue(cpuTime);
return MetricsCollector.getAllMetrics();
} catch (err) {
debug('Error with metrics', err);
}
}
export async function getStandardData(
args: ArgsOptions[],
): Promise<StandardAnalyticsData> {
const isStandalone = version.isStandaloneBuild();
const snykVersion = version.getVersion();
const seed = uuidv4();
const shasum = crypto.createHash('sha1');
const durationMs = Date.now() - START_TIME;
const metrics = getMetrics(durationMs);
let osNameString;
try {
osNameString = osName(os.platform(), os.release());
} catch (e) {
osNameString = 'unknown';
}
const data = {
os: osNameString,
osPlatform: os.platform(),
osRelease: os.release(),
osArch: os.arch(),
version: snykVersion,
nodeVersion: process.version,
standalone: isStandalone,
integrationName: getIntegrationName(args),
integrationVersion: getIntegrationVersion(args),
integrationEnvironment: getIntegrationEnvironment(args),
integrationEnvironmentVersion: getIntegrationEnvironmentVersion(args),
id: shasum.update(seed).digest('hex'),
ci: isCI(),
durationMs,
metrics,
};
return data;
}