go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/server/rpcs/bots_list_bot_events.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpcs
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/grpc/codes"
    21  	"google.golang.org/grpc/status"
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  
    24  	"go.chromium.org/luci/common/clock"
    25  	"go.chromium.org/luci/common/logging"
    26  	"go.chromium.org/luci/gae/service/datastore"
    27  
    28  	apipb "go.chromium.org/luci/swarming/proto/api_v2"
    29  	"go.chromium.org/luci/swarming/server/acls"
    30  	"go.chromium.org/luci/swarming/server/model"
    31  )
    32  
    33  // ListBotEvents implements the corresponding RPC method.
    34  func (*BotsServer) ListBotEvents(ctx context.Context, req *apipb.BotEventsRequest) (*apipb.BotEventsResponse, error) {
    35  	if req.BotId == "" {
    36  		return nil, status.Errorf(codes.InvalidArgument, "bot_id is required")
    37  	}
    38  
    39  	var err error
    40  	if req.Limit, err = ValidateLimit(req.Limit); err != nil {
    41  		return nil, status.Errorf(codes.InvalidArgument, "invalid limit: %s", err)
    42  	}
    43  
    44  	var cursor datastore.Cursor
    45  	if req.Cursor != "" {
    46  		var err error
    47  		cursor, err = datastore.DecodeCursor(ctx, req.Cursor)
    48  		if err != nil {
    49  			return nil, status.Errorf(codes.InvalidArgument, "invalid cursor: %s", err)
    50  		}
    51  	}
    52  
    53  	res := State(ctx).ACL.CheckBotPerm(ctx, req.BotId, acls.PermPoolsListBots)
    54  	if !res.Permitted {
    55  		return nil, res.ToGrpcErr()
    56  	}
    57  
    58  	q := model.BotEventsQuery(ctx, req.BotId)
    59  	if req.Start != nil {
    60  		q = q.Gte("ts", req.Start.AsTime())
    61  	}
    62  	if req.End != nil {
    63  		q = q.Lt("ts", req.End.AsTime())
    64  	}
    65  	q = q.Limit(req.Limit)
    66  	if cursor != nil {
    67  		q = q.Start(cursor)
    68  	}
    69  
    70  	out := &apipb.BotEventsResponse{}
    71  
    72  	err = datastore.Run(ctx, q, func(ev *model.BotEvent, cb datastore.CursorCB) error {
    73  		out.Items = append(out.Items, ev.ToProto())
    74  		if len(out.Items) == int(req.Limit) {
    75  			cursor, err := cb()
    76  			if err != nil {
    77  				return err
    78  			}
    79  			out.Cursor = cursor.String()
    80  			return datastore.Stop
    81  		}
    82  		return nil
    83  	})
    84  	if err != nil {
    85  		logging.Errorf(ctx, "Error querying BotEvent for %q: %s", req.BotId, err)
    86  		return nil, status.Errorf(codes.Internal, "datastore error fetching events")
    87  	}
    88  
    89  	out.Now = timestamppb.New(clock.Now(ctx))
    90  	return out, nil
    91  }