| 115 | } |
| 116 | |
| 117 | func (lv *labelValue) refresh(args ...interface{}) error { |
| 118 | var count int64 |
| 119 | if err := lv.org.DB.Model(&metadbmodel.PrometheusLabelValue{}).Count(&count).Error; err != nil { |
| 120 | return err |
| 121 | } |
| 122 | |
| 123 | rows, err := lv.org.DB.Model(&metadbmodel.PrometheusLabelValue{}).Select("id", "value").Rows() |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | defer rows.Close() |
| 128 | |
| 129 | newActive := make(map[string]int, count) |
| 130 | newActiveR := make(map[int]string, count) |
| 131 | for rows.Next() { |
| 132 | var id int |
| 133 | var value string |
| 134 | if scanErr := rows.Scan(&id, &value); scanErr != nil { |
| 135 | log.Errorf("stream scan prometheus_label_value interrupted: %v", scanErr, lv.org.LogPrefix) |
| 136 | return scanErr |
| 137 | } |
| 138 | newActive[value] = id |
| 139 | newActiveR[id] = value |
| 140 | } |
| 141 | if err := rows.Err(); err != nil { |
| 142 | log.Errorf("stream read prometheus_label_value error: %v", err, lv.org.LogPrefix) |
| 143 | return err |
| 144 | } |
| 145 | |
| 146 | lv.mu.Lock() |
| 147 | pending := lv.pending |
| 148 | pendingR := lv.pendingIDToValue |
| 149 | lv.pending = make(map[string]int) |
| 150 | lv.pendingIDToValue = make(map[int]string) |
| 151 | for key, value := range pending { |
| 152 | newActive[key] = value |
| 153 | } |
| 154 | for id, value := range pendingR { |
| 155 | newActiveR[id] = value |
| 156 | } |
| 157 | lv.mu.Unlock() |
| 158 | |
| 159 | lv.activeR.Store(newActiveR) |
| 160 | lv.replaceActive(newActive) |
| 161 | return nil |
| 162 | } |