<commit_before>
module Jmx {
  export function MBeansController($scope, $location: ng.ILocationService, workspace: Workspace) {
    $scope.workspace = workspace;
    $scope.tree = new Folder('MBeans');
    $scope.counter = 0;

    $scope.$on("$routeChangeSuccess", function (event, current, previous) {
      // lets do this asynchronously to avoid Error: $digest already in progress
      setTimeout(updateSelectionFromURL, 50);
    });

    // we could use $scope.$on and $rootScope.$broadCast here but it seems watching
    // an expression may be a little more efficient and use less event broadcasting around controllers
    $scope.$watch('workspace.operationCounter', function () {
      $scope.counter += 1;
      loadTree();
      //setTimeout(loadTree, 1);
    });

    $scope.select = (node:DynaTreeNode) => {
      $scope.workspace.updateSelectionNode(node);
      $scope.$apply();
    };

    function updateSelectionFromURL() {
      updateTreeSelectionFromURL($location, $("#jmxtree"));
    }

    function folderGetOrElse(folder, value) {
      if (folder) {
        try {
          return folder.getOrElse(value);
        } catch (e) {
          console.log("Failed to find value " + value + " on folder " + folder);
        }
      }
      return null;
    }

    function populateTree(response) {
      var rootId = 'root';
      var separator = '_';
      workspace.mbeanTypesToDomain = {};
      var tree = new Folder('MBeans');
      tree.key = rootId;
      var domains = response.value;
      for (var domain in domains) {
        var domainClass = escapeDots(domain);
        var mbeans = domains[domain];
        for (var path in mbeans) {
          var entries = {};
          var folder = folderGetOrElse(tree, domain);
          //if (!folder) continue;
          folder.domain = domain;
          if (!folder.key) {
            folder.key = rootId + separator + domain;
          }
          var folderNames = [domain];
          folder.folderNames = folderNames;
          folderNames = folderNames.clone();
          var items = path.split(',');
          var paths = [];
          var typeName = null;
          items.forEach(item => {
            var kv = item.split('=');
            var key = kv[0];
            var value = kv[1] || key;
            entries[key] = value;
            var moveToFront = false;
            if (key.toLowerCase() === "type") {
              typeName = value;
              // if the type name value already exists in the root node
              // of the domain then lets move this property around too
              if (folder.map[value]) {
                moveToFront = true;
              }
            }
            if (moveToFront) {
              paths.splice(0, 0, value);
            } else {
              paths.push(value);
            }
          });

          function configureFolder(folder: Folder, name: string) {
            folder.domain = domain;
            folder.key = rootId + separator + folderNames.join(separator);
            folder.folderNames = folderNames.clone();
            var classes = escapeDots(folder.key);
            var kindName = folderNames.last() || typeName;
            if (folder.parent && folder.parent.title === typeName) {
              kindName = typeName;
            } else if (kindName === name) {
              kindName += "_Folder";
            }
            if (kindName) {
              classes += " " + domainClass + separator + kindName;
            }
            folder.addClass = classes;
            return folder;
          }

          var lastPath = paths.pop();
          paths.forEach(value => {
            folder = folderGetOrElse(folder, value);
            if (folder) {
              folderNames.push(value);
              configureFolder(folder, value);
            }
          });
          var key = rootId + separator + folderNames.join(separator) + separator + lastPath;
          var objectName = domain + ":" + path;
/*
          var mbeanInfo:NodeSelection = {
            key: key,
            title: trimQuotes(lastPath),
            domain: domain,
            path: path,
            paths: paths,
            objectName: objectName,
            parent: folder,
            entries: entries,
            typeName: typeName,
            addClass: escapeDots(key),
            get: (key:string) => null
          };
*/

          if (folder) {
            folder = folderGetOrElse(folder, lastPath);
            if (folder) {
              // lets add the various data into the folder
              configureFolder(folder, lastPath);
              folder.entries = entries;
              folder.key = key;
              folder.title = trimQuotes(lastPath);
              folder.objectName = objectName;
              folder.typeName = typeName;

              var mbeanInfo = folder;
              if (typeName) {
                var map = workspace.mbeanTypesToDomain[typeName];
                if (!map) {
                  map = {};
                  workspace.mbeanTypesToDomain[typeName] = map;
                }
                var value = map[domain];
                if (!value) {
                  map[domain] = mbeanInfo;
                } else {
                  var array = null;
                  if (angular.isArray(value)) {
                    array = value;
                  } else {
                    array = [value];
                    map[domain] = array;
                  }
                  array.push(mbeanInfo);
                }
              }
            }
          } else {
            console.log("No folder found for lastPath: " + lastPath);
          }
        }
      }
      // TODO we should do a merge across...
      // so we only insert or delete things!
      $scope.tree = tree;
      if ($scope.workspace) {
        $scope.workspace.tree = tree;
      }
      $scope.$apply();
      console.log("Loaded tree!");

      var treeElement = $("#jmxtree");
      enableTree($scope, $location, workspace, treeElement, $scope.workspace.tree.children);

      if (treeElement.length) {
        if ($scope.counter > 1) {
          //console.log("Reloading the tree as counter is " + $scope.counter);
          treeElement.dynatree("getTree").reload();
        }
      }
      // TODO should this be async?
      //updateSelectionFromURL();
    }

    function loadTree() {
      var jolokia = workspace.jolokia;
      jolokia.request(
              {type: 'list'},
              onSuccess(populateTree, {canonicalNaming: false, maxDepth: 2}));
    }<commit_msg>fixed tree updating after creating new destinations<commit_after>
module Jmx {
  export function MBeansController($scope, $location: ng.ILocationService, workspace: Workspace) {
    $scope.workspace = workspace;
    $scope.tree = new Folder('MBeans');
    $scope.counter = 0;

    $scope.$on("$routeChangeSuccess", function (event, current, previous) {
      // lets do this asynchronously to avoid Error: $digest already in progress
      setTimeout(updateSelectionFromURL, 50);
    });

    // we could use $scope.$on and $rootScope.$broadCast here but it seems watching
    // an expression may be a little more efficient and use less event broadcasting around controllers
    $scope.$watch('workspace.operationCounter', function () {
      $scope.counter += 1;
      loadTree();
      //setTimeout(loadTree, 1);
    });

    $scope.select = (node:DynaTreeNode) => {
      $scope.workspace.updateSelectionNode(node);
      $scope.$apply();
    };

    function updateSelectionFromURL() {
      updateTreeSelectionFromURL($location, $("#jmxtree"));
    }

    function folderGetOrElse(folder, value) {
      if (folder) {
        try {
          return folder.getOrElse(value);
        } catch (e) {
          console.log("Failed to find value " + value + " on folder " + folder);
        }
      }
      return null;
    }

    function populateTree(response) {
      var rootId = 'root';
      var separator = '_';
      workspace.mbeanTypesToDomain = {};
      var tree = new Folder('MBeans');
      tree.key = rootId;
      var domains = response.value;
      for (var domain in domains) {
        var domainClass = escapeDots(domain);
        var mbeans = domains[domain];
        for (var path in mbeans) {
          var entries = {};
          var folder = folderGetOrElse(tree, domain);
          //if (!folder) continue;
          folder.domain = domain;
          if (!folder.key) {
            folder.key = rootId + separator + domain;
          }
          var folderNames = [domain];
          folder.folderNames = folderNames;
          folderNames = folderNames.clone();
          var items = path.split(',');
          var paths = [];
          var typeName = null;
          items.forEach(item => {
            var kv = item.split('=');
            var key = kv[0];
            var value = kv[1] || key;
            entries[key] = value;
            var moveToFront = false;
            if (key.toLowerCase() === "type") {
              typeName = value;
              // if the type name value already exists in the root node
              // of the domain then lets move this property around too
              if (folder.map[value]) {
                moveToFront = true;
              }
            }
            if (moveToFront) {
              paths.splice(0, 0, value);
            } else {
              paths.push(value);
            }
          });

          function configureFolder(folder: Folder, name: string) {
            folder.domain = domain;
            folder.key = rootId + separator + folderNames.join(separator);
            folder.folderNames = folderNames.clone();
            var classes = escapeDots(folder.key);
            var kindName = folderNames.last() || typeName;
            if (folder.parent && folder.parent.title === typeName) {
              kindName = typeName;
            } else if (kindName === name) {
              kindName += "_Folder";
            }
            if (kindName) {
              classes += " " + domainClass + separator + kindName;
            }
            folder.addClass = classes;
            return folder;
          }

          var lastPath = paths.pop();
          paths.forEach(value => {
            folder = folderGetOrElse(folder, value);
            if (folder) {
              folderNames.push(value);
              configureFolder(folder, value);
            }
          });
          var key = rootId + separator + folderNames.join(separator) + separator + lastPath;
          var objectName = domain + ":" + path;
/*
          var mbeanInfo:NodeSelection = {
            key: key,
            title: trimQuotes(lastPath),
            domain: domain,
            path: path,
            paths: paths,
            objectName: objectName,
            parent: folder,
            entries: entries,
            typeName: typeName,
            addClass: escapeDots(key),
            get: (key:string) => null
          };
*/

          if (folder) {
            folder = folderGetOrElse(folder, lastPath);
            if (folder) {
              // lets add the various data into the folder
              configureFolder(folder, lastPath);
              folder.entries = entries;
              folder.key = key;
              folder.title = trimQuotes(lastPath);
              folder.objectName = objectName;
              folder.typeName = typeName;

              var mbeanInfo = folder;
              if (typeName) {
                var map = workspace.mbeanTypesToDomain[typeName];
                if (!map) {
                  map = {};
                  workspace.mbeanTypesToDomain[typeName] = map;
                }
                var value = map[domain];
                if (!value) {
                  map[domain] = mbeanInfo;
                } else {
                  var array = null;
                  if (angular.isArray(value)) {
                    array = value;
                  } else {
                    array = [value];
                    map[domain] = array;
                  }
                  array.push(mbeanInfo);
                }
              }
            }
          } else {
            console.log("No folder found for lastPath: " + lastPath);
          }
        }
      }
      // TODO we should do a merge across...
      // so we only insert or delete things!
      $scope.tree = tree;
      if ($scope.workspace) {
        $scope.workspace.tree = tree;
      }
      $scope.$apply();
      console.log("Loaded tree!");

      var treeElement = $("#jmxtree");
      enableTree($scope, $location, workspace, treeElement, $scope.workspace.tree.children);

      if (treeElement.length) {
        if ($scope.counter > 1) {
          //console.log("Reloading the tree as counter is " + $scope.counter);
          treeElement.dynatree("getTree").reload();
        }
      }
      // lets do this asynchronously to avoid Error: $digest already in progress
      setTimeout(updateSelectionFromURL, 50);
    }

    function loadTree() {
      var jolokia = workspace.jolokia;
      jolokia.request(
              {type: 'list'},
              onSuccess(populateTree, {canonicalNaming: false, maxDepth: 2}));
    }
{'fraction_non_alphanumeric': 0.11101842723894745, 'fraction_numerical': 0.0018199742170319254, 'mean_word_length': 1.5627671978235522, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6638962', 'n_tokens_mistral': 3497, 'n_tokens_neox': 3373, 'n_words': 883}