Files
cocos/pkg/sdk/agent.go
T
Sammy Kerata Oina ee7159a406 COCOS-35 - Refactor RunRequest to use structured Computation (#38)
* Refactor RunRequest to use structured Computation

The protobuf and associated service implementations for the RunRequest message were refactored to replace the raw Computation byte slice with a structured ComputationReq object. This allows clearer and more type-safe manipulation of computation requests. The grpc, http, and agent service layers were updated to build and parse ComputationReq accordingly. The ComputationReq structure includes details like IDs, names, time stamps, and metadata, forming a well-defined contract for computation tasks.

This change aligns with efforts to standardize request formats and improve clarity in inter-service communication. It impacts all systems interfacing with the RunRequest service and thus requires coordinated updates to the entire stack.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* Initialize metadata maps and handle nil values

Improved the robustness of metadata handling in gRPC endpoints and SDK by initializing metadata maps and explicitly checking for nil values before converting them. This ensures that both the agent's gRPC endpoint and the SDK properly handle cases where metadata fields may be uninitialized or contain nil values, preventing potential null pointer exceptions.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* Refactor computation request handling

Refactored the endpoint to construct Computation object from gRPC request, incorporating structpb for metadata handling and timestamppb for StartTime and EndTime fields. The management service and API requests are also updated to align with these changes, improving type safety and ensuring data is correctly marshalled when making service calls.

Resolves data marshalling issues for computation requests.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* use singular

Signed-off-by: SammyOina <sammyoina@gmail.com>

* remove unuse fields

Signed-off-by: SammyOina <sammyoina@gmail.com>

* remove unused fields

Signed-off-by: SammyOina <sammyoina@gmail.com>

---------

Signed-off-by: SammyOina <sammyoina@gmail.com>
2024-01-08 17:51:46 +01:00

113 lines
2.7 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package sdk
import (
"context"
mglog "github.com/absmach/magistrala/logger"
"github.com/ultravioletrs/cocos/agent"
)
var _ agent.Service = (*agentSDK)(nil)
type agentSDK struct {
client agent.AgentServiceClient
logger mglog.Logger
}
func NewAgentSDK(log mglog.Logger, agentClient agent.AgentServiceClient) *agentSDK {
return &agentSDK{
client: agentClient,
logger: log,
}
}
func (sdk *agentSDK) Run(ctx context.Context, computation agent.Computation) (string, error) {
var datasets []*agent.DatasetReq
for _, data := range computation.Datasets {
datasets = append(datasets, &agent.DatasetReq{Id: data.ID, Provider: data.Provider})
}
var algos []*agent.AlgorithmReq
for _, algo := range computation.Algorithms {
algos = append(algos, &agent.AlgorithmReq{Id: algo.ID, Provider: algo.Provider})
}
request := &agent.RunRequest{
Computation: &agent.ComputationReq{
Id: computation.ID,
Name: computation.Name,
Description: computation.Description,
Datasets: datasets,
Algorithms: algos,
ResultConsumers: computation.ResultConsumers,
Timeout: computation.Timeout.String(),
},
}
response, err := sdk.client.Run(ctx, request)
if err != nil {
sdk.logger.Error("Failed to call Run RPC")
return "", err
}
return response.Computation, nil
}
func (sdk *agentSDK) Algo(ctx context.Context, algorithm agent.Algorithm) (string, error) {
request := &agent.AlgoRequest{
Algorithm: algorithm.Algorithm,
Provider: algorithm.Provider,
Id: algorithm.ID,
}
response, err := sdk.client.Algo(ctx, request)
if err != nil {
sdk.logger.Error("Failed to call Algo RPC")
return "", err
}
return response.AlgorithmID, nil
}
func (sdk *agentSDK) Data(ctx context.Context, dataset agent.Dataset) (string, error) {
request := &agent.DataRequest{
Dataset: dataset.Dataset,
Provider: dataset.Provider,
Id: dataset.ID,
}
response, err := sdk.client.Data(ctx, request)
if err != nil {
sdk.logger.Error("Failed to call Data RPC")
return "", err
}
return response.DatasetID, nil
}
func (sdk *agentSDK) Result(ctx context.Context, consumer string) ([]byte, error) {
request := &agent.ResultRequest{
Consumer: consumer,
}
response, err := sdk.client.Result(ctx, request)
if err != nil {
sdk.logger.Error("Failed to call Result RPC")
return nil, err
}
return response.File, nil
}
func (sdk *agentSDK) Attestation(ctx context.Context) ([]byte, error) {
request := &agent.AttestationRequest{}
response, err := sdk.client.Attestation(ctx, request)
if err != nil {
sdk.logger.Error("Failed to call Attestation RPC")
return nil, err
}
return response.File, nil
}