Excluding a series, based on another field's value

I apologise if I’m not using the correct terminology, but I’ve tried searching and not finding anything relating to the Flux language that might help point the right way.

I have SNMP data coming from some network switches, this has a number of fields such as ifName, ifHCInOctets, ifHCOutOctets, and ifConnectorPresent.

I can happily graph ifHCInOctets and ifHCInOctets using a query like so:

from(bucket: "mikrotik")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "snmp")
  |> filter(fn: (r) => r["_field"] == "ifHCInOctets" or r["_field"] == "ifHCOutOctets")
  |> filter(fn: (r) => r["hostname"] == "desk-sw1")
  |> derivative(unit: 1s, nonNegative: true)
  |> yield(name: "nonnegative derivative")

What I am trying to do, however, is exclude data when the value of ifConnectorPresent is 0. At a minimum just for that measurement, but ideally just totally exclude the whole series if the value is 0 for the entire range I’m looking at.

If it were SQL it would be something along the lines of:

SELECT time, ifName, ifHCInOctets, ifHCOutOctets
FROM mikrotik 
WHERE ifConnectorPresent==1 

or maybe:

SELECT time, ifName, ifHCInOctets, ifHCOutOctets
FROM mikrotik m join (select distinct ifName from mikrotik where ifConnectorPresent = 1) up on m.ifName = up.ifName 

Thanks!

Hello @willhughes-au,
You’ll want to use filtering something like:

|> filter(fn: (r) => r._field == "ifConnectorPresent" and r._value == 1

You might also want to be aware of:

Hi @Anaisdg - thanks for the response. .

Perhaps I don’t understand what you’re saying to do.

If I have a query like:

from(bucket: "mikrotik")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "snmp")
  |> filter(fn: (r) => r["_field"] == "ifConnectorPresent" and r["_value"] == 1)
  |> filter(fn: (r) => r["_field"] == "ifHCInOctets" or r["_field"] == "ifHCOutOctets")
  |> filter(fn: (r) => r["hostname"] == "desk-sw1")
  |> derivative(unit: 1s, nonNegative: true)
  |> yield(name: "nonnegative derivative")

then I get nothing, that would be because of this filter, which is the columns I actually want to chart:

  |> filter(fn: (r) => r["_field"] == "ifHCInOctets" or r["_field"] == "ifHCOutOctets")

However… The schema.fieldsAsCols function is nice, that’s looking like what I want. Thanks!

1 Like