Skip to content

Commit

Permalink
template stats
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner committed Jul 11, 2023
1 parent 02b39bd commit d704078
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/unfurl-artifacts/.stats/stats.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env sh
set -e

echo "TOSCA"
yarn cli template stats \
--template examples/unfurl-artifacts/.stats/service-template-business-de.yaml \
--template examples/unfurl-artifacts/.stats/service-template-business-en.yaml \
--template examples/unfurl-artifacts/.stats/service-template-business-es.yaml \
--template examples/unfurl-artifacts/.stats/service-template-community-de.yaml \
--template examples/unfurl-artifacts/.stats/service-template-community-en.yaml \
--template examples/unfurl-artifacts/.stats/service-template-community-es.yaml \
--template examples/unfurl-artifacts/.stats/service-template-enterprise-de.yaml \
--template examples/unfurl-artifacts/.stats/service-template-enterprise-en.yaml \
--template examples/unfurl-artifacts/.stats/service-template-enterprise-es.yaml
echo

echo "VDMMv1"
yarn cli template stats --template examples/unfurl-artifacts/.stats/variable-service-template-v1.yaml
echo

echo "VDMMv2"
yarn cli template stats --template examples/unfurl-artifacts/.stats/variable-service-template-v2.yaml
echo
11 changes: 11 additions & 0 deletions src/cli/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ template
})
)

template
.command('stats')
.description('collects stats of a given service template')
.requiredOption('--template <strings...>', 'path to service template')
.action(
hae.exit(async options => {
const result = await Controller.template.stats(options)
console.log(result)
})
)

template
.command('test')
.description('runs tests defined in the CSAR')
Expand Down
2 changes: 2 additions & 0 deletions src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import pumlTopologyTemplate from './template/puml/topology'
import pumlTypesTemplate from './template/puml/types'
import queryTemplate from './template/query'
import resolveTemplate from './template/resolve'
import statsTemplate from './template/stats'
import testTemplate from './template/test'
import unpackageTemplate from './template/unpackage'
import codeTemplate from './templates/code'
Expand Down Expand Up @@ -94,6 +95,7 @@ const Controller = {
query: queryTemplate,
test: testTemplate,
inputs: inputsTemplate,
stats: statsTemplate,
puml: {
topology: pumlTopologyTemplate,
types: pumlTypesTemplate,
Expand Down
48 changes: 48 additions & 0 deletions src/controller/template/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as files from '#files'
import Graph from '#graph/graph'
import {ServiceTemplate} from '#spec/service-template'
import * as utils from '#utils'
import * as validator from '#validator'

export type TemplateStatsOptions = {
template: string[]
}

export type TemplateStats = {
nodes: number
relations: number
properties: number
types: number
policies: number
groups: number
inputs: number
artifacts: number
imports: number
elements: number
// Nodes + Relations + Properties + Artifacts
nrpa: number
}

export default async function (options: TemplateStatsOptions) {
if (validator.isUndefined(options.template)) throw new Error(`Template not defined`)

return utils.sumObjects(
options.template.map(it => {
const graph = new Graph(files.loadYAML<ServiceTemplate>(it))
const stats: TemplateStats = {
nodes: graph.nodes.length,
relations: graph.relations.length,
properties: graph.properties.length,
types: graph.types.length,
policies: graph.types.length,
groups: graph.groups.length,
inputs: graph.inputs.length,
artifacts: graph.artifacts.length,
imports: graph.artifacts.length,
elements: graph.elements.length,
nrpa: graph.nodes.length + graph.relations.length + graph.properties.length + graph.artifacts.length,
}
return stats
})
)
}
8 changes: 8 additions & 0 deletions src/server/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ resolvers.post(
})
)

resolvers.post(
'/template/stats',
hae.express(async (req, res, next) => {
const stats = await Controller.template.stats(req.body)
res.json({stats})
})
)

resolvers.post(
'/template/test',
hae.express(async (req, res, next) => {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,13 @@ export function propagateOptions<T>(data: {base: T; flag?: boolean; mode?: T; op
result = _.merge(result, _.clone(data.options))
return result
}

export function sumObjects(objects: {[key: string]: number}[]) {
return objects.reduce((a, b) => {
for (const key in b) {
// eslint-disable-next-line no-prototype-builtins
if (b.hasOwnProperty(key)) a[key] = (a[key] || 0) + b[key]
}
return a
}, {})
}

0 comments on commit d704078

Please sign in to comment.