AydarZaynutdinov commented on a change in pull request #16493:
URL: https://github.com/apache/beam/pull/16493#discussion_r783162649



##########
File path: playground/backend/cmd/server/server.go
##########
@@ -109,6 +119,18 @@ func setupCache(ctx context.Context, appEnv 
environment.ApplicationEnvs) (cache.
        }
 }
 
+// setupExamplesCatalog saves precompiled objects catalog from storage to cache
+func setupExamplesCatalog(ctx context.Context, cacheService cache.Cache) error 
{
+       sdkCategories, err := 
utils.GetPrecompiledObjectsCatalogFromStorage(ctx, pb.Sdk_SDK_UNSPECIFIED, "")
+       if err != nil {
+               return err

Review comment:
       Maybe add a log here?

##########
File path: playground/backend/cmd/server/controller.go
##########
@@ -241,20 +241,34 @@ func (controller *playgroundController) Cancel(ctx 
context.Context, info *pb.Can
 }
 
 // GetPrecompiledObjects returns the list of examples
+// - If SDK and category are unspecified in the request, gets the whole 
catalog from the cache
+//     - If there is no catalog in the cache, gets the catalog from the 
Storage and saves it to the cache
+// - If SDK or category is specified in the request, gets the specific catalog 
from the Storage
 func (controller *playgroundController) GetPrecompiledObjects(ctx 
context.Context, info *pb.GetPrecompiledObjectsRequest) 
(*pb.GetPrecompiledObjectsResponse, error) {
-       bucket := cloud_bucket.New()
-       sdkToCategories, err := bucket.GetPrecompiledObjects(ctx, info.Sdk, 
info.Category)
-       if err != nil {
-               logger.Errorf("GetPrecompiledObjects(): cloud storage error: 
%s", err.Error())
-               return nil, errors.InternalError("Error during getting 
Precompiled Objects", "Error with cloud connection")
-       }
-       response := pb.GetPrecompiledObjectsResponse{SdkCategories: 
make([]*pb.Categories, 0)}
-       for sdkName, categories := range *sdkToCategories {
-               sdkCategory := pb.Categories{Sdk: 
pb.Sdk(pb.Sdk_value[sdkName]), Categories: make([]*pb.Categories_Category, 0)}
-               for categoryName, precompiledObjects := range categories {
-                       utils.PutPrecompiledObjectsToCategory(categoryName, 
&precompiledObjects, &sdkCategory)
+       var response pb.GetPrecompiledObjectsResponse
+       if info.Sdk != pb.Sdk_SDK_UNSPECIFIED || info.Category != "" {
+               sdkCategories, err := 
utils.GetPrecompiledObjectsCatalogFromStorage(ctx, info.Sdk, info.Category)
+               if err != nil {
+                       logger.Errorf("GetPrecompiledObjects(): cloud storage 
error: %s", err.Error())
+                       return nil, errors.InternalError("Error during getting 
Precompiled Objects", "Error with cloud connection")
+               }
+               response = pb.GetPrecompiledObjectsResponse{SdkCategories: 
sdkCategories}

Review comment:
       Maybe wrap this part as a separate method? So we could use this method 
in `else` part.

##########
File path: playground/backend/internal/utils/precompiled_objects_utils.go
##########
@@ -37,3 +46,37 @@ func PutPrecompiledObjectsToCategory(categoryName string, 
precompiledObjects *cl
        }
        sdkCategory.Categories = append(sdkCategory.Categories, &category)
 }
+
+// GetPrecompiledObjectsCatalogFromCache returns the precompiled objects 
catalog from the cache
+func GetPrecompiledObjectsCatalogFromCache(ctx context.Context, cacheService 
cache.Cache) ([]*pb.Categories, error) {
+       value, err := cacheService.GetValue(ctx, ExamplesDataPipelineId, 
cache.ExamplesCatalog)
+       if err != nil {
+               logger.Errorf("%s: cache.GetValue: %s\n", 
ExamplesDataPipelineId, err.Error())
+               return nil, err
+       }
+       catalog, converted := value.([]*pb.Categories)
+       if !converted {
+               logger.Errorf("%s: couldn't convert value to catalog: %s", 
cache.ExamplesCatalog, value)
+               return nil, errors.InternalError("Error during getting the 
catalog from cache", "Error during getting status")

Review comment:
       ```suggestion
                return nil, errors.InternalError("Error during getting the 
catalog from cache", "Error during getting catalog values")
   ```

##########
File path: playground/backend/internal/utils/precompiled_objects_utils_test.go
##########
@@ -73,3 +77,82 @@ func TestPutPrecompiledObjectsToCategory(t *testing.T) {
                })
        }
 }
+
+func TestGetPrecompiledObjectsCatalogFromCache(t *testing.T) {
+       ctx := context.Background()
+       sdkCategories := []*pb.Categories{
+               {
+                       Sdk: pb.Sdk_SDK_JAVA,
+                       Categories: []*pb.Categories_Category{
+                               {
+                                       CategoryName: "TestCategory", 
PrecompiledObjects: []*pb.PrecompiledObject{
+                                               {
+                                                       CloudPath:   
"SDK_JAVA/TestCategory/TestName.java",
+                                                       Name:        "TestName",
+                                                       Description: 
"TestDescription",
+                                                       Type:        
pb.PrecompiledObjectType_PRECOMPILED_OBJECT_TYPE_EXAMPLE,
+                                               },
+                                       },
+                               },
+                       },
+               }}
+       type args struct {
+               ctx          context.Context
+               cacheService cache.Cache
+       }
+       tests := []struct {
+               name     string
+               args     args
+               prepFunc func(cacheService cache.Cache) error
+               want     []*pb.Categories
+               wantErr  bool
+       }{
+               {
+                       // Test case with getting Precompiled Objects Catalog 
from cache when it exists.
+                       // As a result, want to receive an expected catalog 
from cache.
+                       name: "get existing catalog",
+                       args: args{
+                               ctx:          ctx,
+                               cacheService: local.New(ctx),
+                       },
+                       prepFunc: func(cacheService cache.Cache) error {
+                               err := cacheService.SetValue(ctx, uuid.Nil, 
cache.ExamplesCatalog, sdkCategories)
+                               if err != nil {
+                                       return err
+                               }
+                               return nil
+                       },
+                       want:    sdkCategories,
+                       wantErr: false,
+               },
+               // Test case with getting Precompiled Objects Catalog from 
cache when it doesn't exist.
+               // As a result, want to receive an error.
+               {
+                       name: "get missing catalog",

Review comment:
       ```suggestion
                {
                        // Test case with getting Precompiled Objects Catalog 
from cache when it doesn't exist.
                        // As a result, want to receive an error.
                        name: "get missing catalog",
   ```




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to