Provides an example of processing a YAML file containing evidence for device detection.
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.
const LineReader = require('n-readlines');
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(path.join(__dirname, '/../../../../', requestedPackage));
}
};
require51('fiftyone.devicedetection.onpremise').DeviceDetectionOnPremisePipelineBuilder;
const ExampleUtils = require(path.join(__dirname,
'/../exampleUtils')).ExampleUtils;
const DataExtension = require51(
'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)) {
if (value === null) {
data.evidence.add(key, '');
} else {
data.evidence.add(key, value.toString());
}
document[key] = value;
}
await data.process();
const device = data.device;
outputFunc(outputFile, yaml.dump(document));
};
const run = async function (dataFile, evidenceFile, outputFile, outputFunc) {
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
};
Definition dataExtension.js:23
static getValueHelper(elementData, propertyName)
Helper function to read property values from flowData.
Definition dataExtension.js:31
Definition deviceDetectionOnPremisePipelineBuilder.js:35
Definition fiftyone.devicedetection.onpremise/examples/onpremise/exampleUtils.js:36