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

Method logCount

log-driver/loki/loki.go:302–336  ·  view source on GitHub ↗
(clusterId string, conditions map[string]string, start time.Time, end time.Time)

Source from the content-addressed store, hash-verified

300}
301
302func (d *Driver) logCount(clusterId string, conditions map[string]string, start time.Time, end time.Time) (int64, error) {
303 // 先查在这段时间内符合条件的日志数量
304 queries := url.Values{}
305 queries.Add("start", strconv.FormatInt(start.UnixNano(), 10))
306 queries.Add("end", strconv.FormatInt(end.UnixNano(), 10))
307 cs := make([]string, 0, len(conditions))
308 for k, v := range conditions {
309 if strings.HasPrefix(k, "#") {
310 cs = append(cs, v)
311 continue
312 }
313 cs = append(cs, fmt.Sprintf("%s=\"%s\"", k, v))
314 }
315 tmpCondition := ""
316 if len(conditions) > 0 {
317 tmpCondition = "|" + strings.Join(cs, "|")
318 }
319 queries.Set("query", fmt.Sprintf("sum(count_over_time({cluster=\"%s\"} | json %s [720h]))", clusterId, tmpCondition))
320 list, err := send[LogCount](http.MethodGet, fmt.Sprintf("%s/loki/api/v1/query", d.url), d.headers, queries, "")
321 if err != nil {
322 return 0, err
323 }
324 if len(list) < 1 || len(list[0].Value) < 2 {
325 return 0, nil
326 }
327 value, ok := list[0].Value[1].(string)
328 if !ok {
329 return 0, nil
330 }
331 v, err := strconv.ParseInt(value, 10, 64)
332 if err != nil {
333 return 0, err
334 }
335 return v, nil
336}
337
338func send[T any](method string, uri string, headers map[string]string, queries url.Values, body string) ([]*T, error) {
339 if queries != nil && len(queries) > 0 {

Callers 1

LogsMethod · 0.95

Calls 1

SetMethod · 0.65

Tested by

no test coverage detected