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
57 from fiftyone_pipeline_core.logger
import Logger
60 from ruamel.yaml
import YAML
62 class OfflineProcessing():
63 def run(self, data_file, evidence_yaml, logger, output):
65 Process a YAML representation of evidence - and create a YAML output containing 66 the processed evidence. 67 @param data_file: The path to the device detection data file 68 @param evidence_yaml: File containing the yaml representation of the evidence to process 69 @param logger: Logger to use within the pipeline 70 @param output: Output file to write results to 77 pipeline = DeviceDetectionPipelineBuilder(
78 data_file_path = data_file,
85 performance_profile =
"LowMemory",
97 usage_sharing =
False,
100 licence_keys =
"").add_logger(logger).
build()
104 yaml_data = yaml.load_all(evidence_yaml)
108 for evidence
in yaml_data:
110 records = records + 1
111 if (records % 100 == 0):
112 logger.log(
"info", f
"Processed {records} records")
115 print(
"---", file = output)
119 headers[f
"header.{key}"] = evidence[key]
121 self.analyseEvidence(headers, pipeline, output, yaml)
122 except BaseException
as err:
125 logger.log(
"error", err)
128 print(
"...", file = output)
130 ExampleUtils.check_data_file(pipeline, logger)
132 def analyseEvidence(self, evidence, pipeline, output, yaml):
138 data = pipeline.create_flowdata()
140 data.evidence.add_from_dict(evidence)
149 values[key] = evidence[key]
151 values[
"device.ismobile"] = device.ismobile.value()
if device.ismobile.has_value()
else "Unknown" 152 values[
"device.platformname"] = ExampleUtils.get_human_readable(device,
"platformname")
153 values[
"device.platformversion"] = ExampleUtils.get_human_readable(device,
"platformversion")
154 values[
"device.browsername"] = ExampleUtils.get_human_readable(device,
"browsername")
155 values[
"device.browserversion"] = ExampleUtils.get_human_readable(device,
"browserversion")
169 values[
"device.deviceid"] = ExampleUtils.get_human_readable(device,
"deviceid")
170 yaml.dump(values, output)
181 data_file = argv[0]
if len(argv) > 0
else ExampleUtils.find_file(LITE_DATAFILE_NAME)
184 evidence_file = argv[1]
if len(argv) > 1
else ExampleUtils.find_file(EVIDENCE_FILE_NAME)
187 output_file = argv[2]
if len(argv) > 2
else Path.joinpath(Path(evidence_file).absolute().parent,
"offline-processing-output.yml")
190 logger = Logger(min_level=
"info")
192 if (data_file !=
None):
193 with open(output_file,
"w")
as output:
194 with open(evidence_file,
"r")
as input:
195 OfflineProcessing().
run(data_file, input, logger, output)
197 f
"Processing complete. See results in: '{output_file}'")
200 "Failed to find a device detection data file. Make sure the " +
201 "device-detection-data submodule has been updated by running " +
202 "`git submodule update --recursive`.")
204 if __name__ ==
"__main__":