Provides an example of processing a YAML file containing evidence for device detection.There are 20,000 examples in the supplied file of evidence representing HTTP Headers. For example:
We create a device detection pipeline to read the data and find out about the associated device, we write this data to a YAML formatted output stream.
As well as explaining the basic operation of off line processing using the defaults, for advanced operation this example can be used to experiment with tuning device detection for performance and predictive power using Performance Profile, Graph and Difference and Drift settings.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
const LineReader = require('n-readlines');
const path = require('path');
require((process.env.directory || __dirname) +
'/../../../deviceDetectionOnPremisePipelineBuilder');
const DataExtension = require(
'fiftyone.devicedetection.shared').dataExtension;
const LITE_V_4_1_HASH = '51Degrees-LiteV4.1.hash';
const EVIDENCE = '20000 Evidence Records.yml';
const fs = require('fs');
const yaml = require('js-yaml');
const analyzeEvidence = async function (evidence, pipeline, outputFile, outputFunc) {
const data = pipeline.createFlowData();
const document = {};
for (const [key, value] of Object.entries(evidence)) {
data.evidence.add(key, value.toString());
document[key] = value;
}
await data.process();
const device = data.device;
document[
'device.ismobile'] =
DataExtension.
getValueHelper(device,
'ismobile');
document[
'device.platformname'] =
DataExtension.getValueHelper(device,
'platformname');
document[
'device.platformversion'] =
DataExtension.getValueHelper(device,
'platformversion');
document[
'device.browsername'] =
DataExtension.getValueHelper(device,
'browsername');
document[
'device.browserversion'] =
DataExtension.getValueHelper(device,
'browserversion');
document[
'device.deviceID'] =
DataExtension.getValueHelper(device,
'deviceID');
outputFunc(outputFile, yaml.dump(document));
};
const run = async function (dataFile, evidenceFile, outputFile, outputFunc) {
dataFile: dataFile,
performanceProfile: 'LowMemory',
shareUsage: false,
autoUpdate: false,
updateOnStart: false,
fileSystemWatcher: false
}).build();
const liner = new LineReader(evidenceFile);
let buffer, utf8Line, evidence;
let records = 0;
let document = '';
while ((buffer = liner.next())) {
utf8Line = buffer.toString('utf8').trim();
if ((utf8Line.match(/^---/) || utf8Line.match(/^\.\.\./)) && document) {
records++;
if (records % 1000 === 0) {
console.log(`Processed ${records} records`);
}
outputFunc(outputFile, '---\n');
evidence = yaml.load(document);
await analyzeEvidence(evidence, pipeline, outputFile, outputFunc);
if (utf8Line.match(/^\.\.\./)) {
break;
}
document = `${utf8Line}\n`;
} else {
document += `${utf8Line}\n`;
}
}
outputFunc(outputFile, '...\n');
console.log(`Processing complete. See results in: '${outputFile}'`);
};
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const dataFile = args.length > 0 ? args[0] :
ExampleUtils.
findFile(LITE_V_4_1_HASH);
const evidenceFile = args.length > 1 ? args[1] :
ExampleUtils.findFile(EVIDENCE);
const outputFile = args.length > 2
? args[2]
: path.join(path.dirname(path.resolve(evidenceFile)), 'offline-processing-output.yml');
if (dataFile !== undefined) {
fs.writeFileSync(outputFile, '');
run(
dataFile,
evidenceFile,
outputFile,
(output, content) => { fs.appendFileSync(output, content); });
} else {
console.error('Failed to find a device detection ' +
'data file. Make sure the device-detection-data ' +
'submodule has been updated by running ' +
'`git submodule update --recursive`.');
}
};
module.exports = {
run: run
};