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
using FiftyOne.Pipeline.Core.FlowElements;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
{
public class Program
{
public class Example : ExampleBase
{
string dataFile,
TextReader evidenceYaml,
ILoggerFactory loggerFactory,
TextWriter output)
{
var logger = loggerFactory.CreateLogger<Program>();
using (var pipeline = new DeviceDetectionPipelineBuilder(loggerFactory)
.UseOnPremise(dataFile, null, false)
.SetPerformanceProfile(PerformanceProfiles.LowMemory)
.SetShareUsage(false)
.SetAutoUpdate(false)
.SetDataUpdateOnStartUp(false)
.SetDataFileSystemWatcher(false)
.Build())
{
var serializer = new Serializer();
foreach (var evidence
in GetEvidence(evidenceYaml, logger))
{
output.WriteLine("---");
AnalyseEvidence(evidence, pipeline, output, serializer);
}
output.WriteLine("...");
ExampleUtils.CheckDataFile(pipeline, loggerFactory.CreateLogger<Program>());
}
}
private void AnalyseEvidence(
Dictionary<string, object> evidence,
IPipeline pipeline,
TextWriter writer,
Serializer serializer)
{
using (var data = pipeline.CreateFlowData())
{
data.AddEvidence(evidence);
data.Process();
var device = data.Get<IDeviceData>();
Dictionary<string, string> output = new Dictionary<string, string>();
foreach(var entry in evidence)
{
output.Add(entry.Key, entry.Value.ToString());
}
output.Add("device.ismobile", device.IsMobile.HasValue ?
device.IsMobile.Value.ToString() : "Unknown");
output.Add("device.platformname", device.PlatformName.GetHumanReadable());
output.Add("device.platformversion", device.PlatformVersion.GetHumanReadable());
output.Add("device.browsername", device.BrowserName.GetHumanReadable());
output.Add("device.browserversion", device.BrowserVersion.GetHumanReadable());
output.Add("device.deviceid", device.DeviceId.GetHumanReadable());
serializer.Serialize(writer, output);
}
}
}
static void Main(string[] args)
{
var dataFile = args.Length > 0 ? args[0] :
ExampleUtils.FindFile(Constants.LITE_HASH_DATA_FILE_NAME);
var evidenceFile = args.Length > 1 ? args[1] :
ExampleUtils.FindFile(Constants.YAML_EVIDENCE_FILE_NAME);
var outputFile = args.Length > 2 ? args[2] :
Path.Combine(Path.GetDirectoryName(evidenceFile), "offline-processing-output.yml");
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
var logger = loggerFactory.CreateLogger<Program>();
if (dataFile != null)
{
using (var writer = new StreamWriter(File.OpenWrite(outputFile)))
using (var reader = new StreamReader(File.OpenRead(evidenceFile)))
{
new Example().Run(dataFile, reader, loggerFactory, writer);
}
logger.LogInformation($"Processing complete. See results in: '{outputFile}'");
}
else
{
logger.LogError("Failed to find a device detection data file. Make sure the " +
"device-detection-data submodule has been updated by running " +
"`git submodule update --recursive`.");
}
loggerFactory.Dispose();
}
}
}