forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetry.ts
More file actions
105 lines (92 loc) · 3.18 KB
/
Telemetry.ts
File metadata and controls
105 lines (92 loc) · 3.18 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fs from 'fs';
import * as path from 'path';
import { cloneDeep } from 'lodash';
import { RushConfiguration } from '../api/RushConfiguration';
import { Rush } from '../api/Rush';
import { FileSystem } from '@rushstack/node-core-library';
export interface ITelemetryData {
name: string;
duration: number;
result: string;
timestamp?: number;
platform?: string;
rushVersion?: string;
extraData?: { [key: string]: string };
}
const MAX_FILE_COUNT: number = 100;
export class Telemetry {
private _enabled: boolean;
private _store: ITelemetryData[];
private _dataFolder: string;
private _rushConfiguration: RushConfiguration;
public constructor(rushConfiguration: RushConfiguration) {
this._rushConfiguration = rushConfiguration;
this._enabled = this._rushConfiguration.telemetryEnabled;
this._store = [];
const folderName: string = 'telemetry';
this._dataFolder = path.join(this._rushConfiguration.commonTempFolder, folderName);
}
public log(telemetryData: ITelemetryData): void {
if (!this._enabled) {
return;
}
const data: ITelemetryData = cloneDeep(telemetryData);
data.timestamp = data.timestamp || new Date().getTime();
data.platform = data.platform || process.platform;
data.rushVersion = data.rushVersion || Rush.version;
this._store.push(data);
}
public flush(writeFile: (file: string, data: string) => void = FileSystem.writeFile): void {
if (!this._enabled || this._store.length === 0) {
return;
}
const fullPath: string = this._getFilePath();
FileSystem.ensureFolder(this._dataFolder);
writeFile(fullPath, JSON.stringify(this._store));
this._store = [];
this._cleanUp();
}
public get store(): ITelemetryData[] {
return this._store;
}
/**
* When there are too many log files, delete the old ones.
*/
private _cleanUp(): void {
if (FileSystem.exists(this._dataFolder)) {
const files: string[] = FileSystem.readFolder(this._dataFolder);
if (files.length > MAX_FILE_COUNT) {
const sortedFiles: string[] = files.map(fileName => {
const filePath: string = path.join(this._dataFolder, fileName);
const stats: fs.Stats = FileSystem.getStatistics(filePath);
return {
filePath: filePath,
modifiedTime: stats.mtime.getTime(),
isFile: stats.isFile()
};
})
.filter(value => {
// Only delete files
return value.isFile;
})
.sort((a, b) => {
return a.modifiedTime - b.modifiedTime;
})
.map(s => {
return s.filePath;
});
const filesToDelete: number = sortedFiles.length - MAX_FILE_COUNT;
for (let i: number = 0; i < filesToDelete; i++) {
FileSystem.deleteFile(sortedFiles[i]);
}
}
}
}
private _getFilePath(): string {
let fileName: string = `telemetry_${new Date().toISOString()}`;
fileName = fileName.replace(/[\-\:\.]/g, '_') + '.json';
return path.join(this._dataFolder, fileName);
}
}