diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java index bfe188e5a120..19fa5faac9e7 100644 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java +++ b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java @@ -28,8 +28,6 @@ import io.ballerina.projects.Project; import io.ballerina.projects.util.ProjectUtils; import org.ballerinalang.diagramutil.DiagramUtil; -import org.ballerinalang.observability.anaylze.model.DocumentHolder; -import org.ballerinalang.observability.anaylze.model.ModuleHolder; import org.ballerinalang.observability.anaylze.model.PackageHolder; import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; import org.wso2.ballerinalang.util.RepoUtils; @@ -88,25 +86,10 @@ public void process(Project project) { if (!isObservabilityIncluded) { return; } - Package currentPackage = project.currentPackage(); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - packageCompilation.diagnosticResult(); // Trigger Compilation - - packageHolder.setOrg(currentPackage.packageOrg().toString()); - packageHolder.setName(currentPackage.packageName().toString()); - packageHolder.setVersion(currentPackage.packageVersion().toString()); // Skip this part if project has not updated if (ProjectUtils.isProjectUpdated(project)) { - for (ModuleId moduleId : currentPackage.moduleIds()) { - SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId); - Module module = currentPackage.module(moduleId); - for (DocumentId documentId : module.documentIds()) { - Document document = module.document(documentId); - JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(document, semanticModel); - packageHolder.addSyntaxTree(module.descriptor(), document.name(), syntaxTreeJSON); - } - } + packageHolder.setSyntaxTreeDataString(generateSyntaxTreeJsonString(project)); } } @@ -123,7 +106,7 @@ public void writeToExecutable(Path executableFile, Project project) throws IOExc // Writing Syntax Tree Json String syntaxTreeDataString; if (ProjectUtils.isProjectUpdated(project)) { - syntaxTreeDataString = generateSyntaxTreeJsonString(packageHolder); + syntaxTreeDataString = packageHolder.getSyntaxTreeDataString(); } else { // When project is not updated read Syntax Tree Json from Observability Symbols Jar Path observeJarCachePath = project.targetDir() @@ -173,7 +156,15 @@ private static String bytesToHex(byte[] hash) { return hexString.toString(); } - private String generateSyntaxTreeJsonString(PackageHolder packageHolder) throws IOException { + private String generateSyntaxTreeJsonString(Project project) { + Package currentPackage = project.currentPackage(); + PackageCompilation packageCompilation = currentPackage.getCompilation(); + packageCompilation.diagnosticResult(); // Trigger Compilation + + packageHolder.setOrg(currentPackage.packageOrg().toString()); + packageHolder.setName(currentPackage.packageName().toString()); + packageHolder.setVersion(currentPackage.packageVersion().toString()); + final String ballerinaVersion = RepoUtils.getBallerinaVersion(); StringBuilder jsonStringBuilder = new StringBuilder().append("{\"") .append(BALLERINA_VERSION_KEY).append("\":\"").append(ballerinaVersion).append("\",\"") @@ -182,39 +173,48 @@ private String generateSyntaxTreeJsonString(PackageHolder packageHolder) throws .append(PACKAGE_VERSION_KEY).append("\":\"").append(packageHolder.getVersion()).append("\",\"") .append(PACKAGE_MODULES_KEY).append("\":{"); - String[] moduleKeys = packageHolder.getModules().keySet().toArray(new String[0]); - Arrays.sort(moduleKeys); - for (int i = 0, packageNamesLength = moduleKeys.length; i < packageNamesLength; i++) { - String moduleKey = moduleKeys[i]; - ModuleHolder moduleHolder = packageHolder.getModules().get(moduleKey); - if (i != 0) { - jsonStringBuilder.append(","); - } + ModuleId[] moduleIds = currentPackage.moduleIds().toArray(new ModuleId[0]); + Arrays.sort(moduleIds, (moduleId1, moduleId2) -> { + String moduleName1 = moduleId1.moduleName(); + String moduleName2 = moduleId2.moduleName(); + return moduleName1.compareTo(moduleName2); + }); + + for (ModuleId moduleId : moduleIds) { + SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId); + Module module = currentPackage.module(moduleId); + String moduleName = module.descriptor().name().toString(); - jsonStringBuilder.append("\"").append(moduleKey).append("\":{\"") - .append(MODULE_NAME_KEY).append("\":\"").append(moduleHolder.getName()).append("\",\"") + jsonStringBuilder.append("\"").append(moduleName).append("\":{\"") + .append(MODULE_NAME_KEY).append("\":\"").append(moduleName).append("\",\"") .append(MODULE_DOCUMENTS_KEY).append("\":{"); - String[] documentKeys = moduleHolder.getDocuments().keySet().toArray(new String[0]); - Arrays.sort(documentKeys); - for (int j = 0, documentNamesLength = documentKeys.length; j < documentNamesLength; j++) { - String documentKey = documentKeys[j]; - DocumentHolder documentHolder = moduleHolder.getDocuments().get(documentKey); - String syntaxTreeDataString = documentHolder.getSyntaxTree().toString(); - - if (j != 0) { - jsonStringBuilder.append(","); - } - jsonStringBuilder.append("\"").append(documentKey).append("\":{\"") + DocumentId[] documentIds = module.documentIds().toArray(new DocumentId[0]); + Arrays.sort(documentIds, (documentId1, documentId2) -> { + String documentName1 = module.document(documentId1).name(); + String documentName2 = module.document(documentId2).name(); + return documentName1.compareTo(documentName2); + }); + + for (DocumentId documentId : documentIds) { + Document document = module.document(documentId); + JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(document, semanticModel); + + jsonStringBuilder.append("\"").append(document.name()).append("\":{\"") .append(DOCUMENT_NAME_KEY).append("\":\"") - .append(documentHolder.getDocumentName()).append("\",\"") + .append(document.name()).append("\",\"") .append(DOCUMENT_SYNTAX_TREE_KEY).append("\":") - .append(syntaxTreeDataString) - .append("}"); + .append(syntaxTreeJSON.toString()) + .append("}") + .append(","); } - jsonStringBuilder.append("}}"); + jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1); + jsonStringBuilder.append("}}") + .append(","); } + jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1); + return jsonStringBuilder.append("}}").toString(); } } diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/DocumentHolder.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/DocumentHolder.java deleted file mode 100644 index 51706f192f28..000000000000 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/DocumentHolder.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.observability.anaylze.model; - -import com.google.gson.JsonElement; - -/** - * Holds data related to a document. - * - * @since 2.0.0 - */ -public class DocumentHolder { - private final String documentName; - private final JsonElement syntaxTree; - - DocumentHolder(String documentName, JsonElement syntaxTree) { - this.documentName = documentName; - this.syntaxTree = syntaxTree; - } - - public String getDocumentName() { - return documentName; - } - - public JsonElement getSyntaxTree() { - return syntaxTree; - } -} diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/ModuleHolder.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/ModuleHolder.java deleted file mode 100644 index 6bcac6adbeff..000000000000 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/ModuleHolder.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.observability.anaylze.model; - -import com.google.gson.JsonElement; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * Holds data related to a module. - * - * @since 2.0.0 - */ -public class ModuleHolder { - private final String name; - private final Map documentsMap = new HashMap<>(); - - ModuleHolder(String name) { - this.name = name; - } - - public void addSyntaxTree(String documentName, JsonElement syntaxTree) { - this.documentsMap.put(documentName, new DocumentHolder(documentName, syntaxTree)); - } - - public String getName() { - return name; - } - - public Map getDocuments() { - return Collections.unmodifiableMap(documentsMap); - } -} diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java index 0ffcead98d71..6b8e5568b5ff 100644 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java +++ b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java @@ -17,12 +17,6 @@ */ package org.ballerinalang.observability.anaylze.model; -import com.google.gson.JsonElement; -import io.ballerina.projects.ModuleDescriptor; - -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Hold data related to a package. @@ -34,16 +28,10 @@ public class PackageHolder { private String org; private String name; private String version; - private final Map modulesMap = new ConcurrentHashMap<>(); - - public void addSyntaxTree(ModuleDescriptor moduleDescriptor, String documentName, JsonElement syntaxTreeJson) { - String moduleName = moduleDescriptor.name().toString(); - ModuleHolder moduleHolder = this.modulesMap.computeIfAbsent(moduleName, k -> new ModuleHolder(moduleName)); - moduleHolder.addSyntaxTree(documentName, syntaxTreeJson); - } + private String syntaxTreeDataString; - public Map getModules() { - return Collections.unmodifiableMap(modulesMap); + public void setSyntaxTreeDataString(String syntaxTreeDataString) { + this.syntaxTreeDataString = syntaxTreeDataString; } public String getOrg() { @@ -69,4 +57,8 @@ public String getVersion() { public void setVersion(String version) { this.version = version; } + + public String getSyntaxTreeDataString() { + return syntaxTreeDataString; + } }