The example illustrates the various metrics that can be obtained about the device detection process, for example, the degree of certainty about the result.
The example illustrates the various metrics that can be obtained about the device detection process, for example, the degree of certainty about the result. Running the example outputs those properties and values.
The example also illustrates controlling properties that are returned from the detection process - reducing the number of components required to return the properties requested reduces the overall time taken.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(path.join(__dirname, '/../../../../', requestedPackage));
}
};
const fs = require('fs');
require51('fiftyone.devicedetection.onpremise').DeviceDetectionOnPremisePipelineBuilder;
const ExampleUtils = require(path.join(__dirname,
'/../exampleUtils')).ExampleUtils;
const exampleConstants = require51('fiftyone.devicedetection.shared').exampleConstants;
const LITE_V_4_1_HASH = '51Degrees-LiteV4.1.hash';
const displayMatchMetrics = async function (pipeline, output, evidenceValues) {
const flowData = pipeline.createFlowData();
evidenceValues.forEach((value, key, map) => {
flowData.evidence.add(key, value);
});
await flowData.process();
const device = flowData.device;
output.write('--- Compare evidence with what was matched ---\n\n');
output.write('Evidence\n');
evidenceValues,
(a, b) => { return a[1].length - b[1].length; });
sortedEvidence.forEach((value, key, map) => {
output.write(`\t${key.padEnd(34)}: ${value}\n`);
});
output.write('Matches\n');
device.userAgents.value.sort((a, b) => { return b.length - a.length; })
.forEach((u) => { output.write(`\t${'Matched Chars'.padEnd(34)}: ${u}\n`); });
output.write('\n');
output.write('--- Listing all available properties, by component, by property name ---\n');
output.write(`For a discussion of what the match properties mean, see:
/documentation/4.5/_device_detection__hash.html#DeviceDetection_Hash_DataSetProduction\n`);
const availableProperties = Object.entries(device.flowElement.properties);
const componentMap = groupBy(
availableProperties.filter(p => p !== null && p[0] !== 'userAgents'),
p => p[1] == null || p[1].component == null ? 'Unknown' : p[1].component.name);
componentMap.forEach((properties, componentName, map) => {
output.write(`${componentName}\n`);
properties.forEach(property => {
const propertyName = property[0];
const propertyDesc = property[1].description;
const value = device[propertyName];
const valueText = value.hasValue ? value.value : `unknown (${value.noValueMessage})`;
output.write(`\t${propertyName.padEnd(24)}: ${valueText}\n`);
output.write(`\t\t${propertyDesc}\n`);
});
output.write('\n');
});
};
const groupBy = function (list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
};
const findDataFile = function (dataFile, output) {
if (dataFile === null) {
dataFile = LITE_V_4_1_HASH;
output.write(`No filename specified. Using default '${dataFile}'\n`);
}
if (path.isAbsolute(dataFile) === false) {
}
if (dataFile === null || typeof dataFile === 'undefined' || fs.existsSync(dataFile) === false) {
output.write(`Failed to find a device detection data file. If using the default 'lite'
data file, make sure that git lfs is installed and that the device-detection-data
submodule has been updated by running 'git submodule update --recursive'.\n`);
throw new Error(`Data file '${dataFile}' not found`);
}
return dataFile;
};
const run = async function (dataFile, output, evidenceList) {
dataFile = findDataFile(dataFile, output);
dataFile,
performanceProfile: 'LowMemory',
shareUsage: false,
autoUpdate: false,
updateOnStart: false,
fileSystemWatcher: false,
restrictedProperties: ['ismobile', 'hardwarename', 'userAgents', 'deviceID', 'difference', 'method', 'matchedNodes', 'drift', 'iterations'],
updateMatchedUserAgent: true
}).build();
pipeline.on('error', console.error);
for (const evidenceValues of evidenceList) {
await displayMatchMetrics(pipeline, output, evidenceValues);
}
};
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const dataFile = args.length > 0 ? args[0] : null;
run(dataFile, process.stdout, exampleConstants.defaultEvidenceValues.slice(2, 3));
};
module.exports = {
run
};
Definition deviceDetectionOnPremisePipelineBuilder.js:35
Definition fiftyone.devicedetection.onpremise/examples/onpremise/exampleUtils.js:36