<commit_before>'use strict';

const Schema = require('joyent-cp-gql-schema');
const Graphi = require('graphi');
const Hoek = require('hoek');
const Piloted = require('piloted');
const Data = require('./data');
const Pack = require('../package.json');
const Resolvers = require('./resolvers');
const ContainerPilotWatcher = require('./watch/container-pilot');
const MachinesWatcher = require('./watch/machines');


const internals = {
  namespace: process.env.NAMESPACE ? `/${process.env.NAMESPACE}` : '',
  defaults: {
    data: {
      db: {}
    },
    watch: {
      url: process.env.SDC_URL,
      account: process.env.SDC_ACCOUNT,
      keyId: process.env.SDC_KEY_ID
    }
  }
};

module.exports = function (server, options, next) {
  const settings = Hoek.applyToDefaults(internals.defaults, options || {});

  try {
    const docker = Piloted.service('docker-compose-api');
    if (docker) {
      settings.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
    }

    const rethinkdb = Piloted.service('rethinkdb');
    if (rethinkdb) {
      settings.data.db.host = rethinkdb.address;
    }
  } catch (ex) {
    server.log(['error'], ex);
  }

  settings.watch.server = server;
  settings.data.server = server;

  const data = new Data(settings.data);
  const cpWatcher = new ContainerPilotWatcher(Object.assign(settings.watch, { data }));
  const machinesWatcher = new MachinesWatcher(Object.assign(settings.watch, { data }));

  // watcher <-> watcher
  // portal depends on watcher and vice-versa
  // I'm sure there is a better way to organize this domains
  // but this works for now
  data.setMachinesWatcher(machinesWatcher);

  cpWatcher.on('error', (err) => {
    server.log(['error'], err);
  });

  data.on('error', (err) => {
    server.log(['error'], err);
  });

  data.connect((err) => {
    if (err) {
      return next(err);
    }

    server.bind(data);

    Piloted.on('refresh', internals.refresh(data));

    machinesWatcher.poll();
    cpWatcher.poll();

    server.register([
      {
        register: Graphi,
        options: {
          graphqlPath: `${internals.namespace}/graphql`,
          graphiqlPath: `${internals.namespace}/graphiql`,
          schema: Schema,
          resolvers: Resolvers(data)
        }
      }
    ]);

    next();
  });
};

module.exports.attributes = {
  name: Pack.name,
  version: Pack.version,
  once: true,
  multiple: false
};


internals.refresh = function (data) {
  return () => {
    const docker = Piloted.service('docker-compose-api');
    if (docker) {
      data.reconnectCompose(`tcp://${docker.address}:${docker.port}`);
    }

    const rethinkdb = Piloted.service('rethinkdb');
    if (rethinkdb) {
      data.reconnectDb({ host: rethinkdb.address });
    }
  };
};
<commit_msg>fix(portal-api): default rethinkdb host to localhost<commit_after>'use strict';

const Schema = require('joyent-cp-gql-schema');
const Graphi = require('graphi');
const Hoek = require('hoek');
const Piloted = require('piloted');
const Data = require('./data');
const Pack = require('../package.json');
const Resolvers = require('./resolvers');
const ContainerPilotWatcher = require('./watch/container-pilot');
const MachinesWatcher = require('./watch/machines');


const internals = {
  namespace: process.env.NAMESPACE ? `/${process.env.NAMESPACE}` : '',
  defaults: {
    data: {
      db: {
        host: 'localhost'
      }
    },
    watch: {
      url: process.env.SDC_URL,
      account: process.env.SDC_ACCOUNT,
      keyId: process.env.SDC_KEY_ID
    }
  }
};

module.exports = function (server, options, next) {
  const settings = Hoek.applyToDefaults(internals.defaults, options || {});

  try {
    const docker = Piloted.service('docker-compose-api');
    if (docker) {
      settings.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
    }

    const rethinkdb = Piloted.service('rethinkdb');
    if (rethinkdb) {
      settings.data.db.host = rethinkdb.address;
    }
  } catch (ex) {
    server.log(['error'], ex);
  }

  settings.watch.server = server;
  settings.data.server = server;

  const data = new Data(settings.data);
  const cpWatcher = new ContainerPilotWatcher(Object.assign(settings.watch, { data }));
  const machinesWatcher = new MachinesWatcher(Object.assign(settings.watch, { data }));

  // watcher <-> watcher
  // portal depends on watcher and vice-versa
  // I'm sure there is a better way to organize this domains
  // but this works for now
  data.setMachinesWatcher(machinesWatcher);

  cpWatcher.on('error', (err) => {
    server.log(['error'], err);
  });

  data.on('error', (err) => {
    server.log(['error'], err);
  });

  data.connect((err) => {
    if (err) {
      return next(err);
    }

    server.bind(data);

    Piloted.on('refresh', internals.refresh(data));

    machinesWatcher.poll();
    cpWatcher.poll();

    server.register([
      {
        register: Graphi,
        options: {
          graphqlPath: `${internals.namespace}/graphql`,
          graphiqlPath: `${internals.namespace}/graphiql`,
          schema: Schema,
          resolvers: Resolvers(data)
        }
      }
    ]);

    next();
  });
};

module.exports.attributes = {
  name: Pack.name,
  version: Pack.version,
  once: true,
  multiple: false
};


internals.refresh = function (data) {
  return () => {
    const docker = Piloted.service('docker-compose-api');
    if (docker) {
      data.reconnectCompose(`tcp://${docker.address}:${docker.port}`);
    }

    const rethinkdb = Piloted.service('rethinkdb');
    if (rethinkdb) {
      data.reconnectDb({ host: rethinkdb.address });
    }
  };
};

{'fraction_non_alphanumeric': 0.16405972271596161, 'fraction_numerical': 0.0, 'mean_word_length': 3.071635311143271, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5302872', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1819, 'n_words': 353}