FLUX - InfluxDB 2.0 - type error @40:29-40:44: [t8206] is not Comparable

Hello,

Writing a flux script in Explore.

We are trying to load data from csv and alert based on threshold (column in csv) for a respective host.

I am getting error at the following line:
crit: (r) => r._value > alert_threshold,

I am not able to figure out what is wrong. How to use variable alert_threshold under monitor.check

Error 
**type error @40:29-40:44: [t8206] is not Comparable**

You can run this script as it is to recreate:

import "csv"

import "influxdata/influxdb/monitor"

csvData = "

#datatype,string,long,string,double

#group,false,false,false,false

#default,,,,

,result,table,host,threshold

,mean,0,Computer_1,15.1

,mean,0,Computer_2,20.2

,mean,0,Computer_3,52.62

"

threshold_data = csv.from(csv: csvData)

getFieldValue = (threshold_data=<-,hostname) => 

threshold_data

|> reduce(

            identity: {new_threshold: 90.0},

            fn: (r,accumulator) => ({

              new_threshold:

                if r.host == hostname then r.threshold

                else accumulator.new_threshold

            })

        )

alert_threshold=threshold_data

|> getFieldValue(hostname: "A700459-W10")

|>yield()

main_data =  from(bucket: "amsv_bucket")

  |> range(start: -7d)

  |> filter(fn: (r) =>

      r._measurement == "Processor" and

      r._field == "Percent_Processor_Time"

  )

  |> group(columns: ["_measurement","host"])

  |> aggregateWindow(every: 1d, fn: mean, createEmpty: false)

  |> monitor.check(

    crit: (r) => r._value > alert_threshold,

    messageFn: (r) =>

      if r._level == "crit" then "Critical alert for host ${r.host} !!"

      else "Things are looking good for host ${r.host}.",

    data: {

      _check_name: "CPU Utilization (Used Percent_Processor_Time)",

      _check_id: "Percent_Processor_Time",

      _type: "threshold",

      tags: {}

    }

  )

|> yield(name: "val1")

Do you have any idea @Anaisdg ?

Hello @Ashish_Sikarwar,
I’m not sure. It looks like maybe that’s a value type error? Can you verify the types with the raw data view? and try converting the type please? Flux function types and categories | Flux 0.x Documentation

Hey @Anaisdg,

Actually it seemed to me too but it was not a conversion issue, they were all floats.

After making changes noted on line# 27, 28, 38 it worked for me.

  1. import “csv”

  2. import “influxdata/influxdb/monitor”

  3. csvData = "

  4. #datatype,string,long,string,double

  5. #group,false,false,false,false

  6. #default,

  7. ,result,table,host,threshold

  8. ,mean,0,A700459-W10,15.1

  9. ,mean,0,M20095-W10N,20.2

  10. ,mean,0,V822548-W10,52.62

  11. "

  12. threshold_data = csv.from(csv: csvData)

  13. getFieldValue = (threshold_data=<-,hostname) =>

  14. threshold_data

  15. |> reduce(

  16.         identity: {new_threshold: 90.0},
    
  17.         fn: (r,accumulator) => ({
    
  18.           new_threshold:
    
  19.             if r.host == hostname then r.threshold
    
  20.             else accumulator.new_threshold
    
  21.         })
    
  22.     )
    
  23. alert_threshold=threshold_data

  24. |> getFieldValue(hostname: “M20095-W10N”)

  25. //|>yield()

  26. |> findRecord(fn: (key) => true, idx: 0)

  27. main_data = from(bucket: “amsv_bucket”)

  28. |> range(start: -17d)

  29. |> filter(fn: ® =>

  30.   r._measurement == "Processor" and
    
  31.   r._field == "Percent_Processor_Time"
    
  32. )

  33. |> group(columns: ["_measurement",“host”])

  34. |> aggregateWindow(every: 1d, fn: mean, createEmpty: false)

  35. |> monitor.check(

  36. crit: (r) => r._value > alert_threshold.new_threshold ,
    
  37. messageFn: (r) =>
    
  38.   if r._level == "crit" then "Critical alert for host ${r.host} !!"
    
  39.   else "Things are looking good for host ${r.host}.",
    
  40. data: {
    
  41.   _check_name: "CPU Utilization (Used Percent_Processor_Time)",
    
  42.   _check_id: "Percent_Processor_Time",
    
  43.   _type: "threshold",
    
  44.   tags: {}
    
  45. }
    
  46. )

  47. |> yield(name: “val1”)

1 Like

Hello @Ashish_Sikarwar,
Nice! Do you mind me asking, what are you trying to do with that scipt/what’s your usecase?
Thanks

Hello @Anaisdg,
I won’t mind at all. Actually I was looking to create dynamic thresholds for specific clients on the fly or pull them from .csv file.
Some client wants to trigger an alert only when CPU usage is above 50% whereas other may want at > 95%

Thanks

1 Like

@Ashish_Sikarwar,
Cool! Thanks for sharing. I haven’t seen this approach yet.

:thought_balloon: :v: :smiley: