Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2139 from snyk/test/analytics
Migrate Tap tests for Analytics to Jest
- Loading branch information
Showing
12 changed files
with
988 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as version from '../version'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import * as os from 'os'; | ||
const osName = require('os-name'); | ||
import * as crypto from 'crypto'; | ||
import { isCI } from '../is-ci'; | ||
import { | ||
getIntegrationName, | ||
getIntegrationVersion, | ||
getIntegrationEnvironment, | ||
getIntegrationEnvironmentVersion, | ||
getCommandVersion, | ||
} from './sources'; | ||
|
||
import { StandardAnalyticsData } from './types'; | ||
import { MetricsCollector } from '../metrics'; | ||
import * as createDebug from 'debug'; | ||
import { ArgsOptions } from '../../cli/args'; | ||
|
||
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 = await version.getVersion(); | ||
const seed = uuidv4(); | ||
const shasum = crypto.createHash('sha1'); | ||
const environment = isStandalone | ||
? {} | ||
: { | ||
npmVersion: await getCommandVersion('npm'), | ||
}; | ||
const durationMs = Date.now() - START_TIME; | ||
|
||
const metrics = getMetrics(durationMs); | ||
|
||
const data = { | ||
os: osName(os.platform(), os.release()), | ||
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(), | ||
environment, | ||
durationMs, | ||
metrics, | ||
}; | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type Environment = { | ||
npmVersion?: string | undefined; | ||
}; | ||
|
||
export type StandardAnalyticsData = { | ||
version: string; | ||
os: string; | ||
nodeVersion: string; | ||
standalone: boolean; | ||
integrationName: string; | ||
integrationVersion: string; | ||
integrationEnvironment: string; | ||
integrationEnvironmentVersion: string; | ||
id: string; | ||
ci: boolean; | ||
environment: Environment; | ||
durationMs: number; | ||
metrics: any[] | undefined; | ||
}; |
Oops, something went wrong.