Tutorial: How to build an InfluxDB query using Flux?
Frederik Van Leeckwyck
on , updated
An advanced use case for the new Flux query language
Process historians are used to gather high-resolution sensor data from industrial controllers like PLCs and SCADA systems. Once the data is collected, it is available for further processing and query building. This blog post describes the use of Flux, a new language for querying InfluxDB, the time series database behind Factry Historian.
It is recommended for advanced readers with profound technical knowledge.
Ever since it was first announced in June 2018, we have been eagerly following the development of the Flux query language. We started experimenting with Flux’s capabilities pretty early on and are now confident that it has matured enough for it to be really useful in a production or manufacturing context.
However, we think it can be difficult to translate the theorethical documentation into an approach that solves a real-life challenge.
In this blog post, we will take a stepwise approach to building the necessary queries in Flux-InfluxDB, while displaying the intermediate results. Our use case is situated in the manufacturing industry. We hope this tutorial can help the community to form a better idea of what is possible and how it can be achieved.
So, what about that challenge? Read on!
The case: how to query InfluxDB using Flux
Imagine you’re a manufacturer of sweet delicious candy. The marketing department has spotted a new opportunity for a new type of candy: a bubble gum core coated with fancy colors and wacky flavours. After launching a marketing campaign, demand has soared!
Now it’s up to you to satisfy that demand without buying more machines. Thanks a lot, marketing dep… but how are we going to do all that?
Luckily, the bubble gum candy is actually a pretty simple product: it’s made of a core of bubble gum - 3 different tastes are possible - and a unique flavor coating. After flavoring, the bubble gum candy is immediately packaged on the box filler and ready for shipping.
Factory layout
Our factory layout looks like this: we own 3 bubble gum machines that are each built to make one type of bubble gum. The bubble gums they produce are then transported over a product router, so that each coater and box filler can draw bubble gum from a specific bubble gum machine. Think of all the options!
The problem
To smoothen production speeds and run our production plant optimally, we need to constantly balance the output speed of our 3 bubble gum machines with the processing speed of our 11 flavoring and packaging stations, depending on which flavoring machine draws product from which bubble gum machine.
If we run our bubble gum machines too quickly, we will create an overload of bubble gum cores because our flavoring machines can’t process them quickly enough. Run the bubble gum machines too slowly however and we are underutilizing our capacity, resulting in frequent starts and stops of our flavoring machines, which is detrimental to productivity. This is what we call a mass balance.
The expected outcome
Keeping the mass balance between supply (the bubble gum machines) and demand (the flavoring machines and packaging stations) is key to keep our production manager happy. To help her, we will calculate the total kg / h that is being drawn by the flavoring machines from each bubble gum machine, respectively. She can then use this output to take appropriate action if needed, for example by changing the speed of the bubble gum machines. Flux to the rescue!
The dataset
To make this possible, we need the following data from Factry Historian:
Mass flow for all producers (3 in this case). These measurements are named BGX_FLOW_KG/H_ACT, with X = {1, 2, 3} and are stored as kilograms per hour (kg/h).
Mass flow for all consumers (11 in this case). This data is not directly available, but we can compute this by multiplying the amount of boxes per minute (in boxes per minute) with the mass of each box of bubble gum (in g). The data is available as machine setpoints. The measurements are named CYY_BM_SPEED_BPM_SP and CYY_BM_MW_WEIGHT_TARGET_G_SP (with YY = {01 - 11}) .
The routing map (3 * 11 = 33 in this case). These booleans indicate which consumer (coater and box filler) is drawing product from which producer (bubble gum maker). These are named CYY_ROUTING_BGX. For example, a measurement C03_ROUTING_BG1 with field value_num = 1.0 would mean that coater/packager 03 is drawing bubble gums from bubble gum maker 1.
This data is being gathered from the machine PLCs e.g. via OPC-UA, with InfluxDB as time-series database.
Writing the Flux queries
We typically write our Flux queries in Chronograf because it ties in pretty well with Flux’s capabilities. Once the queries are finished, we switch to Grafana to build the dashboard that is used in production.
For this example, we will focus on getting a view on which coaters/packagers are drawing product from the second bubble gum machine.
1. Obtaining the downstream consumers for a specific producer
We’ll start by obtaining a view on the downstream consumers i.e. the coaters/packagers that are drawing product from producer 2. To do so, we’ll retrieve the last 30 days of data and filter on the routing measurements. We’re only interested in the current routing, so our query becomes:
We are only interested in getting the product flow from machines that are actively drawing product from the upstream producer. So, we add the next operation, namely a filter() statement for values that are 1. Because Flux only allows to compare values of the same type, we check for equality to 1.0.
Note the range starting at -30d. We can reasonably expect consumers to change which producer they draw product from at least once a month. This rather large range does not have a negative impact on the query speed because the data is very sparse as you can see from the _time column.
As you can see from the table above, we end up with the typical case in industrial settings where we have really sparse data. If you look at the _time column, you can see the timestamp for the _value_num bits being written to InfluxDB over the span of 5 days. This is different from much of the DevOps use cases we encounter regularly, such as memory and CPU usage being written e.g. every 10 sec.
Before we can proceed to request the throughput from the consumers, we need to prepare our data a little more. We will need to join our data later on, but as you can expect from the sparse data, we will not be joining on _time. We will join on the _measurement column or, more precisely, on multiple _measurement columns. Do achieve that, we will import the strings package and use its substring function to split the first 3 characters of the list of machines and append this to the other measurements. Our query now becomes:
We find that coaters/packagers 2, 6, 7, 10 and 11 are drawing from our producer, bubble gum machine 2.
2. Obtaining the consumer’s throughput
The consumer’s output should be presented in kg/h because the producers are also measured in kg/h. As mentioned above, we need to multiply two measurements to achieve this, namely the boxes per minute (BPM) and the weight per box (G_SP, set point in g).
Let’s start with retrieving the machine setpoint in gram per minute. To prepare the data for joining later, we’ll rename the _measurement column to _measurement_targetgsp.
So now that we have retrieved the routing table and the flow of product, let’s combine all of this to retrieve the total product draw from each bubble gum machine.
We have found our total product draw in kg / h from bubble gum machine 2. Great!
3. Obtaining the producer mass flow
We will write a query to retrieve the last 30 days of data from the historian database, filter on the measurements from bubble gum machine 2 (for example). We are only interested in the last or current values, so the query becomes:
As simple as that! We find that bubble gum machine 2 is producing bubble gum cores at 890 kg/h.
4. Now what?
We have found that our bubble gum machine 2 is only producing bubble gum at 890 kg / h while our coaters and packagers are set to handle 1575 kg / h. I guess we’ll have to inform our production manager to:
increase the speed of the bubble gum machine to match the product draw or buy a new bubble gum machine because we are not running at full capacity
instruct our coaters/packagers to draw product from other bubble gum machines
lower the setpoints of our coaters/packagers so their combined draw equals 890 kg / h
In conclusion
We have shown a real-life use case of Flux to retrieve data in InfluxDB from multiple measurements and use it to solve a real business challenge encountered in a manufacturing or production context.
We’re curious about your InfluxDB/Flux use cases as well! If you can share them or if you have any questions, please do not hesitate to reach out through frederik@factry.io