<commit_before>/*
 * Copyright 2018 Crown Copyright
 *
 * Licensed 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.
 */

import { findItem, findMatch, iterateNodes } from 'lib/treeUtils';

/**
 * This function takes the denormalised pipeline definition and creates
 * a tree like structure. The root element is the 'from' of the first link.
 * This tree structure can then be used to lay the elements out in the graphical display.
 *
 * @param {object} pipeline
 * @return {object} Tree like structure
 */
export function getPipelineAsTree(pipeline) {
  const elements = {};

  // Put all the elements into an object, keyed on id
  pipeline.elements.add.forEach((e) => {
    elements[e.id] = {
      uuid: e.id,
      type: e.type,
      children: [],
    };
  });

  // Create the tree using links
  pipeline.links.add.forEach((l) => {
    elements[l.from].children.push(elements[l.to]);
  });

  // Figure out the root -- if a link doesn't have anything going to it then it's the root.
  const roots = pipeline.links.add.filter((fromLink) => {
    const toLinks = pipeline.links.add.filter(l => fromLink.from === l.to);
    return toLinks.length === 0;
  });
  let rootId;
  if (roots.length === 0) {
    // Maybe there's only one thing and therefore no links?
    rootId = pipeline.elements.add[0].id;
  } else {
  rootId = roots[0].from;
  }

  return elements[rootId];
}

export const ORIENTATION = {<commit_msg>Guard against there not being any elements<commit_after>/*
 * Copyright 2018 Crown Copyright
 *
 * Licensed 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.
 */

import { findItem, findMatch, iterateNodes } from 'lib/treeUtils';

/**
 * This function takes the denormalised pipeline definition and creates
 * a tree like structure. The root element is the 'from' of the first link.
 * This tree structure can then be used to lay the elements out in the graphical display.
 *
 * @param {object} pipeline
 * @return {object} Tree like structure
 */
export function getPipelineAsTree(pipeline) {
  const elements = {};

  // Put all the elements into an object, keyed on id
  pipeline.elements.add.forEach((e) => {
    elements[e.id] = {
      uuid: e.id,
      type: e.type,
      children: [],
    };
  });

  // Create the tree using links
  pipeline.links.add.forEach((l) => {
    elements[l.from].children.push(elements[l.to]);
  });

  // Figure out the root -- if a link doesn't have anything going to it then it's the root.
  const rootLinks = pipeline.links.add.filter((fromLink) => {
    const toLinks = pipeline.links.add.filter(l => fromLink.from === l.to);
    return toLinks.length === 0;
  });

  let rootId;
  if (rootLinks.length === 0) {
    // Maybe there's only one thing and therefore no links?
    if (pipeline.elements.add.length !== 0) {
      // If there're no links then we can use the first element.
      rootId = pipeline.elements.add[0].id;
    } else {
      // If there are no elements then we can't have a root node.
      rootId = undefined;
    }
  } else {
    rootId = rootLinks[0].from;
  }

  return rootId ? elements[rootId] : undefined;
}

export const ORIENTATION = {
{'fraction_non_alphanumeric': 0.10800291899781075, 'fraction_numerical': 0.006081245439065921, 'mean_word_length': 3.195918367346939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3398900', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1155, 'n_words': 471}