11/**
22 * A menu with right icons, see https://italia.github.io/design-react-kit/?path=/docs/documentazione-componenti-dropdown--documentazione#menu-icona-a-destra
33 */
4- import { Dropdown , DropdownToggle } from 'design-react-kit' ;
5-
6- import { DropdownMenu , Icon , LinkList , LinkListItem } from 'design-react-kit' ;
7- import { fromJS , Map } from 'immutable' ;
4+ import { Dropdown , DropdownMenu , DropdownToggle , Icon , LinkList , LinkListItem } from 'design-react-kit' ;
85import yaml from 'js-yaml' ;
96import { useConfiguration } from '../../configuration' ;
10- import { resolveJsonldContext } from '../../jsonld-context/resolve-jsonld-context' ;
11- import {
12- buildOntoScoreSparqlQuery ,
13- compressAndBase64UrlSafe ,
14- copyToClipboard ,
15- determinePropertiesToValidate ,
16- fetchValidOntoScorePropertiesCount ,
17- normalizeOpenAPISpec ,
18- resolveOpenAPISpec ,
19- } from '../utils' ;
7+ import { compressAndBase64UrlSafe , copyToClipboard , normalizeOpenAPISpec } from '../utils' ;
8+ import { calculateGlobalOntoscore } from '../utils/onto-score' ;
209
2110const copyAsB64zipToClipboard = ( text : string , prefix : string = '' ) => {
2211 copyToClipboard ( `${ prefix } ${ compressAndBase64UrlSafe ( text ) } ` ) ;
@@ -34,87 +23,14 @@ const downloadContent = (content: any, mediaType: string, fileName: string) => {
3423 URL . revokeObjectURL ( url ) ;
3524} ;
3625
37- export const createBundle = async ( specJson : object , options : { sparqlUrl : string } ) => {
38- // Resolve openapi spec with external references
39- let resolvedSpecJson = await resolveOpenAPISpec ( specJson ) ;
40- const resolvedSpecOrderedMap = fromJS ( resolvedSpecJson ) ;
41-
42- // Extract all data models from spec
43- const dataModels = resolvedSpecOrderedMap . getIn ( [ 'components' , 'schemas' ] ) as Map < any , any > | undefined ;
44- if ( ! dataModels ) {
45- return 'No #/components/schemas models provided' ;
46- }
47-
48- // Calculate specific and global ontoscores
49- let globalOntoScoreModels = 0 ;
50- let globalOntoScoreSum = 0 ;
51-
52- const setOntoscoreValue = ( dataModelKey : string , value : number ) => {
53- resolvedSpecJson [ 'components' ] [ 'schemas' ] [ dataModelKey ] [ 'x-ontoscore' ] = value ;
54- globalOntoScoreSum += value ;
55- globalOntoScoreModels ++ ;
56- } ;
57-
58- // Process every datamodel
59- for ( const [ dataModelKey , dataModel ] of dataModels . entries ( ) ) {
60- // Filter only data models with type "object"
61- const isObject = ( dataModel . get ( 'type' , '' ) as string | undefined ) ?. toLowerCase ( ) === 'object' ;
62- if ( ! isObject ) {
63- continue ;
64- }
65-
66- // Extract x-jsonld-context if present
67- if ( ! dataModel . has ( 'x-jsonld-context' ) ) {
68- setOntoscoreValue ( dataModelKey , 0 ) ;
69- continue ;
70- }
71-
72- // Resolve x-jsonld-context
73- const jsonldContext = resolveJsonldContext ( dataModel ) ?. get ( '@context' ) ;
74- if ( ! jsonldContext ) {
75- setOntoscoreValue ( dataModelKey , 0 ) ;
76- continue ;
77- }
78-
79- // Determine data model's properties to use for ontoscore calculation
80- const propertiesPaths : string [ ] [ ] =
81- dataModel
82- . get ( 'properties' )
83- ?. keySeq ( )
84- . toArray ( )
85- . map ( ( x ) => [ x ] ) || [ ] ;
86-
87- // Determine properties to validate
88- const { valid : validPropertiesPaths , unknown : unknownPropertiesPaths } = await determinePropertiesToValidate (
89- jsonldContext ,
90- propertiesPaths ,
91- ) ;
92-
93- // Execute sparql fetch to check if mapped onto-properties are correct
94- const sparqlResultCount = await fetchValidOntoScorePropertiesCount ( unknownPropertiesPaths , {
95- sparqlUrl : options . sparqlUrl ,
96- } ) ;
97- const semanticPropertiesCount = validPropertiesPaths . length + sparqlResultCount ;
98- const rawPropertiesCount = propertiesPaths ?. length ;
99- const score = rawPropertiesCount > 0 ? semanticPropertiesCount / rawPropertiesCount : 0 ;
100-
101- setOntoscoreValue ( dataModelKey , score ) ;
102- }
103-
104- // Setting global onto score (calculated as an average ontoscore value)
105- if ( ! resolvedSpecJson [ 'info' ] ) {
106- resolvedSpecJson [ 'info' ] = { } ;
107- }
108- resolvedSpecJson [ 'info' ] [ 'x-ontoscore' ] = globalOntoScoreSum / globalOntoScoreModels ;
109-
110- // Normalize x-ref elements
26+ export const createBundle = async ( specJson : object , options : { sparqlUrl : string } ) : Promise < object > => {
27+ let { resolvedSpecJson } = await calculateGlobalOntoscore ( specJson , { sparqlUrl : options . sparqlUrl } ) ;
11128 resolvedSpecJson = normalizeOpenAPISpec ( resolvedSpecJson ) ;
112-
11329 return resolvedSpecJson ;
11430} ;
11531
11632export const ActionsMenu = ( { specSelectors, url, specActions } ) => {
117- const { oasCheckerUrl, schemaEditorUrl, sparqlUrl } = useConfiguration ( ) ;
33+ const { oasCheckerUrl, schemaEditorUrl, sparqlUrl = '' } = useConfiguration ( ) ;
11834
11935 const actions : Array < {
12036 text : string ;
@@ -146,8 +62,8 @@ export const ActionsMenu = ({ specSelectors, url, specActions }) => {
14662 text : 'Download bundle' ,
14763 icon : 'it-download' ,
14864 onClick : async ( ) => {
149- const resolvedSpec = await createBundle ( specSelectors . specJson ( ) . toJS ( ) , { sparqlUrl : sparqlUrl as string } ) ;
150- downloadContent ( yaml . dump ( resolvedSpec ) , 'application/yaml' , 'spec.yaml' ) ;
65+ const resolvedSpecJson = await createBundle ( specSelectors . specJson ( ) . toJS ( ) , { sparqlUrl } ) ;
66+ downloadContent ( yaml . dump ( resolvedSpecJson ) , 'application/yaml' , 'spec.yaml' ) ;
15167 } ,
15268 } ,
15369 {
0 commit comments