| 68 | } |
| 69 | |
| 70 | func (i *imlStrategyModule) GetStrategyLogs(ctx context.Context, keyword string, strategyID string, start time.Time, end time.Time, limit int64, offset int64) ([]*strategy_dto.LogItem, int64, error) { |
| 71 | if strategyID == "" { |
| 72 | return nil, 0, errors.New("strategy id required") |
| 73 | } |
| 74 | conditions := map[string]string{ |
| 75 | "strategy": strategyID, |
| 76 | } |
| 77 | if keyword != "" { |
| 78 | // 查询符合条件的应用ID |
| 79 | apps, err := i.appService.Search(ctx, keyword, map[string]interface{}{ |
| 80 | "as_app": true, |
| 81 | }) |
| 82 | if err != nil { |
| 83 | return nil, 0, err |
| 84 | } |
| 85 | orCondition := fmt.Sprintf("request_uri =~ \".*%s.*\"", keyword) |
| 86 | if len(apps) > 0 { |
| 87 | appIds := utils.SliceToSlice(apps, func(a *service.Service) string { return a.Id }) |
| 88 | orCondition = fmt.Sprintf("%s or application =~ \"%s\"", orCondition, strings.Join(appIds, "|")) |
| 89 | } |
| 90 | conditions["#1"] = orCondition |
| 91 | } |
| 92 | |
| 93 | c, err := i.clusterService.Get(ctx, cluster.DefaultClusterID) |
| 94 | if err != nil { |
| 95 | return nil, 0, fmt.Errorf("cluster %s not found", cluster.DefaultClusterID) |
| 96 | } |
| 97 | items, total, err := i.logService.Logs(ctx, "loki", c.Cluster, conditions, start, end, limit, offset) |
| 98 | if err != nil { |
| 99 | return nil, 0, err |
| 100 | } |
| 101 | result := make([]*strategy_dto.LogItem, 0, len(items)) |
| 102 | for _, item := range items { |
| 103 | result = append(result, &strategy_dto.LogItem{ |
| 104 | ID: item.ID, |
| 105 | Service: auto.UUID(item.Service), |
| 106 | Method: item.Method, |
| 107 | Url: item.Url, |
| 108 | RemoteIP: item.RemoteIP, |
| 109 | Consumer: auto.UUID(item.Consumer), |
| 110 | Authorization: auto.UUID(item.Authorization), |
| 111 | RecordTime: auto.TimeLabel(item.RecordTime), |
| 112 | }) |
| 113 | } |
| 114 | return result, total, nil |
| 115 | } |
| 116 | |
| 117 | func (i *imlStrategyModule) Restore(ctx context.Context, id string) error { |
| 118 | return i.strategyService.Restore(ctx, id) |