@awsui/components-react
Version:
AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A
98 lines (97 loc) • 3.39 kB
JavaScript
import { THEME, PACKAGE_VERSION } from './environment';
var oneTimeMetrics = {};
var buildMetricHash = function (_a) {
var source = _a.source, action = _a.action;
return ["src" + source, "action" + action].join('_');
};
var getMajorVersion = function (versionString) {
var majorVersionMatch = versionString.match(/^(\d+\.\d+)/);
return majorVersionMatch ? majorVersionMatch[1] : '';
};
var formatMajorVersionForMetricDetail = function (version) {
return version.replace(/\s/g, '');
};
var formatVersionForMetricName = function (theme, version) {
return "" + theme.charAt(0) + getMajorVersion(version).replace('.', '');
};
var buildMetricDetail = function (_a) {
var source = _a.source, action = _a.action, version = _a.version;
var detailObject = {
s: source,
t: THEME,
a: action,
f: framework,
v: formatMajorVersionForMetricDetail(version)
};
return JSON.stringify(detailObject);
};
var buildMetricName = function (_a) {
var source = _a.source, action = _a.action, version = _a.version;
return ['awsui', source, "" + formatVersionForMetricName(THEME, version)].join('_');
};
var framework = 'react';
function setFramework(fwk) {
framework = fwk;
}
export var Metrics = {
initMetrics: function (fwk) {
setFramework(fwk);
},
sendMetric: function (metricName, value, detail) {
if (!metricName || !/^[a-zA-Z0-9_-]{1,32}$/.test(metricName)) {
console.error("Invalid metric name: " + metricName);
return;
}
if (detail && detail.length > 200) {
console.error("Detail for metric " + metricName + " is too long: " + detail);
return;
}
if (typeof window !== 'undefined') {
var win = window;
if (typeof win.AWSC === 'object' &&
typeof win.AWSC.Clog === 'object' &&
typeof win.AWSC.Clog.log === 'function') {
win.AWSC.Clog.log(metricName, value, detail);
}
}
},
sendMetricObject: function (metric, value) {
this.sendMetric(buildMetricName(metric), value, buildMetricDetail(metric));
},
sendMetricObjectOnce: function (metric, value) {
var metricHash = buildMetricHash(metric);
if (!oneTimeMetrics[metricHash]) {
this.sendMetricObject(metric, value);
oneTimeMetrics[metricHash] = true;
}
},
sendMetricOnce: function (metricName, value, detail) {
if (!oneTimeMetrics[metricName]) {
this.sendMetric(metricName, value, detail);
oneTimeMetrics[metricName] = true;
}
},
logComponentLoaded: function () {
this.sendMetricObjectOnce({
source: 'components',
action: 'loaded',
version: PACKAGE_VERSION
}, 1);
},
logComponentUsed: function (componentName) {
this.sendMetricObjectOnce({
source: componentName,
action: 'used',
version: PACKAGE_VERSION
}, 1);
}
};
export var MetricsTestHelper = {
resetOneTimeMetricsCache: function () {
for (var prop in oneTimeMetrics) {
delete oneTimeMetrics[prop];
}
},
formatMajorVersionForMetricDetail: formatMajorVersionForMetricDetail,
formatVersionForMetricName: formatVersionForMetricName
};