How to use getNewestExecutions method of v1 Package

Best Testkube code snippet using v1.getNewestExecutions

executions.go

Source:executions.go Github

copy

Full Screen

...551 return result552}553// GetLatestExecutionLogs returns the latest executions' logs554func (s *TestkubeAPI) GetLatestExecutionLogs(c context.Context) (map[string][]string, error) {555 latestExecutions, err := s.getNewestExecutions(c)556 if err != nil {557 return nil, fmt.Errorf("could not list executions: %w", err)558 }559 executionLogs := map[string][]string{}560 for _, e := range latestExecutions {561 logs, err := s.getExecutionLogs(e)562 if err != nil {563 return nil, fmt.Errorf("could not get logs: %w", err)564 }565 executionLogs[e.Id] = logs566 }567 return executionLogs, nil568}569// getNewestExecutions returns the latest Testkube executions570func (s *TestkubeAPI) getNewestExecutions(c context.Context) ([]testkube.Execution, error) {571 f := result.NewExecutionsFilter().WithPage(1).WithPageSize(latestExecutions)572 executions, err := s.ExecutionResults.GetExecutions(c, f)573 if err != nil {574 return []testkube.Execution{}, fmt.Errorf("could not get executions from repo: %w", err)575 }576 return executions, nil577}578// getExecutionLogs returns logs from an execution579func (s *TestkubeAPI) getExecutionLogs(execution testkube.Execution) ([]string, error) {580 result := []string{}581 if execution.ExecutionResult.IsCompleted() {582 return append(result, execution.ExecutionResult.Output), nil583 }584 logs, err := s.Executor.Logs(execution.Id)...

Full Screen

Full Screen

mongo.go

Source:mongo.go Github

copy

Full Screen

1package testresult2import (3 "context"4 "strings"5 "time"6 "go.mongodb.org/mongo-driver/bson"7 "go.mongodb.org/mongo-driver/bson/primitive"8 "go.mongodb.org/mongo-driver/mongo"9 "go.mongodb.org/mongo-driver/mongo/options"10 "github.com/kubeshop/testkube/internal/pkg/api/repository/common"11 "github.com/kubeshop/testkube/pkg/api/v1/testkube"12)13var _ Repository = &MongoRepository{}14const CollectionName = "testresults"15func NewMongoRespository(db *mongo.Database) *MongoRepository {16 return &MongoRepository{17 Coll: db.Collection(CollectionName),18 }19}20type MongoRepository struct {21 Coll *mongo.Collection22}23func (r *MongoRepository) Get(ctx context.Context, id string) (result testkube.TestSuiteExecution, err error) {24 err = r.Coll.FindOne(ctx, bson.M{"id": id}).Decode(&result)25 return26}27func (r *MongoRepository) GetByName(ctx context.Context, name string) (result testkube.TestSuiteExecution, err error) {28 err = r.Coll.FindOne(ctx, bson.M{"name": name}).Decode(&result)29 return30}31func (r *MongoRepository) GetByNameAndTestSuite(ctx context.Context, name, testSuiteName string) (result testkube.TestSuiteExecution, err error) {32 err = r.Coll.FindOne(ctx, bson.M{"name": name, "testsuite.name": testSuiteName}).Decode(&result)33 return34}35func (r *MongoRepository) GetLatestByTestSuite(ctx context.Context, testSuiteName, sortField string) (result testkube.TestSuiteExecution, err error) {36 findOptions := options.FindOne()37 findOptions.SetSort(bson.D{{Key: sortField, Value: -1}})38 err = r.Coll.FindOne(ctx, bson.M{"testsuite.name": testSuiteName}, findOptions).Decode(&result)39 return40}41func (r *MongoRepository) GetLatestByTestSuites(ctx context.Context, testSuiteNames []string, sortField string) (executions []testkube.TestSuiteExecution, err error) {42 var results []struct {43 LatestID string `bson:"latest_id"`44 }45 if len(testSuiteNames) == 0 {46 return executions, nil47 }48 conditions := bson.A{}49 for _, testSuiteName := range testSuiteNames {50 conditions = append(conditions, bson.M{"testsuite.name": testSuiteName})51 }52 pipeline := []bson.D{{{Key: "$match", Value: bson.M{"$or": conditions}}}}53 pipeline = append(pipeline, bson.D{{Key: "$sort", Value: bson.D{{Key: sortField, Value: -1}}}})54 pipeline = append(pipeline, bson.D{55 {Key: "$group", Value: bson.D{{Key: "_id", Value: "$testsuite.name"}, {Key: "latest_id", Value: bson.D{{Key: "$first", Value: "$id"}}}}}})56 cursor, err := r.Coll.Aggregate(ctx, pipeline)57 if err != nil {58 return nil, err59 }60 err = cursor.All(ctx, &results)61 if err != nil {62 return nil, err63 }64 if len(results) == 0 {65 return executions, nil66 }67 conditions = bson.A{}68 for _, result := range results {69 conditions = append(conditions, bson.M{"id": result.LatestID})70 }71 cursor, err = r.Coll.Find(ctx, bson.M{"$or": conditions})72 if err != nil {73 return nil, err74 }75 err = cursor.All(ctx, &executions)76 if err != nil {77 return nil, err78 }79 return executions, nil80}81func (r *MongoRepository) GetNewestExecutions(ctx context.Context, limit int) (result []testkube.TestSuiteExecution, err error) {82 result = make([]testkube.TestSuiteExecution, 0)83 resultLimit := int64(limit)84 opts := &options.FindOptions{Limit: &resultLimit}85 opts.SetSort(bson.D{{Key: "_id", Value: -1}})86 cursor, err := r.Coll.Find(ctx, bson.M{}, opts)87 if err != nil {88 return result, err89 }90 err = cursor.All(ctx, &result)91 return92}93func (r *MongoRepository) GetExecutionsTotals(ctx context.Context, filter ...Filter) (totals testkube.ExecutionsTotals, err error) {94 var result []struct {95 Status string `bson:"_id"`96 Count int `bson:"count"`97 }98 query := bson.M{}99 if len(filter) > 0 {100 query, _ = composeQueryAndOpts(filter[0])101 }102 pipeline := []bson.D{{{Key: "$match", Value: query}}}103 if len(filter) > 0 {104 pipeline = append(pipeline, bson.D{{Key: "$sort", Value: bson.D{{Key: "starttime", Value: -1}}}})105 pipeline = append(pipeline, bson.D{{Key: "$skip", Value: int64(filter[0].Page() * filter[0].PageSize())}})106 pipeline = append(pipeline, bson.D{{Key: "$limit", Value: int64(filter[0].PageSize())}})107 }108 pipeline = append(pipeline, bson.D{{Key: "$group", Value: bson.D{{Key: "_id", Value: "$status"},109 {Key: "count", Value: bson.D{{Key: "$sum", Value: 1}}}}}})110 cursor, err := r.Coll.Aggregate(ctx, pipeline)111 if err != nil {112 return totals, err113 }114 err = cursor.All(ctx, &result)115 if err != nil {116 return totals, err117 }118 var sum int119 for _, o := range result {120 sum += o.Count121 switch testkube.TestSuiteExecutionStatus(o.Status) {122 case testkube.QUEUED_TestSuiteExecutionStatus:123 totals.Queued = o.Count124 case testkube.RUNNING_TestSuiteExecutionStatus:125 totals.Running = o.Count126 case testkube.PASSED_TestSuiteExecutionStatus:127 totals.Passed = o.Count128 case testkube.FAILED_TestSuiteExecutionStatus:129 totals.Failed = o.Count130 }131 }132 totals.Results = sum133 return134}135func (r *MongoRepository) GetExecutions(ctx context.Context, filter Filter) (result []testkube.TestSuiteExecution, err error) {136 result = make([]testkube.TestSuiteExecution, 0)137 query, opts := composeQueryAndOpts(filter)138 cursor, err := r.Coll.Find(ctx, query, opts)139 if err != nil {140 return141 }142 err = cursor.All(ctx, &result)143 return144}145func (r *MongoRepository) Insert(ctx context.Context, result testkube.TestSuiteExecution) (err error) {146 _, err = r.Coll.InsertOne(ctx, result)147 return148}149func (r *MongoRepository) Update(ctx context.Context, result testkube.TestSuiteExecution) (err error) {150 _, err = r.Coll.ReplaceOne(ctx, bson.M{"id": result.Id}, result)151 return152}153// StartExecution updates execution start time154func (r *MongoRepository) StartExecution(ctx context.Context, id string, startTime time.Time) (err error) {155 _, err = r.Coll.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": bson.M{"starttime": startTime}})156 return157}158// EndExecution updates execution end time159func (r *MongoRepository) EndExecution(ctx context.Context, id string, endTime time.Time, duration time.Duration) (err error) {160 _, err = r.Coll.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": bson.M{"endtime": endTime, "duration": duration.String()}})161 return162}163func composeQueryAndOpts(filter Filter) (bson.M, *options.FindOptions) {164 query := bson.M{}165 opts := options.Find()166 startTimeQuery := bson.M{}167 if filter.NameDefined() {168 query["testsuite.name"] = filter.Name()169 }170 if filter.TextSearchDefined() {171 query["name"] = bson.M{"$regex": primitive.Regex{Pattern: filter.TextSearch(), Options: "i"}}172 }173 if filter.LastNDaysDefined() {174 startTimeQuery["$gte"] = time.Now().Add(-time.Duration(filter.LastNDays()) * 24 * time.Hour)175 }176 if filter.StartDateDefined() {177 startTimeQuery["$gte"] = filter.StartDate()178 }179 if filter.EndDateDefined() {180 startTimeQuery["$lte"] = filter.EndDate()181 }182 if len(startTimeQuery) > 0 {183 query["starttime"] = startTimeQuery184 }185 if filter.StatusesDefined() {186 statuses := filter.Statuses()187 if len(statuses) == 1 {188 query["status"] = statuses[0]189 } else {190 var conditions bson.A191 for _, status := range statuses {192 conditions = append(conditions, bson.M{"status": status})193 }194 query["$or"] = conditions195 }196 }197 if filter.Selector() != "" {198 items := strings.Split(filter.Selector(), ",")199 for _, item := range items {200 elements := strings.Split(item, "=")201 if len(elements) == 2 {202 query["labels."+elements[0]] = elements[1]203 } else if len(elements) == 1 {204 query["labels."+elements[0]] = bson.M{"$exists": true}205 }206 }207 }208 opts.SetSkip(int64(filter.Page() * filter.PageSize()))209 opts.SetLimit(int64(filter.PageSize()))210 opts.SetSort(bson.D{{Key: "starttime", Value: -1}})211 return query, opts212}213// DeleteByTest deletes execution results by test suite214func (r *MongoRepository) DeleteByTestSuite(ctx context.Context, testSuiteName string) (err error) {215 _, err = r.Coll.DeleteMany(ctx, bson.M{"testsuite.name": testSuiteName})216 return217}218// DeleteAll deletes all execution results219func (r *MongoRepository) DeleteAll(ctx context.Context) (err error) {220 _, err = r.Coll.DeleteMany(ctx, bson.M{})221 return222}223// DeleteByTestSuites deletes execution results by test suites224func (r *MongoRepository) DeleteByTestSuites(ctx context.Context, testSuiteNames []string) (err error) {225 if len(testSuiteNames) == 0 {226 return nil227 }228 var filter bson.M229 if len(testSuiteNames) > 1 {230 conditions := bson.A{}231 for _, testSuiteName := range testSuiteNames {232 conditions = append(conditions, bson.M{"testsuite.name": testSuiteName})233 }234 filter = bson.M{"$or": conditions}235 } else {236 filter = bson.M{"testsuite.name": testSuiteNames[0]}237 }238 _, err = r.Coll.DeleteMany(ctx, filter)239 return240}241// GetTestSuiteMetrics returns test executions metrics242func (r *MongoRepository) GetTestSuiteMetrics(ctx context.Context, name string, limit, last int) (metrics testkube.ExecutionsMetrics, err error) {243 query := bson.M{"testsuite.name": name}244 pipeline := []bson.D{}245 if last > 0 {246 query["starttime"] = bson.M{"$gte": time.Now().Add(-time.Duration(last) * 24 * time.Hour)}247 }248 pipeline = append(pipeline, bson.D{{Key: "$match", Value: query}})249 pipeline = append(pipeline, bson.D{{Key: "$sort", Value: bson.D{{Key: "starttime", Value: -1}}}})250 if limit > 0 {251 pipeline = append(pipeline, bson.D{{Key: "$limit", Value: limit}})252 }253 pipeline = append(pipeline, bson.D{254 {255 Key: "$project", Value: bson.D{256 {Key: "status", Value: 1},257 {Key: "duration", Value: 1},258 {Key: "starttime", Value: 1},259 {Key: "name", Value: 1},260 },261 },262 })263 cursor, err := r.Coll.Aggregate(ctx, pipeline)264 if err != nil {265 return metrics, err266 }267 var executions []testkube.ExecutionsMetricsExecutions268 err = cursor.All(ctx, &executions)269 if err != nil {270 return metrics, err271 }272 return common.CalculateMetrics(executions), nil273}...

Full Screen

Full Screen

getNewestExecutions

Using AI Code Generation

copy

Full Screen

1import (2var (3var (4 RootCmd = &cobra.Command{5 Run: func(cmd *cobra.Command, args []string) {6 fmt.Println(version.Info(Version, GitCommit, GitTreeState, BuildDate))7 cmd.Help()8 },9 }10func init() {11 RootCmd.AddCommand(version.Cmd(Version, GitCommit, GitTreeState, BuildDate))12 RootCmd.AddCommand(tasks.NewTaskCmd())13 RootCmd.AddCommand(webhook.NewWebhookCmd())14}15func main() {16 if err := RootCmd.Execute(); err != nil {17 fmt.Println(err)18 os.Exit(1)19 }20}21import (

Full Screen

Full Screen

getNewestExecutions

Using AI Code Generation

copy

Full Screen

1func main() {2 client := v1.NewClient()3 ctx := context.Background()4 req := &v1.GetNewestExecutionsRequest{5 }6 resp, err := client.GetNewestExecutions(ctx, req)7 if err != nil {8 log.Fatal(err)9 }10 fmt.Printf("%+v11}12func main() {13 client := v1beta1.NewClient()14 ctx := context.Background()15 req := &v1beta1.GetNewestExecutionsRequest{16 }17 resp, err := client.GetNewestExecutions(ctx, req)18 if err != nil {19 log.Fatal(err)20 }21 fmt.Printf("%+v22}23func main() {24 client := v1beta2.NewClient()25 ctx := context.Background()26 req := &v1beta2.GetNewestExecutionsRequest{27 }28 resp, err := client.GetNewestExecutions(ctx, req)29 if err != nil {30 log.Fatal(err)31 }32 fmt.Printf("%+v33}34func main() {35 client := v1beta3.NewClient()36 ctx := context.Background()

Full Screen

Full Screen

getNewestExecutions

Using AI Code Generation

copy

Full Screen

1newestExecutions, err := v1.getNewestExecutions(100)2if err != nil {3 log.Fatal(err)4}5for _, execution := range newestExecutions {6 fmt.Println(execution)7}8newestExecutions, err := v2.getNewestExecutions(100)9if err != nil {10 log.Fatal(err)11}12for _, execution := range newestExecutions {13 fmt.Println(execution)14}15newestExecutions, err := v3.getNewestExecutions(100)16if err != nil {17 log.Fatal(err)18}19for _, execution := range newestExecutions {20 fmt.Println(execution)21}22newestExecutions, err := v4.getNewestExecutions(100)23if err != nil {24 log.Fatal(err)25}26for _, execution := range newestExecutions {27 fmt.Println(execution)28}29newestExecutions, err := v5.getNewestExecutions(100)30if err != nil {31 log.Fatal(err)32}33for _, execution := range newestExecutions {34 fmt.Println(execution)35}36newestExecutions, err := v6.getNewestExecutions(100)37if err != nil {38 log.Fatal(err)39}40for _, execution := range newestExecutions {41 fmt.Println(execution)42}43newestExecutions, err := v7.getNewestExecutions(100)44if err != nil {45 log.Fatal(err)46}47for _, execution := range newestExecutions {48 fmt.Println(execution)49}50newestExecutions, err := v8.getNewestExecutions(100)51if err != nil {52 log.Fatal(err)53}

Full Screen

Full Screen

getNewestExecutions

Using AI Code Generation

copy

Full Screen

1func main() {2 v1 := v1.New()3 getNewestExecutionsOptions := v1.NewGetNewestExecutionsOptions()4 getNewestExecutionsOptions.SetLimit(3)5 resp, response, err := v1.GetNewestExecutions(getNewestExecutionsOptions)6 if err != nil {7 fmt.Println(err)8 } else {9 fmt.Printf("%v10 fmt.Printf("%v11 }12}13{14 {

Full Screen

Full Screen

getNewestExecutions

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 response, err := client.GetNewestExecutions("test", 5)4 if err != nil {5 fmt.Println("Error occurred")6 }7 fmt.Println(response)8}9import (10func main() {11 response, err := client.GetNewestExecutions("test", 5)12 if err != nil {13 fmt.Println("Error occurred")14 }15 fmt.Println(response)16}17import (18func main() {19 response, err := client.GetNewestExecutions("test", 5)20 if err != nil {21 fmt.Println("Error occurred")22 }23 fmt.Println(response)24}25import (26func main() {27 response, err := client.GetNewestExecutions("test", 5)28 if err != nil {29 fmt.Println("Error occurred")30 }31 fmt.Println(response)32}33import (34func main() {35 response, err := client.GetNewestExecutions("test", 5)36 if err != nil {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful