英文:
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 == "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})
}
}
An example of the values contained in my variable modules
:
{"Module_one":{"host_one":"pending","host_two":"pending"},"Module_two":{"host_three":"err","host_four":"warning","host_five":"pending"}}
I get this output:
[{"module":"Module_one","nb_nodes":{"all":2,"pending":2,"critical":0}},{"module":"Module_two","nb_nodes":{"all":5,"pending":3,"critical":2}}]
I want to have this output :
[{"module":"Module_one","nb_nodes":{"all":2,"pending":2,"critical":0}},{"module":"Module_two","nb_nodes":{"all":3,"pending":1,"critical":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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论