MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / LogCount

Method LogCount

log-driver/loki/loki.go:120–159  ·  view source on GitHub ↗
(clusterId string, conditions map[string]string, spendHour int64, group string)

Source from the content-addressed store, hash-verified

118}
119
120func (d *Driver) LogCount(clusterId string, conditions map[string]string, spendHour int64, group string) (map[string]int64, error) {
121
122 cs := make([]string, 0, len(conditions))
123 for k, v := range conditions {
124 if strings.HasPrefix(k, "#") {
125 cs = append(cs, v)
126 continue
127 }
128 cs = append(cs, fmt.Sprintf("%s=\"%s\"", k, v))
129 }
130 tmpCondition := ""
131 if len(conditions) > 0 {
132 tmpCondition = "|" + strings.Join(cs, "|")
133 }
134 queries := url.Values{}
135 queries.Set("query", fmt.Sprintf("sum(count_over_time({cluster=\"%s\"} | json %s [%dh])) by (%s)", clusterId, tmpCondition, spendHour, group))
136 sendRequestTime := time.Now()
137 list, err := send[LogCount](http.MethodGet, fmt.Sprintf("%s/loki/api/v1/query", d.url), d.headers, queries, "")
138 if err != nil {
139 return nil, err
140 }
141 log.DebugF("send request spend time: %v", time.Now().Sub(sendRequestTime))
142 log.Debug("query is ", queries.Get("query"))
143 result := make(map[string]int64)
144 for _, l := range list {
145 if len(l.Value) != 2 {
146 continue
147 }
148 value, ok := l.Value[1].(string)
149 if !ok {
150 continue
151 }
152 v, err := strconv.ParseInt(value, 10, 64)
153 if err != nil {
154 continue
155 }
156 result[l.Metric[group]] = v
157 }
158 return result, nil
159}
160
161func (d *Driver) LogRecords(clusterId string, start time.Time, end time.Time) ([]*log_driver.LogItem, error) {
162 if start.After(end) {

Callers 1

TestLokiFunction · 0.95

Calls 2

SetMethod · 0.65
GetMethod · 0.65

Tested by 1

TestLokiFunction · 0.76