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
53 from pathlib
import Path
56 from fiftyone_devicedetection.devicedetection_pipelinebuilder
import DeviceDetectionPipelineBuilder
57 from fiftyone_devicedetection_examples.example_utils
import ExampleUtils
58 from fiftyone_pipeline_core.logger
import Logger
68 LITE_V_4_1_HASH =
"51Degrees-LiteV4.1.hash" 72 EVIDENCE =
"20000 Evidence Records.yml" 74 class OfflineProcessing():
75 def run(self, data_file, evidence_yaml, logger, output):
77 Process a YAML representation of evidence - and create a YAML output containing 78 the processed evidence. 79 @param data_file: The path to the device detection data file 80 @param evidence_yaml: File containing the yaml representation of the evidence to process 81 @param logger: Logger to use within the pipeline 82 @param output: Output file to write results to 89 pipeline = DeviceDetectionPipelineBuilder(
90 data_file_path = data_file,
97 performance_profile =
"LowMemory",
109 usage_sharing =
False,
112 licence_keys =
"").add_logger(logger).build()
115 yaml_data = yaml.safe_load_all(evidence_yaml)
117 for evidence
in yaml_data:
119 records = records + 1
120 if (records % 100 == 0):
121 logger.log(
"info", f
"Processed {records} records")
124 print(
"---", file = output)
128 headers[f
"header.{key}"] = evidence[key]
130 self.analyseEvidence(headers, pipeline, output)
132 print(
"...", file = output)
134 ExampleUtils.check_data_file(pipeline, logger)
136 def analyseEvidence(self, evidence, pipeline, output):
142 data = pipeline.create_flowdata()
144 data.evidence.add_from_dict(evidence)
153 values[key] = evidence[key]
155 values[
"device.ismobile"] = device.ismobile.value()
if device.ismobile.has_value()
else "Unknown" 156 values[
"device.platformname"] = ExampleUtils.get_human_readable(device,
"platformname")
157 values[
"device.platformversion"] = ExampleUtils.get_human_readable(device,
"platformversion")
158 values[
"device.browsername"] = ExampleUtils.get_human_readable(device,
"browsername")
159 values[
"device.browserversion"] = ExampleUtils.get_human_readable(device,
"browserversion")
173 values[
"device.deviceid"] = ExampleUtils.get_human_readable(device,
"deviceid")
174 yaml.dump(values, output)
179 data_file = argv[0]
if len(argv) > 0
else ExampleUtils.find_file(LITE_V_4_1_HASH)
181 evidence_file = argv[1]
if len(argv) > 1
else ExampleUtils.find_file(EVIDENCE)
184 output_file = argv[2]
if len(argv) > 2
else Path.joinpath(Path(evidence_file).absolute().parent,
"offline-processing-output.yml")
187 logger = Logger(min_level=
"info")
189 if (data_file !=
None):
190 with open(output_file,
"w")
as output:
191 with open(evidence_file,
"r")
as input:
192 OfflineProcessing().run(data_file, input, logger, output)
194 f
"Processing complete. See results in: '{output_file}'")
197 "Failed to find a device detection data file. Make sure the " +
198 "device-detection-data submodule has been updated by running " +
199 "`git submodule update --recursive`.")
201 if __name__ ==
"__main__":