mardi 18 mai 2021

Conditional loop to return a value


I try to recover the worst state it contains out of 4 states: critical, warning, notice, info The states are defined in this order from worst to best : critical, warning, notice, info

In my code below :

for _, log := range results {
    if log.Filename == metas[log.Id.Host] {
        if modules[log.Id.Module] == nil {
            modules[log.Id.Module] = make(map[string]interface{}, 0)
        } else {
            level := modules[log.Id.Module][log.Id.Host]
            if level != log.Id.Level {
                if log.Id.Level == "err" && (level == "warning" || level == "notice" || level == "info") {
                    modules[log.Id.Module][log.Id.Host] = "err"
                } else if log.Id.Level == "warning" && (level == "notice" || level == "info"){
                    modules[log.Id.Module][log.Id.Host] = "warning"
                } else if log.Id.Level == "notice" && level == "info" {
                    modules[log.Id.Module][log.Id.Host] = "notice"
                } else if log.Id.Level == "info" {
                    modules[log.Id.Module][log.Id.Host] = "info"
                }
            } else {
                modules[log.Id.Module][log.Id.Host] = log.Id.Level
            }
        }
    } 
}

When I display the values
I receive this value: {"Module":{"Host":"notice"} and when I reload I have this value : {"Module":{"Host":"info"}
Whereas normally I would only get the worst value each time I reload: notice

Aucun commentaire:

Enregistrer un commentaire