<commit_before>import { Id, Params, ServiceMethods } from '@feathersjs/feathers'
import { Application } from '../../declarations'
import { Structure as IStructure } from '../../insights/definitions.d'
import getStructure from '../../insights/structure'
import { SubsetData } from '../subsets/subsets.class'

interface ServiceOptions {}

function applySubsetToStructure (structure: IStructure, subset: SubsetData) {
  const { selection = {}, addNewModels, addNewFields, newFields = {} } = subset

  const newStructure = {}

  Object.entries(structure).forEach(([modelKey, modelStructure]) => {
    if (selection[modelKey]) {
      const newModel = {
        ...modelStructure,
        links: {},
        columns: {},
        custom: {}
      };

      (['links', 'columns', 'custom']).forEach(typeKey => {
        if (modelStructure[typeKey]) {
          Object.entries(modelStructure[typeKey]).forEach(([fieldKey, fieldData]) => {
            if (
              selection[modelKey][fieldKey] === true ||
              (typeof selection[modelKey][fieldKey] === 'undefined' && addNewFields)
            ) {
              newModel[typeKey][fieldKey] = fieldData
            }
          })
        }
      })

      if (newFields[modelKey]) {
        Object.values(newFields[modelKey]).forEach(field => {
          if (field.type === 'custom') {
            newModel.custom[field.key] = field.meta
          }
          if (field.type === 'column') {
            newModel.columns[field.key] = field.meta
          }
          if (field.type === 'link') {
            newModel.links[field.key] = field.meta
          }
        })
      }

      newStructure[modelKey] = newModel
    } else if (typeof selection[modelKey] === 'undefined' && addNewModels) {
      newStructure[modelKey] = modelStructure
    }
  })

  return newStructure
}

export class Structure implements Partial<ServiceMethods<IStructure>> {
  app: Application;
  options: ServiceOptions;
<commit_msg>add edit changes to the structure<commit_after>import { Id, Params, ServiceMethods } from '@feathersjs/feathers'
import { Application } from '../../declarations'
import { Structure as IStructure } from '../../insights/definitions.d'
import getStructure from '../../insights/structure'
import { SubsetData } from '../subsets/subsets.class'

interface ServiceOptions {}

function applySubsetToStructure (structure: IStructure, subset: SubsetData) {
  const { selection = {}, addNewModels, addNewFields, newFields = {}, editedFields = {} } = subset

  const newStructure = {}

  Object.entries(structure).forEach(([modelKey, modelStructure]) => {
    if (selection[modelKey]) {
      const newModel = {
        ...modelStructure,
        links: {},
        columns: {},
        custom: {}
      };

      const skipOldEditedFields = editedFields[modelKey] ? Object.keys(editedFields[modelKey]) : [];

      (['links', 'columns', 'custom']).forEach(typeKey => {
        if (modelStructure[typeKey]) {
          Object.entries(modelStructure[typeKey]).forEach(([fieldKey, fieldData]) => {
            if (
              !skipOldEditedFields.includes(fieldKey) && (
                selection[modelKey][fieldKey] === true ||
                (typeof selection[modelKey][fieldKey] === 'undefined' && addNewFields)
              )
            ) {
              newModel[typeKey][fieldKey] = fieldData
            }
          })
        }
      })

      // const newFields = Object.values(editedFields[modelKey] || []).concat(Object.values(newFields[modelKey]) || [])

      if (editedFields[modelKey]) {
        Object.values(editedFields[modelKey]).forEach(field => {
          if (field.type === 'custom') {
            newModel.custom[field.key] = field.meta
          }
          if (field.type === 'column') {
            newModel.columns[field.key] = field.meta
          }
          if (field.type === 'link') {
            newModel.links[field.key] = field.meta
          }
        })
      }

      if (newFields[modelKey]) {
        Object.values(newFields[modelKey]).forEach(field => {
          if (field.type === 'custom') {
            newModel.custom[field.key] = field.meta
          }
          if (field.type === 'column') {
            newModel.columns[field.key] = field.meta
          }
          if (field.type === 'link') {
            newModel.links[field.key] = field.meta
          }
        })
      }

      newStructure[modelKey] = newModel
    } else if (typeof selection[modelKey] === 'undefined' && addNewModels) {
      newStructure[modelKey] = modelStructure
    }
  })

  return newStructure
}

export class Structure implements Partial<ServiceMethods<IStructure>> {
  app: Application;
  options: ServiceOptions;

{'fraction_non_alphanumeric': 0.14431455897980872, 'fraction_numerical': 0.0, 'mean_word_length': 2.383177570093458, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '3139173', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1285, 'n_words': 243}