Skip to content

Commit

Permalink
CPPSDK: calls metrics support added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Dec 12, 2023
1 parent 4c9e870 commit 23d644e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
5 changes: 5 additions & 0 deletions languages/cpp/templates/declarations-override/calls-metrics.h
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions languages/cpp/templates/declarations/calls-metrics.h
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions languages/cpp/templates/imports/calls-metrics.impl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "metrics_impl.h"
29 changes: 29 additions & 0 deletions languages/cpp/templates/methods/calls-metrics.cpp
Original file line number Diff line number Diff line change
@@ -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<void*>(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<WPEFramework::Core::JSON::IElement>* 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<FireboltSDK::Accessor>(), "${info.Title}.${method.rpc.name} is successfully invoked");
${if.result.nonvoid}${method.result.instantiation.with.indent}${end.if.result.nonvoid}
WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch> job = WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch>(WPEFramework::Core::ProxyType<FireboltSDK::Worker>::Create(${method.name}Dispatcher, nullptr));
WPEFramework::Core::IWorkerPool::Instance().Submit(job);
}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}

return${if.result.nonvoid} ${method.result.name}${end.if.result.nonvoid};
}
2 changes: 2 additions & 0 deletions languages/cpp/templates/modules/src/module_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "FireboltSDK.h"
#include "IModule.h"
/* ${IMPORTS} */
Expand Down
30 changes: 18 additions & 12 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''

Expand Down Expand Up @@ -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('')
}
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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' : '')
Expand Down
2 changes: 1 addition & 1 deletion src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 23d644e

Please sign in to comment.