Skip to content

Commit

Permalink
Merge pull request #16484 from [BEAM-13633] [Playground] Implement me…
Browse files Browse the repository at this point in the history
…thod to get a default example for each SDKs

* Implement method to get a default example for each SDKs

* Add error handling

* Added saving of precompiled objects catalog to cache at the server startup

* Added caching of the catalog only in case of unspecified SDK

* Update regarding comments

* Update regarding comments

* Simplified logging regarding comment

* Get defaultExamplePath from the corresponding config

* Refactoring code

* Add the `link` field to response

* Remove gjson;
Resolve conflicts;

* Refactoring code

* Getting default precompiled object from cache

* Refactoring code

* Added saving of precompiled objects catalog to cache at the server startup

* Added caching of the catalog only in case of unspecified SDK

* Update regarding comments

* Update regarding comments

* Simplified logging regarding comment

* Updates regarding comments

* Update for environment_service_test.go

* Get default example from catalog

* GetCatalogFromCacheOrStorage method

* Update licenses

* Update licenses;
Resolve conflicts;

* [BEAM-13633][Playground]
Change saving default precompiled objects to the cache

* [BEAM-13633][Playground]
Change logic of saving and receiving info about default precompiled objects

* [BEAM-13633][Playground]
Separate for each sdk

* [BEAM-13633][Playground]
regenerate proto files

* Add code of the default example to response

* Revert "Add code of the default example to response"

This reverts commit da6baa0.

* Refactoring code

* Refactoring code;
Add test;

* Edit commentaries

* Refactoring code

* Add bucket name to methods

Co-authored-by: Artur Khanin <[email protected]>
Co-authored-by: AydarZaynutdinov <[email protected]>
Co-authored-by: Pavel Avilov <pavel.avilov>
  • Loading branch information
3 people authored Mar 1, 2022
1 parent d42d8d7 commit 7735cb9
Show file tree
Hide file tree
Showing 20 changed files with 651 additions and 272 deletions.
1 change: 1 addition & 0 deletions playground/api/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ message PrecompiledObject{
string link = 6;
bool multifile = 7;
int32 context_line = 8;
bool default_example = 9;
}

// Categories represent the array of messages with sdk and categories at this sdk
Expand Down
30 changes: 20 additions & 10 deletions playground/backend/cmd/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,10 @@ func (controller *playgroundController) Cancel(ctx context.Context, info *pb.Can
// - 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 catalog from the cache and filters it by SDK and category
func (controller *playgroundController) GetPrecompiledObjects(ctx context.Context, info *pb.GetPrecompiledObjectsRequest) (*pb.GetPrecompiledObjectsResponse, error) {
catalog, err := controller.cacheService.GetCatalog(ctx)
catalog, err := utils.GetCatalogFromCacheOrStorage(ctx, controller.cacheService, controller.env.ApplicationEnvs.BucketName())
if err != nil {
logger.Errorf("GetPrecompiledObjects(): cache error: %s", err.Error())
catalog, err = utils.GetCatalogFromStorage(ctx, controller.env.ApplicationEnvs.BucketName())
if err != nil {
return nil, errors.InternalError("Error during getting Precompiled Objects", "Error with cloud connection")
}
if err = controller.cacheService.SetCatalog(ctx, catalog); err != nil {
logger.Errorf("GetPrecompiledObjects(): cache error: %s", err.Error())
}
logger.Errorf("GetPrecompiledObjects(): error during getting catalog: %s", err.Error())
return nil, errors.InternalError("Error during getting Precompiled Objects", "Error with cloud connection")
}
return &pb.GetPrecompiledObjectsResponse{
SdkCategories: utils.FilterCatalog(catalog, info.Sdk, info.Category),
Expand All @@ -279,7 +273,7 @@ func (controller *playgroundController) GetPrecompiledObjects(ctx context.Contex
// GetPrecompiledObjectCode returns the code of the specific example
func (controller *playgroundController) GetPrecompiledObjectCode(ctx context.Context, info *pb.GetPrecompiledObjectCodeRequest) (*pb.GetPrecompiledObjectCodeResponse, error) {
cd := cloud_bucket.New()
codeString, err := cd.GetPrecompiledObject(ctx, info.GetCloudPath(), controller.env.ApplicationEnvs.BucketName())
codeString, err := cd.GetPrecompiledObjectCode(ctx, info.GetCloudPath(), controller.env.ApplicationEnvs.BucketName())
if err != nil {
logger.Errorf("GetPrecompiledObjectCode(): cloud storage error: %s", err.Error())
return nil, errors.InternalError("Error during getting Precompiled Object's code", "Error with cloud connection")
Expand Down Expand Up @@ -323,3 +317,19 @@ func (controller *playgroundController) GetPrecompiledObjectGraph(ctx context.Co
response := pb.GetPrecompiledObjectGraphResponse{Graph: logs}
return &response, nil
}

// GetDefaultPrecompiledObject returns the default precompile object for sdk.
func (controller *playgroundController) GetDefaultPrecompiledObject(ctx context.Context, info *pb.GetDefaultPrecompiledObjectRequest) (*pb.GetDefaultPrecompiledObjectResponse, error) {
switch info.Sdk {
case pb.Sdk_SDK_UNSPECIFIED:
logger.Errorf("GetDefaultPrecompiledObject(): unimplemented sdk: %s\n", info.Sdk)
return nil, errors.InvalidArgumentError("Error during preparing", "Sdk is not implemented yet: %s", info.Sdk.String())
}
precompiledObject, err := utils.GetDefaultPrecompiledObject(ctx, info.Sdk, controller.cacheService, controller.env.ApplicationEnvs.BucketName())
if err != nil {
logger.Errorf("GetDefaultPrecompiledObject(): error during getting catalog: %s", err.Error())
return nil, errors.InternalError("Error during getting Precompiled Objects", "Error with cloud connection")
}
response := pb.GetDefaultPrecompiledObjectResponse{PrecompiledObject: precompiledObject}
return &response, nil
}
13 changes: 13 additions & 0 deletions playground/backend/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"beam.apache.org/playground/backend/internal/cache"
"beam.apache.org/playground/backend/internal/cache/local"
"beam.apache.org/playground/backend/internal/cache/redis"
"beam.apache.org/playground/backend/internal/cloud_bucket"
"beam.apache.org/playground/backend/internal/environment"
"beam.apache.org/playground/backend/internal/logger"
"beam.apache.org/playground/backend/internal/utils"
Expand Down Expand Up @@ -127,6 +128,18 @@ func setupExamplesCatalog(ctx context.Context, cacheService cache.Cache, bucketN
if err = cacheService.SetCatalog(ctx, catalog); err != nil {
logger.Errorf("GetPrecompiledObjects(): cache error: %s", err.Error())
}

bucket := cloud_bucket.New()
defaultPrecompiledObjects, err := bucket.GetDefaultPrecompiledObjects(ctx, bucketName)
if err != nil {
return err
}
for sdk, precompiledObject := range defaultPrecompiledObjects {
if err := cacheService.SetDefaultPrecompiledObject(ctx, sdk, precompiledObject); err != nil {
logger.Errorf("GetPrecompiledObjects(): cache error: %s", err.Error())
return err
}
}
return nil
}

Expand Down
Loading

0 comments on commit 7735cb9

Please sign in to comment.