<commit_before>import {
  SendEndpoint,
  ReceiveEndpoint,
  SendEndpointDefault
} from 'kronos-endpoint';
import { Service } from 'kronos-service';
import { makeLogEvent } from 'loglevel-mixin';
import { mergeAttributes, createAttributes } from 'model-attributes';

export class Step extends Service {
  static get configurationAttributes() {
    return mergeAttributes(
      createAttributes({
        // TODO move into service
        description: {
          type: 'string'
        }
      }),
      Service.configurationAttributes
    );
  }

  static get name() {
    return 'kronos-step';
  }

  /**
   * This method could be overwritten by a derived object to setup default endpoints.
   * This method may be overwritten by derived classes.
   * This will be called by the initialize method
   * @param {Object} stepConfiguration The default step configuration
   * @api protected
   */
  createPredefinedEndpoints(stepConfiguration) {
    if (this.endpoints.log) return;

    const logEndpoint = new SendEndpointDefault('log', this);
    if (this.manager.services && this.manager.services.logger) {
      logEndpoint.connected = this.manager.services.logger.endpoints.log;
    }
    this.addEndpoint(logEndpoint);
  }

  /**
   * Creates the endpoint objects defined as a combination from
   * implementation and definition
   * @param {Object} def The step configuration
   * @api protected
   */
  createEndpoints(def) {
    if (def && def.endpoints) {
      Object.keys(def.endpoints).forEach(name =>
        this.createEndpoint(name, def.endpoints[name])
      );
    }
  }

  /**
   * Deliver the endpoint options for a given endpoint definition.
   * @return {Object} suiable to pass as options to the endpoint factory
   */
  endpointOptions(name, def) {
    let options = {};

    if (def.opposite) {
      options.createOpposite = true;
    }

    return options;
  }

  createEndpoint(name, def) {
    let ep;

    if (def.in)
      ep = new ReceiveEndpoint(name, this, this.endpointOptions(name, def));
    if (def.out)
      ep = new SendEndpoint(name, this, this.endpointOptions(name, def));

    this.addEndpoint(ep);

    if (def.interceptors) {
      ep.interceptors = def.interceptors.map(icDef =>
        this.manager.createInterceptorInstanceFromConfig(icDef, ep)
      );
    }
  }

  /**
   * @return {String} separator between step name and endpoint name
   **/
  get endpointParentSeparator() {
    return '/';
  }

  log(level, arg) {
    const logevent = makeLogEvent(level, arg, {
      'step-type': this.type,
      'step-name': this.name
    });

    if (this.endpoints.log && this.endpoints.log.isConnected) {
      this.endpoints.log.receive(logevent);
    } else {
      console.log(logevent);
    }
  }
}<commit_msg>refactor: use more functionalty from Service class<commit_after>import {
  SendEndpoint,
  ReceiveEndpoint,
  SendEndpointDefault
} from 'kronos-endpoint';
import { Service } from 'kronos-service';
import { makeLogEvent } from 'loglevel-mixin';
import { mergeAttributes, createAttributes } from 'model-attributes';

export class Step extends Service {
  static get configurationAttributes() {
    return mergeAttributes(
      createAttributes({
        // TODO move into service
        description: {
          type: 'string'
        }
      }),
      Service.configurationAttributes
    );
  }

  static get name() {
    return 'kronos-step';
  }

  /**
   * Creates the endpoint objects defined as a combination from
   * implementation and definition
   * @param {Object} def The step configuration
   * @api protected
   */
  createEndpoints(def) {
    if (def && def.endpoints) {
      Object.keys(def.endpoints).forEach(name =>
        this.createEndpoint(name, def.endpoints[name])
      );
    }
  }

  /**
   * Deliver the endpoint options for a given endpoint definition.
   * @return {Object} suiable to pass as options to the endpoint factory
   */
  endpointOptions(name, def) {
    let options = {};

    if (def.opposite) {
      options.createOpposite = true;
    }

    return options;
  }

  createEndpoint(name, def) {
    let ep;

    if (def.in)
      ep = new ReceiveEndpoint(name, this, this.endpointOptions(name, def));
    if (def.out)
      ep = new SendEndpoint(name, this, this.endpointOptions(name, def));

    this.addEndpoint(ep);

    if (def.interceptors) {
      ep.interceptors = def.interceptors.map(icDef =>
        this.manager.createInterceptorInstanceFromConfig(icDef, ep)
      );
    }
  }

  /**
   * @return {String} separator between step name and endpoint name
   **/
  get endpointParentSeparator() {
    return '/';
  }

  log(level, arg) {
    this.endpoints.log.receive(
      makeLogEvent(level, arg, {
        'step-type': this.type,
        'step-name': this.name
      })
    );
  }
}
{'fraction_non_alphanumeric': 0.11233847436431847, 'fraction_numerical': 0.0, 'mean_word_length': 2.811755361397935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6831934', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1399, 'n_words': 362}