From 23d644ed677fe67c64db5aa98411ef179fc5ca0b Mon Sep 17 00:00:00 2001 From: HaseenaSainul Date: Tue, 12 Dec 2023 01:57:44 -0500 Subject: [PATCH] CPPSDK: calls metrics support added --- .../declarations-override/calls-metrics.h | 5 ++++ .../templates/declarations/calls-metrics.h | 6 ++++ .../cpp/templates/imports/calls-metrics.impl | 1 + .../cpp/templates/methods/calls-metrics.cpp | 29 ++++++++++++++++++ .../cpp/templates/modules/src/module_impl.h | 2 ++ src/macrofier/engine.mjs | 30 +++++++++++-------- src/macrofier/index.mjs | 2 +- 7 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 languages/cpp/templates/imports/calls-metrics.impl diff --git a/languages/cpp/templates/declarations-override/calls-metrics.h b/languages/cpp/templates/declarations-override/calls-metrics.h index e69de29b..47554196 100644 --- a/languages/cpp/templates/declarations-override/calls-metrics.h +++ b/languages/cpp/templates/declarations-override/calls-metrics.h @@ -0,0 +1,5 @@ + /* + ${method.name} + ${method.description} + */ + ${method.signature.result} ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr )${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid} override; diff --git a/languages/cpp/templates/declarations/calls-metrics.h b/languages/cpp/templates/declarations/calls-metrics.h index e69de29b..fa1890c5 100644 --- a/languages/cpp/templates/declarations/calls-metrics.h +++ b/languages/cpp/templates/declarations/calls-metrics.h @@ -0,0 +1,6 @@ + /* + ${method.name} + ${method.description} + ${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated} + */ + virtual ${method.signature.result} ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr )${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid} = 0; diff --git a/languages/cpp/templates/imports/calls-metrics.impl b/languages/cpp/templates/imports/calls-metrics.impl new file mode 100644 index 00000000..75fd87c6 --- /dev/null +++ b/languages/cpp/templates/imports/calls-metrics.impl @@ -0,0 +1 @@ +#include "metrics_impl.h" diff --git a/languages/cpp/templates/methods/calls-metrics.cpp b/languages/cpp/templates/methods/calls-metrics.cpp index e69de29b..67b67d22 100644 --- a/languages/cpp/templates/methods/calls-metrics.cpp +++ b/languages/cpp/templates/methods/calls-metrics.cpp @@ -0,0 +1,29 @@ + /* ${method.rpc.name} - ${method.description} */ + static void ${method.name}Dispatcher(const void* result) { + Metrics::MetricsImpl::${method.name}(${if.result.nonboolean}${if.result.nonvoid}(static_cast<${method.result.json.type}>(const_cast(result)))${end.if.result.nonvoid}${end.if.result.nonboolean}); + } + /* ${method.rpc.name} - ${method.description} */ + ${method.signature.result} ${info.Title}Impl::${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err ) ${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid} + { + Firebolt::Error status = Firebolt::Error::NotConnected; +${if.result.nonvoid}${method.result.initialization}${end.if.result.nonvoid} + FireboltSDK::Transport* transport = FireboltSDK::Accessor::Instance().GetTransport(); + if (transport != nullptr) { + + JsonObject jsonParameters; + ${method.params.serialization.with.indent} + ${method.result.json.type} jsonResult; + status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult); + if (status == Firebolt::Error::None) { + FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module(), "${info.Title}.${method.rpc.name} is successfully invoked"); + ${if.result.nonvoid}${method.result.instantiation.with.indent}${end.if.result.nonvoid} + WPEFramework::Core::ProxyType job = WPEFramework::Core::ProxyType(WPEFramework::Core::ProxyType::Create(${method.name}Dispatcher, nullptr)); + WPEFramework::Core::IWorkerPool::Instance().Submit(job); + } + + } else { + FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module(), "Error in getting Transport err = %d", status); + } + + return${if.result.nonvoid} ${method.result.name}${end.if.result.nonvoid}; + } diff --git a/languages/cpp/templates/modules/src/module_impl.h b/languages/cpp/templates/modules/src/module_impl.h index 33e6e005..ce2837f4 100644 --- a/languages/cpp/templates/modules/src/module_impl.h +++ b/languages/cpp/templates/modules/src/module_impl.h @@ -16,6 +16,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#pragma once + #include "FireboltSDK.h" #include "IModule.h" /* ${IMPORTS} */ diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 70023eb1..f1f4e984 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -980,6 +980,20 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options = return links } +function getTemplateFromDestination(destination, templateName, templates) { + const destinationArray = destination.split('/').pop().split(/[_.]+/) + + let template = '' + destinationArray.filter(value => value).every((suffix) => { + template = getTemplate(templateName +`.${suffix}`, templates) + return template ? false: true + }) + if (!template) { + template = getTemplate(templateName, templates) + } + return template +} + const generateImports = (json, templates, options = { destination: '' }) => { let imports = '' @@ -1011,21 +1025,11 @@ const generateImports = (json, templates, options = { destination: '' }) => { imports += getTemplate('/imports/x-method', templates) } - const suffix = options.destination.split('.').pop() if (callsMetrics(json).length) { - imports += getTemplate(suffix ? `/imports/calls-metrics.${suffix}` : '/imports/calls-metrics', templates) - } - - const destinationArray = options.destination.split('/').pop().split(/[_.]+/) - let template = '' - destinationArray.filter(value => value).every((suffix) => { - template = getTemplate(`/imports/default.${suffix}`, templates) - return template ? false: true - }) - if (!template) { - template = getTemplate('/imports/default', templates) + imports += getTemplateFromDestination(options.destination, '/imports/calls-metrics', templates) } + let template = getTemplateFromDestination(options.destination, '/imports/default', templates) if (json['x-schemas'] && Object.keys(json['x-schemas']).length > 0 && !json.info['x-uri-titles']) { imports += Object.keys(json['x-schemas']).map(shared => template.replace(/\$\{info.title.lowercase\}/g, shared.toLowerCase())).join('') } @@ -1312,6 +1316,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) // hmm... how is this different from callbackSerializedList? i guess they get merged? const callbackResponseInst = event ? (event.params && event.params.length ? (event.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join(', ')) + ', ' : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : '' const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : '' + const resultSchemaType = result.schema.type const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : '' const resultParams = generateResultParams(result.schema, json, templates, { name: result.name}) @@ -1357,6 +1362,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '') .replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$1' : '') .replace(/\$\{if\.result.nonvoid\}(.*?)\$\{end\.if\.result.nonvoid\}/gms, resultType && resultType !== 'void' ? '$1' : '') + .replace(/\$\{if\.result.nonboolean\}(.*?)\$\{end\.if\.result.nonboolean\}/gms, resultSchemaType && resultSchemaType !== 'boolean' ? '$1' : '') .replace(/\$\{if\.result\.properties\}(.*?)\$\{end\.if\.result\.properties\}/gms, resultParams ? '$1' : '') .replace(/\$\{if\.params\.empty\}(.*?)\$\{end\.if\.params\.empty\}/gms, method.params.length === 0 ? '$1' : '') .replace(/\$\{if\.signature\.empty\}(.*?)\$\{end\.if\.signature\.empty\}/gms, (method.params.length === 0 && resultType === '') ? '$1' : '') diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index f2923717..c6cf1595 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -133,7 +133,7 @@ const macrofy = async ( const staticCodeList = staticContent ? await readDir(staticContent, { recursive: true }) : [] const staticModules = staticModuleNames.map(name => ( { info: { title: name } } )) - + let modules if (hidePrivate) { modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules, extractSubSchemas)).filter(hasPublicAPIs)