TickScript issues with no field or tag exists for a variable

#1

I am trying to calculate error percentage but getting the error message of no tag or field exists.I am not sure why I am getting the no field or tag error pops out but this error_rate is a variable I am using to calculate error percentage and is not part of influx DB to be associated with any tag or field. Can someone help me with debugging this or letting me know where I am going wrong.

Error I am getting : E! error evaluating expression for level CRITICAL: no field or tag exists for error_rate

//Parameters

var unit = 1m
var rateThreshold = 0
/var error_rate = 0

//Dataframe

var data = stream
|from()
.measurement(‘messages’)
.groupBy(‘service’,‘topic’)
|window()
.period(1m)
.every(1m)
|log()
.prefix('Messages: ')
.level(‘DEBUG’)

// get failed messages

var FM = data
|where(lambda: “status” == ‘FAILED’)
|count(‘messageCount’)
|log()
.prefix('Failed Messages: ')
.level(‘DEBUG’)

// get completed messages

var TM = data
|where(lambda: “status” == ‘COMPLETE’)
|count(‘messageCount’)
|log()
.prefix('Completed Messages: ')
.level(‘DEBUG’)

//Joining two to get error percentage.

TM
|join(FM)
.fill(0)
.as(‘TM’,‘FM’)
|eval(lambda: if((“FM.count” + “TM.count”) == 0, 0, ((“FM.count”)/(“FM.count” + “TM.count”))*100))
.as(‘error_rate’)
.keep()
|log()
.prefix('Error rate: ')
.level(‘DEBUG’)

var alert = data
|alert()
.id(’{{ index .Tags “service” }}-{{ index .Tags “topic” }}’)
.message(’{{ .Level }}: Error rate threshold exceeded for {{ index .Tags “service” }}-{{ index .Tags “topic” }} for {{ index .Fields “messageCount” }} messages’)
// .crit(eval(lambda: if((“FM.count” + “TM.count”) == 0, 0, ((“FM.count”)/(“FM.count” + “TM.count”))*100)) > rateThreshold)
.crit(lambda: “error_rate” > rateThreshold)
.stateChangesOnly(unit)

//alert

alert
.sensu()
.source(‘TimeRangeProcessingFailure’)
.id(’{{ index .Tags “service” }}-{{ index .Tags “topic” }}’)
//write to a log file.
.log(’/tmp/xMessageAlert.log’)