-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathanalytics-settings-service.ts
93 lines (75 loc) · 3.23 KB
/
analytics-settings-service.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
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
import { createGUID } from "../common/helpers";
import { exported } from "../common/decorators";
class AnalyticsSettingsService implements IAnalyticsSettingsService {
private static SESSIONS_STARTED_KEY_PREFIX = "SESSIONS_STARTED_";
constructor(private $userSettingsService: UserSettings.IUserSettingsService,
private $staticConfig: IStaticConfig,
private $hostInfo: IHostInfo,
private $osInfo: IOsInfo,
private $logger: ILogger) { }
public async canDoRequest(): Promise<boolean> {
return true;
}
public getUserId(): Promise<string> {
return this.getSettingValueOrDefault("USER_ID");
}
@exported("analyticsSettingsService")
public getClientId(): Promise<string> {
return this.getSettingValueOrDefault(this.$staticConfig.ANALYTICS_INSTALLATION_ID_SETTING_NAME);
}
public getClientName(): string {
return "" + this.$staticConfig.CLIENT_NAME_ALIAS.cyan.bold;
}
public getPrivacyPolicyLink(): string {
return "http://www.telerik.com/company/privacy-policy";
}
public async getUserSessionsCount(projectName: string): Promise<number> {
const sessionsCountForProject = await this.$userSettingsService.getSettingValue<number>(this.getSessionsProjectKey(projectName));
return sessionsCountForProject || 0;
}
public async setUserSessionsCount(count: number, projectName: string): Promise<void> {
return this.$userSettingsService.saveSetting<number>(this.getSessionsProjectKey(projectName), count);
}
@exported("analyticsSettingsService")
public getUserAgentString(identifier: string): string {
let osString = "";
const osRelease = this.$osInfo.release();
if (this.$hostInfo.isWindows) {
osString = `Windows NT ${osRelease}`;
} else if (this.$hostInfo.isDarwin) {
osString = `Macintosh`;
const macRelease = this.getMacOSReleaseVersion(osRelease);
if (macRelease) {
osString += `; Intel Mac OS X ${macRelease}`;
}
} else {
osString = `Linux x86`;
if (this.$osInfo.arch() === "x64") {
osString += "_64";
}
}
const userAgent = `${identifier} (${osString}; ${this.$osInfo.arch()})`;
return userAgent;
}
private getMacOSReleaseVersion(osRelease: string): string {
// https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
// Each macOS version is labeled 10.<version>, where it looks like <versions> is taken from the major version returned by os.release() (16.x.x for example) and subtracting 4 from it.
// So the version becomes "10.12" in this case.
// Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result.
const majorVersion = osRelease && _.first(osRelease.split("."));
return majorVersion && `10.${+majorVersion - 4}`;
}
private getSessionsProjectKey(projectName: string): string {
return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`;
}
private async getSettingValueOrDefault(settingName: string): Promise<string> {
let guid = await this.$userSettingsService.getSettingValue<string>(settingName);
if (!guid) {
guid = createGUID(false);
this.$logger.trace(`Setting new ${settingName}: ${guid}.`);
await this.$userSettingsService.saveSetting<string>(settingName, guid);
}
return guid;
}
}
$injector.register("analyticsSettingsService", AnalyticsSettingsService);