You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "TSultanov (via GitHub)" <gi...@apache.org> on 2023/04/04 14:22:57 UTC

[GitHub] [beam] TSultanov opened a new pull request, #26102: [Playground] Gradle task to remove snippet by its ID

TSultanov opened a new pull request, #26102:
URL: https://github.com/apache/beam/pull/26102

   Add Gradle task to allow remove snippets form datastore by the snippet ID
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26102:
URL: https://github.com/apache/beam/pull/26102#issuecomment-1500143720

   Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment `assign set of reviewers`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #26102:
URL: https://github.com/apache/beam/pull/26102#discussion_r1170102726


##########
playground/backend/cmd/remove_unused_snippets.go:
##########
@@ -16,46 +16,141 @@
 package main
 
 import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/logger"
 	"context"
+	"flag"
 	"fmt"
 	"os"
-	"strconv"
 	"time"
 
 	"beam.apache.org/playground/backend/internal/db/datastore"
 	"beam.apache.org/playground/backend/internal/db/mapper"
 )
 
-func main() {
-	if len(os.Args) < 3 {
-		fmt.Println("Go must have at least three arguments")
-		return
+func createDatastoreClient(ctx context.Context, projectId string) (*datastore.Datastore, error) {
+	pcMapper := mapper.NewPrecompiledObjectMapper()
+	db, err := datastore.New(ctx, pcMapper, projectId)
+	if err != nil {
+		logger.Errorf("Couldn't create the database client, err: %s \n", err.Error())
+		return nil, err
 	}
-	dayDiff := os.Args[1]
-	projectId := os.Args[2]
-	fmt.Println("Removing unused snippets is running...")
+
+	return db, nil
+}
+
+func cleanup(dayDiff int, projectId, namespace string) error {
+	logger.Infof("Removing unused snippets is running...")
 	startDate := time.Now()
 
-	diff, err := strconv.Atoi(dayDiff)
+	ctx := context.WithValue(context.Background(), constants.DatastoreNamespaceKey, namespace)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't convert days to integer from the input parameter, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	ctx := context.Background()
-	pcMapper := mapper.NewPrecompiledObjectMapper()
-	db, err := datastore.New(ctx, pcMapper, projectId)
+	err = db.DeleteUnusedSnippets(ctx, int32(dayDiff))
+	if err != nil {
+		logger.Errorf("Couldn't delete unused code snippets, err: %s \n", err.Error())
+		return err
+	}
+
+	diffTime := time.Now().Sub(startDate).Milliseconds()
+	logger.Infof("Removing unused snippets finished, work time: %d ms\n", diffTime)
+
+	return nil
+}
+
+func remove(snippetId string, projectId, namespace string) error {
+	logger.Infof("Removing snippet %s is running...", snippetId)
+	startDate := time.Now()
+
+	ctx := context.WithValue(context.Background(), constants.DatastoreNamespaceKey, namespace)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	err = db.DeleteUnusedSnippets(ctx, int32(diff))
+	err = db.DeleteSnippet(ctx, snippetId)
 	if err != nil {
-		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
-		return
+		fmt.Printf("Couldn't delete code snippet, err: %s \n", err.Error())
+		return err
 	}
 
 	diffTime := time.Now().Sub(startDate).Milliseconds()
-	fmt.Printf("Removing unused snippets finished, work time: %d ms\n", diffTime)
+	logger.Infof("Removing snippet %s finished, work time: %d ms\n", snippetId, diffTime)
+
+	return nil
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Println("expected 'cleanup' or 'remove' subcommands")
+		os.Exit(1)
+	}
+
+	cwd, err := os.Getwd()
+	if err != nil {
+		fmt.Printf("Couldn't get the current working directory, err: %s \n", err.Error())
+		os.Exit(1)
+	}
+
+	switch os.Args[1] {

Review Comment:
   Good catch, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] olehborysevych commented on a diff in pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "olehborysevych (via GitHub)" <gi...@apache.org>.
olehborysevych commented on code in PR #26102:
URL: https://github.com/apache/beam/pull/26102#discussion_r1159977487


##########
playground/backend/cmd/remove_unused_snippets.go:
##########
@@ -16,44 +16,130 @@
 package main
 
 import (
+	"beam.apache.org/playground/backend/internal/logger"
 	"context"
+	"flag"
 	"fmt"
 	"os"
-	"strconv"
 	"time"
 
 	"beam.apache.org/playground/backend/internal/db/datastore"
 	"beam.apache.org/playground/backend/internal/db/mapper"
 )
 
-func main() {
-	if len(os.Args) < 3 {
-		fmt.Println("Go must have at least three arguments")
-		return
+func createDatastoreClient(ctx context.Context, projectId string) (*datastore.Datastore, error) {
+	pcMapper := mapper.NewPrecompiledObjectMapper()
+	db, err := datastore.New(ctx, pcMapper, projectId)
+	if err != nil {
+		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
+		return nil, err
 	}
-	dayDiff := os.Args[1]
-	projectId := os.Args[2]
-	fmt.Println("Removing unused snippets is running...")
-	startDate := time.Now()
 
-	diff, err := strconv.Atoi(dayDiff)
+	return db, nil
+}
+
+func cleanup(dayDiff int, projectId string) error {
+	ctx := context.Background()
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't convert days to integer from the input parameter, err: %s \n", err.Error())
-		return
+		return err
 	}
 
+	err = db.DeleteUnusedSnippets(ctx, int32(dayDiff))
+	if err != nil {
+		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+func remove(snippetId string, projectId string) error {
 	ctx := context.Background()
-	pcMapper := mapper.NewPrecompiledObjectMapper()
-	db, err := datastore.New(ctx, pcMapper, projectId)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	err = db.DeleteUnusedSnippets(ctx, int32(diff))
+	err = db.DeleteSnippet(ctx, snippetId)
 	if err != nil {
-		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
-		return
+		fmt.Printf("Couldn't delete code snippet, err: %s \n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Println("expected 'cleanup' or 'remove' subcommands")
+		os.Exit(1)
+	}
+
+	fmt.Println("Removing unused snippets is running...")

Review Comment:
   Please consider moving this below the place where we know if this is a cleanup or remove



##########
playground/backend/cmd/remove_unused_snippets.go:
##########
@@ -16,44 +16,130 @@
 package main
 
 import (
+	"beam.apache.org/playground/backend/internal/logger"
 	"context"
+	"flag"
 	"fmt"
 	"os"
-	"strconv"
 	"time"
 
 	"beam.apache.org/playground/backend/internal/db/datastore"
 	"beam.apache.org/playground/backend/internal/db/mapper"
 )
 
-func main() {
-	if len(os.Args) < 3 {
-		fmt.Println("Go must have at least three arguments")
-		return
+func createDatastoreClient(ctx context.Context, projectId string) (*datastore.Datastore, error) {
+	pcMapper := mapper.NewPrecompiledObjectMapper()
+	db, err := datastore.New(ctx, pcMapper, projectId)
+	if err != nil {
+		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
+		return nil, err
 	}
-	dayDiff := os.Args[1]
-	projectId := os.Args[2]
-	fmt.Println("Removing unused snippets is running...")
-	startDate := time.Now()
 
-	diff, err := strconv.Atoi(dayDiff)
+	return db, nil
+}
+
+func cleanup(dayDiff int, projectId string) error {
+	ctx := context.Background()
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't convert days to integer from the input parameter, err: %s \n", err.Error())
-		return
+		return err
 	}
 
+	err = db.DeleteUnusedSnippets(ctx, int32(dayDiff))
+	if err != nil {
+		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+func remove(snippetId string, projectId string) error {
 	ctx := context.Background()
-	pcMapper := mapper.NewPrecompiledObjectMapper()
-	db, err := datastore.New(ctx, pcMapper, projectId)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	err = db.DeleteUnusedSnippets(ctx, int32(diff))
+	err = db.DeleteSnippet(ctx, snippetId)
 	if err != nil {
-		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
-		return
+		fmt.Printf("Couldn't delete code snippet, err: %s \n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Println("expected 'cleanup' or 'remove' subcommands")
+		os.Exit(1)
+	}
+
+	fmt.Println("Removing unused snippets is running...")
+	startDate := time.Now()
+
+	cwd, err := os.Getwd()
+	if err != nil {
+		fmt.Printf("Couldn't get the current working directory, err: %s \n", err.Error())
+		os.Exit(1)
+	}
+
+	switch os.Args[1] {
+	case "cleanup":
+		cleanupCmd := flag.NewFlagSet("cleanup", flag.ExitOnError)
+		dayDiff := cleanupCmd.Int("day_diff", -1, "a number of days to keep the code snippets")
+		projectId := cleanupCmd.String("project_id", "", "a project id where the code snippets are stored")
+
+		err := cleanupCmd.Parse(os.Args[2:])
+		if err != nil {
+			fmt.Printf("Couldn't parse the command line arguments, err: %s \n", err.Error())
+			os.Exit(1)
+		}
+
+		if projectId == nil || *projectId == "" {
+			fmt.Println("project_id is required")
+			os.Exit(1)
+		}
+
+		if *dayDiff < 0 {
+			fmt.Println("day_diff is required and should be greater than 0")
+			os.Exit(1)
+		}
+
+		logger.SetupLogger(context.Background(), cwd, *projectId)
+		err = cleanup(*dayDiff, *projectId)
+		if err != nil {
+			fmt.Printf("Couldn't cleanup the database, err: %s \n", err.Error())

Review Comment:
   consider using log.Fatal



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damccorm merged pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm merged PR #26102:
URL: https://github.com/apache/beam/pull/26102


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] rshamunov commented on a diff in pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "rshamunov (via GitHub)" <gi...@apache.org>.
rshamunov commented on code in PR #26102:
URL: https://github.com/apache/beam/pull/26102#discussion_r1163950892


##########
playground/README.md:
##########
@@ -150,6 +150,15 @@ cd beam
 ./gradlew playground:backend:removeUnusedSnippet -DdayDiff={int} -DprojectId={string}
 ```
 
+## Removing a specific snippet
+
+Run the method to remove a specific code snippet from the Cloud Datastore.
+
+```
+cd beam
+./gradlew playground:backend:removeSnippet -DsnippetId={string} -DprojectId={string}

Review Comment:
   We need support for custom Datastore namespaces



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damccorm commented on a diff in pull request #26102: [Playground] Gradle task to remove snippet by its ID

Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #26102:
URL: https://github.com/apache/beam/pull/26102#discussion_r1170066578


##########
playground/backend/cmd/remove_unused_snippets.go:
##########
@@ -16,46 +16,141 @@
 package main
 
 import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/logger"
 	"context"
+	"flag"
 	"fmt"
 	"os"
-	"strconv"
 	"time"
 
 	"beam.apache.org/playground/backend/internal/db/datastore"
 	"beam.apache.org/playground/backend/internal/db/mapper"
 )
 
-func main() {
-	if len(os.Args) < 3 {
-		fmt.Println("Go must have at least three arguments")
-		return
+func createDatastoreClient(ctx context.Context, projectId string) (*datastore.Datastore, error) {
+	pcMapper := mapper.NewPrecompiledObjectMapper()
+	db, err := datastore.New(ctx, pcMapper, projectId)
+	if err != nil {
+		logger.Errorf("Couldn't create the database client, err: %s \n", err.Error())
+		return nil, err
 	}
-	dayDiff := os.Args[1]
-	projectId := os.Args[2]
-	fmt.Println("Removing unused snippets is running...")
+
+	return db, nil
+}
+
+func cleanup(dayDiff int, projectId, namespace string) error {
+	logger.Infof("Removing unused snippets is running...")
 	startDate := time.Now()
 
-	diff, err := strconv.Atoi(dayDiff)
+	ctx := context.WithValue(context.Background(), constants.DatastoreNamespaceKey, namespace)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't convert days to integer from the input parameter, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	ctx := context.Background()
-	pcMapper := mapper.NewPrecompiledObjectMapper()
-	db, err := datastore.New(ctx, pcMapper, projectId)
+	err = db.DeleteUnusedSnippets(ctx, int32(dayDiff))
+	if err != nil {
+		logger.Errorf("Couldn't delete unused code snippets, err: %s \n", err.Error())
+		return err
+	}
+
+	diffTime := time.Now().Sub(startDate).Milliseconds()
+	logger.Infof("Removing unused snippets finished, work time: %d ms\n", diffTime)
+
+	return nil
+}
+
+func remove(snippetId string, projectId, namespace string) error {
+	logger.Infof("Removing snippet %s is running...", snippetId)
+	startDate := time.Now()
+
+	ctx := context.WithValue(context.Background(), constants.DatastoreNamespaceKey, namespace)
+	db, err := createDatastoreClient(ctx, projectId)
 	if err != nil {
-		fmt.Printf("Couldn't create the database client, err: %s \n", err.Error())
-		return
+		return err
 	}
 
-	err = db.DeleteUnusedSnippets(ctx, int32(diff))
+	err = db.DeleteSnippet(ctx, snippetId)
 	if err != nil {
-		fmt.Printf("Couldn't delete unused code snippets, err: %s \n", err.Error())
-		return
+		fmt.Printf("Couldn't delete code snippet, err: %s \n", err.Error())
+		return err
 	}
 
 	diffTime := time.Now().Sub(startDate).Milliseconds()
-	fmt.Printf("Removing unused snippets finished, work time: %d ms\n", diffTime)
+	logger.Infof("Removing snippet %s finished, work time: %d ms\n", snippetId, diffTime)
+
+	return nil
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Println("expected 'cleanup' or 'remove' subcommands")
+		os.Exit(1)
+	}
+
+	cwd, err := os.Getwd()
+	if err != nil {
+		fmt.Printf("Couldn't get the current working directory, err: %s \n", err.Error())
+		os.Exit(1)
+	}
+
+	switch os.Args[1] {

Review Comment:
   Should we have a default case to catch if the user didn't correctly specify either cleanup or remove?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org