按键和值的范围和计数

huangapple go评论69阅读模式
英文:

Range and count by key and value

问题

我正在尝试遍历我的map[string]map[string]interface{}。

以下是我的代码:

var marshallJSON Level
var itemCountAll int64
var itemCountPending int64
var itemCountError int64
var itemCountWarning int64
var itemCountCritical int64
var m []*bson.M

for k, value := range modules {
    for _, level := range value {
        if level == "err" {
            itemCountError += 1
        }
        if level == "warning" {
            itemCountWarning += 1
        }
        if level == "pending" {
            itemCountPending += 1
        }
    }
    itemCountCritical = itemCountError + itemCountWarning
    itemCountAll = itemCountPending + itemCountCritical
    marshallJSON = Level{All: itemCountAll, Pending: itemCountPending, Critical: itemCountCritical}
    if k != "null" {
        m = append(m, &bson.M{"module": k, "nb_nodes": marshallJSON})
    }
}

我变量modules中包含的值的示例:

{"Module_one":{"host_one":"pending","host_two":"pending"},"Module_two":{"host_three":"err","host_four":"warning","host_five":"pending"}}

我得到的输出是:

[{"module":"Module_one","nb_nodes":{"all":2,"pending":2,"critical":0}},{"module":"Module_two","nb_nodes":{"all":5,"pending":3,"critical":2}}]

我想要的输出是:

[{"module":"Module_one","nb_nodes":{"all":2,"pending":2,"critical":0}},{"module":"Module_two","nb_nodes":{"all":3,"pending":1,"critical":2}}]

问题在于它将之前模块计算的值相加了起来。

我的问题是如何计算每个模块的状态数?

英文:

I'm trying to range over my map[string]map[string]interface{} <br>

Here is my code below :

var marshallJSON Level
var itemCountAll int64
var itemCountPending int64
var itemCountError int64
var itemCountWarning int64
var itemCountCritical int64
var m []*bson.M

for k, value := range modules {
	for _ , level := range value {
		if level == &quot;err&quot; {
			itemCountError += 1
		}
		if level == &quot;warning&quot; {
			itemCountWarning += 1
		}
		if level == &quot;pending&quot; {
			itemCountPending += 1
		}
	}
	itemCountCritical = itemCountError + itemCountWarning
	itemCountAll = itemCountPending + itemCountCritical
	marshallJSON = Level{All: itemCountAll, Pending: itemCountPending, Critical: itemCountCritical}
	if k != &quot;null&quot; {
		m = append(m, &amp;bson.M{&quot;module&quot;: k, &quot;nb_nodes&quot;: marshallJSON})
	}
}

An example of the values contained in my variable modules :

{&quot;Module_one&quot;:{&quot;host_one&quot;:&quot;pending&quot;,&quot;host_two&quot;:&quot;pending&quot;},&quot;Module_two&quot;:{&quot;host_three&quot;:&quot;err&quot;,&quot;host_four&quot;:&quot;warning&quot;,&quot;host_five&quot;:&quot;pending&quot;}}

I get this output:

[{&quot;module&quot;:&quot;Module_one&quot;,&quot;nb_nodes&quot;:{&quot;all&quot;:2,&quot;pending&quot;:2,&quot;critical&quot;:0}},{&quot;module&quot;:&quot;Module_two&quot;,&quot;nb_nodes&quot;:{&quot;all&quot;:5,&quot;pending&quot;:3,&quot;critical&quot;:2}}]

I want to have this output :

[{&quot;module&quot;:&quot;Module_one&quot;,&quot;nb_nodes&quot;:{&quot;all&quot;:2,&quot;pending&quot;:2,&quot;critical&quot;:0}},{&quot;module&quot;:&quot;Module_two&quot;,&quot;nb_nodes&quot;:{&quot;all&quot;:3,&quot;pending&quot;:1,&quot;critical&quot;:2}}]

The problem is that it takes the values calculated for the previous modules and adds them up <br>
My question is how to calculate the number of states per module ?

答案1

得分: 1

你必须在开始下一次迭代之前重置计数器,这样外部循环的每次迭代都不会包含前面的模块:

for k, value := range modules {
    itemCountError, itemCountWarning, itemCountPending, itemCountCritical,
        itemCountAll = 0, 0, 0, 0, 0

    // ...
}

或者更好的做法是,在循环内部声明计数器,这样它们在每次迭代中都从0开始:

for k, value := range modules {
    var itemCountError, itemCountWarning, itemCountPending,
        itemCountCritical, itemCountAll int64

    // ...
}

另外,请注意bson.M是一个映射,你不需要一个映射的指针(在底层,映射已经是指针),所以使用:

var m []bson.M
英文:

You have to reset the counters before you start the next iteration, so each iteration of the outer loop won't include the previous modules:

for k, value := range modules {
	itemCountError, itemCountWarning, itemCountPending, itemCountCritical,
		itemCountAll = 0, 0, 0, 0, 0

    // ...
}

Or better yet, just declare the counters inside the loop, so they always start from 0 in each iteration:

for k, value := range modules {
	var itemCountError, itemCountWarning, itemCountPending,
		itemCountCritical, itemCountAll int64

    // ...
}

Also note that bson.M is a map, you don't need a pointer to a map (maps are already pointers under the hood), so use:

var m []bson.M

huangapple
  • 本文由 发表于 2021年5月19日 17:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/67600969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定