Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into ui5-2.0-FF_template-u…
Browse files Browse the repository at this point in the history
…pdate

* origin/main:
  chore: apply latest changesets
  feat(fiori-gen): add telemetry helper class and associated functions (#2489)
  • Loading branch information
devinea committed Oct 25, 2024
2 parents d55e890 + f23e147 commit 49e34f2
Show file tree
Hide file tree
Showing 31 changed files with 528 additions and 25 deletions.
7 changes: 7 additions & 0 deletions examples/simple-generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/generator-simple-fe

## 1.0.64

### Patch Changes

- @sap-ux/fiori-elements-writer@1.3.11
- @sap-ux/fiori-freestyle-writer@1.2.8

## 1.0.63

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ux/generator-simple-fe",
"version": "1.0.63",
"version": "1.0.64",
"description": "Simple example of a yeoman generator for Fiori elements.",
"license": "Apache-2.0",
"private": true,
Expand Down
7 changes: 7 additions & 0 deletions packages/abap-deploy-config-inquirer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/abap-deploy-config-inquirer

## 1.0.8

### Patch Changes

- Updated dependencies [231e713]
- @sap-ux/fiori-generator-shared@0.7.0

## 1.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/abap-deploy-config-inquirer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "https://github.com/SAP/open-ux-tools.git",
"directory": "packages/abap-deploy-config-inquirer"
},
"version": "1.0.7",
"version": "1.0.8",
"license": "Apache-2.0",
"main": "dist/index.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cap-config-writer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/cap-config-writer

## 0.7.50

### Patch Changes

- Updated dependencies [231e713]
- @sap-ux/fiori-generator-shared@0.7.0

## 0.7.49

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cap-config-writer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap-ux/cap-config-writer",
"description": "Add or update configuration for SAP CAP projects",
"version": "0.7.49",
"version": "0.7.50",
"repository": {
"type": "git",
"url": "https://github.com/SAP/open-ux-tools.git",
Expand Down
7 changes: 7 additions & 0 deletions packages/create/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/create

## 0.8.52

### Patch Changes

- @sap-ux/abap-deploy-config-inquirer@1.0.8
- @sap-ux/cap-config-writer@0.7.50

## 0.8.51

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/create/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap-ux/create",
"description": "SAP Fiori tools module to add or remove features",
"version": "0.8.51",
"version": "0.8.52",
"repository": {
"type": "git",
"url": "https://github.com/SAP/open-ux-tools.git",
Expand Down
7 changes: 7 additions & 0 deletions packages/fiori-elements-writer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/fiori-elements-writer

## 1.3.11

### Patch Changes

- Updated dependencies [231e713]
- @sap-ux/fiori-generator-shared@0.7.0

## 1.3.10

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fiori-elements-writer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap-ux/fiori-elements-writer",
"description": "SAP Fiori elements application writer",
"version": "1.3.10",
"version": "1.3.11",
"repository": {
"type": "git",
"url": "https://github.com/SAP/open-ux-tools.git",
Expand Down
7 changes: 7 additions & 0 deletions packages/fiori-freestyle-writer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @sap-ux/fiori-freestyle-writer

## 1.2.8

### Patch Changes

- Updated dependencies [231e713]
- @sap-ux/fiori-generator-shared@0.7.0

## 1.2.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fiori-freestyle-writer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap-ux/fiori-freestyle-writer",
"description": "SAP Fiori freestyle application writer",
"version": "1.2.7",
"version": "1.2.8",
"repository": {
"type": "git",
"url": "https://github.com/SAP/open-ux-tools.git",
Expand Down
6 changes: 6 additions & 0 deletions packages/fiori-generator-shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sap-ux/fiori-generator-shared

## 0.7.0

### Minor Changes

- 231e713: adds new functions

## 0.6.4

### Patch Changes
Expand Down
8 changes: 6 additions & 2 deletions packages/fiori-generator-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sap-ux/fiori-generator-shared",
"description": "Commonly used shared functionality and types to support the fiori generator.",
"version": "0.6.4",
"version": "0.7.0",
"repository": {
"type": "git",
"url": "https://github.com/SAP/open-ux-tools.git",
Expand Down Expand Up @@ -30,15 +30,19 @@
"dependencies": {
"@sap-ux/btp-utils": "workspace:*",
"@sap-ux/project-access": "workspace:*",
"@sap-ux/telemetry": "workspace:*",
"@vscode-logging/logger": "2.0.0",
"i18next": "20.6.1",
"logform": "2.4.0",
"mem-fs": "2.1.0",
"mem-fs-editor": "9.4.0"
"mem-fs-editor": "9.4.0",
"os-name": "4.0.1",
"semver": "7.5.4"
},
"devDependencies": {
"@types/mem-fs-editor": "7.0.1",
"@types/mem-fs": "1.1.2",
"@types/semver": "7.5.2",
"@types/yeoman-environment": "2.10.11"
},
"engines": {
Expand Down
15 changes: 15 additions & 0 deletions packages/fiori-generator-shared/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { isAppStudio } from '@sap-ux/btp-utils';
import { hostEnvironment } from './types';

/**
* Determine if the current prompting environment is cli .
*
Expand All @@ -10,3 +13,15 @@ export function isCli(): boolean {
return false;
}
}

/**
* Determine if the current prompting environment is cli or a hosted extension (app studio or vscode).
*
* @returns the platform name and technical name
*/
export function getHostEnvironment(): { name: string; technical: string } {
if (isCli()) {
return hostEnvironment.cli;
}
return isAppStudio() ? hostEnvironment.bas : hostEnvironment.vscode;
}
9 changes: 6 additions & 3 deletions packages/fiori-generator-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export * from './cap';
export * from './environment';
export * from './logWrapper';
export * from './system-utils';
export * from './telemetry';
export { getPackageScripts } from './getPackageScripts';
export { getBootstrapResourceUrls, getDefaultTargetFolder } from './helpers';
export { generateReadMe } from './read-me';
export * from './system-utils';
export { PackageJsonScripts, YeomanEnvironment, VSCodeInstance } from './types';
export * from './logWrapper';
export { getHostEnvironment } from './environment';
export { isExtensionInstalled } from './installedCheck';
export { PackageJsonScripts, YeomanEnvironment, VSCodeInstance, hostEnvironment } from './types';
22 changes: 22 additions & 0 deletions packages/fiori-generator-shared/src/installedCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { coerce, lt } from 'semver';

/**
* Check for an installed extension, optionally specifying a minimum version.
* Note, this does not check for activation state of specified extension.
*
* @param vscode - vscode instance
* @param extensionId - the id of the extension to find
* @param minVersion - the minimum version of the specified extension, lower versions will not be returned. Must be a valid SemVer string.
* @returns true if the extension is installed and the version is >= minVersion (if provided), false otherwise
*/
export function isExtensionInstalled(vscode: any, extensionId: string, minVersion?: string): boolean {
const foundExt = vscode?.extensions?.getExtension(extensionId);
if (foundExt) {
const extVersion = coerce(foundExt.packageJSON.version);
if (extVersion) {
// Check installed ver is >= minVersion or return true if minVersion is not specified
return !(minVersion && lt(extVersion, minVersion));
}
}
return false;
}
2 changes: 2 additions & 0 deletions packages/fiori-generator-shared/src/telemetry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { sendTelemetry, sendTelemetryBlocking } from './utils';
export * from './telemetryHelper';
103 changes: 103 additions & 0 deletions packages/fiori-generator-shared/src/telemetry/telemetryHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
PerformanceMeasurementAPI as Performance,
initTelemetrySettings,
type TelemetryProperties,
type ToolsSuiteTelemetryInitSettings
} from '@sap-ux/telemetry';
import type { TelemetryData } from './types';
import { getHostEnvironment } from '../environment';
import osName from 'os-name';
import { t } from '../i18n';

/**
* Helper class for intialising and preparing event data for telemetry.
*/
export abstract class TelemetryHelper {
private static _telemetryData: TelemetryData;
private static _previousEventTimestamp: number;

/**
* Returns the telemetry data.
*
* @returns telemetry data
*/
public static get telemetryData(): TelemetryData {
return this._telemetryData;
}

/**
* Load telemetry settings.
*
* @param options - tools suite telemetry init settings
*/
public static async initTelemetrySettings(options: ToolsSuiteTelemetryInitSettings): Promise<void> {
await initTelemetrySettings(options);
}

/**
* Creates telemetry data and adds default telemetry props.
*
* @param additionalData - set additional properties to be reported by telemetry
* @param filterDups - filters duplicates by returning undefined if it's suspected to be a repeated event based on previous telemetry data & timestamp (1 second)
* @returns telemetry data
*/
public static createTelemetryData<T extends TelemetryProperties>(
additionalData?: Partial<T>,
filterDups = false
): TelemetryData | undefined {
const currentTimestamp = new Date().getTime();
if (!this._previousEventTimestamp) {
filterDups = false; // can't filter duplicates if no previous event timestamp
this._previousEventTimestamp = currentTimestamp;
}

if (!this._telemetryData) {
let osVersionName = t('telemetry.unknownOs');
try {
osVersionName = osName();
} catch {
// no matched os name, possible beta or unreleased version
}
this._telemetryData = {
Platform: getHostEnvironment().technical,
OperatingSystem: osVersionName
};
}

if (filterDups) {
const newTelemData = { ...this._telemetryData, ...additionalData };
if (
Math.abs(this._previousEventTimestamp - currentTimestamp) < 1000 &&
JSON.stringify(newTelemData) === JSON.stringify(this._telemetryData)
) {
return undefined;
}
}
this._previousEventTimestamp = currentTimestamp;
this._telemetryData = Object.assign(this._telemetryData, additionalData);

return this._telemetryData;
}

/**
* Marks the start time. Example usage:
* At the start of of the writing phase of the yeoman generator.
* It should not be updated everytime calling createTelemetryData().
*/
public static markAppGenStartTime(): void {
TelemetryHelper.createTelemetryData({
markName: Performance.startMark('LOADING_TIME')
});
}

/**
* Marks the end time. Example usage:
* At the end of the writing phase of yeoman generator.
*/
public static markAppGenEndTime(): void {
if (this._telemetryData?.markName) {
Performance.endMark(this._telemetryData.markName);
Performance.measure(this._telemetryData.markName);
}
}
}
3 changes: 3 additions & 0 deletions packages/fiori-generator-shared/src/telemetry/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface TelemetryData {
[key: string]: string;
}
Loading

0 comments on commit 49e34f2

Please sign in to comment.