<commit_before>'use strict';
/* eslint-env mocha */

const pkg = require('./../package.json');
const childProcess = require('child_process');
const Ghostscript = require('./../src/ghostscript');
const chai = require('chai');
const expect = chai.expect;
const path = require('path');

describe(pkg.name + '/src/ghostscript.js', () => {
  describe('#constructor', () => {
    it('should initialize some attributes', () => {
      let gs = new Ghostscript();
      expect(gs.options.length).to.equal(0);
      expect(gs._input).to.deep.equal([]);
    });
  });
  describe('#nopause', () => {
    it('should set nopause option', () => {
      const gs = new Ghostscript();
      expect(gs.nopause().options[0]).to.equal('-dNOPAUSE');
    });
  });
  describe('#quiet', () => {
    it('should set quiet option', () => {
      const gs = new Ghostscript();
      expect(gs.quiet().options[0]).to.equal('-q');
    });
  });
  describe('#batch', () => {
    it('should set batch option', () => {
      const gs = new Ghostscript();
      expect(gs.batch().options[0]).to.equal('-dBATCH');
    });
  });
  describe('#interpolate', () => {
    it('should set interpolate option', () => {
      const gs = new Ghostscript();
      expect(gs.interpolate().options[0]).to.equal('-dINTERPOLATE');
    });
  });
  describe('#ram', () => {
    it('should set ram default option (tiff24nc)', () => {
      const gs = new Ghostscript();
      expect(gs.ram().lastOption[0]).to.equal('-c "30000000 setvmthreshold" -f');
    });
    it('should set ram option', (done) => {
      const gs = new Ghostscript();
      expect(gs.ram(123456789).lastOption[0]).to.equal('-c "123456789 setvmthreshold" -f');
      done();
    });
  });
  describe('#device', () => {
    it('should set device default option (tiff24nc)', () => {
      const gs = new Ghostscript();
      expect(gs.device().options[0]).to.equal('-sDEVICE=tiff24nc');
    });
    it('should set device option', (done) => {
      const gs = new Ghostscript();
      expect(gs.device('jpeg').options[0]).to.equal('-sDEVICE=jpeg');
      done();
    });
  });
  describe('#resolution', () => {
    it('should set resolution default option (300)', () => {
      const gs = new Ghostscript();
      expect(gs.resolution().options[0]).to.equal('-r300');
    });
    it('should set resolution option', () => {
      const gs = new Ghostscript();
      expect(gs.resolution(600).options[0]).to.equal('-r600');
    });
  });
  describe('#firstPage', () => {
    it('should set firstPage option', () => {
      const gs = new Ghostscript();
      expect(gs.firstPage(1).options[0]).to.equal('-dFirstPage=1');
    });
  });
  describe('#lastPage', () => {
    it('should set lastPage option', () => {
      const gs = new Ghostscript();
      expect(gs.lastPage(1).options[0]).to.equal('-dLastPage=1');
    });
  });
  describe('#input', () => {
    it('should set input option', () => {
      const gs = new Ghostscript();
      expect(gs.input('fileInput.pdf')._input).to.deep.equal(['fileInput.pdf']);
    });
  });
  describe('#output', () => {
    it('should set output option', () => {
      const gs = new Ghostscript();
      expect(gs.output('fileOutput.tiff').options[0]).to.equal('-sOutputFile=fileOutput.tiff');
    });
  });
  describe('#compatibility', () => {
    it('should set compatibility option', () => {
      const gs = new Ghostscript();
      expect(gs.compatibility(1.4).options[0]).to.equal('-dCompatibility=1.4');
    });
  });
  describe('#pdfsettings', () => {
    it('should set pdfsettings default option', () => {
      const gs = new Ghostscript();
      expect(gs.pdfsettings('/ebook').options[0]).to.equal('-dPDFSETTINGS=/ebook');
    });
  });
  describe('#autoRotatePages', () => {
    it('should set autoRotatePages default option (All)', () => {
      const gs = new Ghostscript();
      expect(gs.autoRotatePages().options[0]).to.equal('-dAutoRotatePages=/All');
    });

    it('should set autoRotatePages option', () => {
      const gs = new Ghostscript();
      expect(gs.autoRotatePages('PageByPage').options[0]).to.equal('-dAutoRotatePages=/PageByPage');
    });
  });
  describe('#antiAliasColorImage', () => {
    it('should set antiAliasColorImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasColorImage(false).options[0]).to.equal('-dAntiAliasColorImage=false');
    });

    it('should set antiAliasColorImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasColorImage('false').options[0]).to.equal('-dAntiAliasColorImage=false');
    });
  });
  describe('#antiAliasGrayImage', () => {
    it('should set antiAliasGrayImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasGrayImage(false).options[0]).to.equal('-dAntiAliasGrayImage=false');
    });

    it('should set antiAliasGrayImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasGrayImage('false').options[0]).to.equal('-dAntiAliasGrayImage=false');
    });
  });
  describe('#antiAliasMonoImage', () => {
    it('should set antiAliasMonoImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasMonoImage(false).options[0]).to.equal('-dAntiAliasMonoImage=false');
    });

    it('should set antiAliasMonoImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasMonoImage('false').options[0]).to.equal('-dAntiAliasMonoImage=false');
    });
  });
  describe('#autoFilterColorImages', () => {
    it('should set autoFilterColorImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterColorImages(false).options[0]).to.equal('-dAutoFilterColorImages=false');
    });

    it('should set autoFilterColorImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterColorImages('false').options[0]).to.equal('-dAutoFilterColorImages=false');
    });
  });
  describe('#colorImageFilter', () => {
    it('should set colorImageFilter option with string', () => {
      const gs = new Ghostscript();
      expect(gs.colorImageFilter('FlateEncode').options[0]).to.equal('-dColorImageFilter=/FlateEncode');
    });
  });
  describe('#autoFilterGrayImages', () => {
    it('should set autoFilterGrayImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterGrayImages(false).options[0]).to.equal('-dAutoFilterGrayImages=false');
    });

    it('should set autoFilterGrayImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterGrayImages('false').options[0]).to.equal('-dAutoFilterGrayImages=false');
    });
  });
  describe('#grayImageFilter', () => {
    it('should set grayImageFilter option', () => {
      const gs = new Ghostscript();
      expect(gs.grayImageFilter('FlateEncode').options[0]).to.equal('-dGrayImageFilter=/FlateEncode');
    });
  });
  describe('#downsampleColorImages', () => {
    it('should set downsampleColorImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleColorImages(false).options[0]).to.equal('-dDownsampleColorImages=false');
    });

    it('should set downsampleColorImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleColorImages('false').options[0]).to.equal('-dDownsampleColorImages=false');
    });
  });
  describe('#downsampleGrayImages', () => {
    it('should set downsampleGrayImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleGrayImages(false).options[0]).to.equal('-dDownsampleGrayImages=false');
    });

    it('should set downsampleGrayImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleGrayImages('false').options[0]).to.equal('-dDownsampleGrayImages=false');
    });
  });
  describe('#downsampleMonoImages', () => {
    it('should set downsampleMonoImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleMonoImages(false).options[0]).to.equal('-dDownsampleMonoImages=false');
    });

    it('should set downsampleMonoImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleMonoImages('false').options[0]).to.equal('-dDownsampleMonoImages=false');
    });
  });
  describe('#colorConversionStrategy', () => {
    it('should set colorConversionStrategy option', () => {
      const gs = new Ghostscript();
      expect(gs.colorConversionStrategy('LeaveColorUnchanged').options[0]).to.equal('-dColorConversionStrategy=/LeaveColorUnchanged');
    });
  });
  describe('#convertCMYKImagesToRGB', () => {
    it('should set convertCMYKImagesToRGB option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.convertCMYKImagesToRGB(false).options[0]).to.equal('-dConvertCMYKImagesToRGB=false');
    });

    it('should set convertCMYKImagesToRGB option with string', () => {
      const gs = new Ghostscript();
      expect(gs.convertCMYKImagesToRGB('false').options[0]).to.equal('-dConvertCMYKImagesToRGB=false');
    });
  });
  describe('#convertImagesToIndexed', () => {
    it('should set convertImagesToIndexed option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.convertImagesToIndexed(false).options[0]).to.equal('-dConvertImagesToIndexed=false');
    });

    it('should set convertImagesToIndexed option with string', () => {
      const gs = new Ghostscript();
      expect(gs.convertImagesToIndexed('false').options[0]).to.equal('-dConvertImagesToIndexed=false');
    });
  });
  describe('#UCRandBGInfo', () => {
    it('should set UCRandBGInfo option', () => {
      const gs = new Ghostscript();
      expect(gs.UCRandBGInfo('Preserve').options[0]).to.equal('-dUCRandBGInfo=/Preserve');
    });
  });
  describe('#preserveHalftoneInfo', () => {
    it('should set preserveHalftoneInfo option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveHalftoneInfo(false).options[0]).to.equal('-dPreserveHalftoneInfo=false');
    });

    it('should set convertImagesToIndexed option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveHalftoneInfo('false').options[0]).to.equal('-dPreserveHalftoneInfo=false');
    });
  });
  describe('#preserveOPIComments', () => {
    it('should set preserveOPIComments option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOPIComments(false).options[0]).to.equal('-dPreserveOPIComments=false');
    });

    it('should set preserveOPIComments option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOPIComments('false').options[0]).to.equal('-dPreserveOPIComments=false');
    });
  });
  describe('#preserveOverprintSettings', () => {
    it('should set preserveOverprintSettings option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOverprintSettings(false).options[0]).to.equal('-dPreserveOverprintSettings=false');
    });

    it('should set preserveOPIComments option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOverprintSettings('false').options[0]).to.equal('-dPreserveOverprintSettings=false');
    });
  });
  describe('#exec', () => {
    it('should convert pdf to tiff', () => {
      const gs = new Ghostscript();
      return gs.batch()
        .quiet()
        .ram()
        .nopause()
        .device()
        .resolution()
        .input(path.join(__dirname, '/data/test.pdf'))
        .output(path.join(__dirname, '/data/output/test.tif'))
        .exec();
    });

    after(() => {
      childProcess.exec(`rm ${__dirname} /data/output/test.tif`);
    });
  });
});
<commit_msg>fix wrong path who indicate tif file to delete<commit_after>'use strict';
/* eslint-env mocha */

const pkg = require('./../package.json');
const childProcess = require('child_process');
const Ghostscript = require('./../src/ghostscript');
const chai = require('chai');
const expect = chai.expect;
const path = require('path');

describe(pkg.name + '/src/ghostscript.js', () => {
  describe('#constructor', () => {
    it('should initialize some attributes', () => {
      let gs = new Ghostscript();
      expect(gs.options.length).to.equal(0);
      expect(gs._input).to.deep.equal([]);
    });
  });
  describe('#nopause', () => {
    it('should set nopause option', () => {
      const gs = new Ghostscript();
      expect(gs.nopause().options[0]).to.equal('-dNOPAUSE');
    });
  });
  describe('#quiet', () => {
    it('should set quiet option', () => {
      const gs = new Ghostscript();
      expect(gs.quiet().options[0]).to.equal('-q');
    });
  });
  describe('#batch', () => {
    it('should set batch option', () => {
      const gs = new Ghostscript();
      expect(gs.batch().options[0]).to.equal('-dBATCH');
    });
  });
  describe('#interpolate', () => {
    it('should set interpolate option', () => {
      const gs = new Ghostscript();
      expect(gs.interpolate().options[0]).to.equal('-dINTERPOLATE');
    });
  });
  describe('#ram', () => {
    it('should set ram default option (tiff24nc)', () => {
      const gs = new Ghostscript();
      expect(gs.ram().lastOption[0]).to.equal('-c "30000000 setvmthreshold" -f');
    });
    it('should set ram option', (done) => {
      const gs = new Ghostscript();
      expect(gs.ram(123456789).lastOption[0]).to.equal('-c "123456789 setvmthreshold" -f');
      done();
    });
  });
  describe('#device', () => {
    it('should set device default option (tiff24nc)', () => {
      const gs = new Ghostscript();
      expect(gs.device().options[0]).to.equal('-sDEVICE=tiff24nc');
    });
    it('should set device option', (done) => {
      const gs = new Ghostscript();
      expect(gs.device('jpeg').options[0]).to.equal('-sDEVICE=jpeg');
      done();
    });
  });
  describe('#resolution', () => {
    it('should set resolution default option (300)', () => {
      const gs = new Ghostscript();
      expect(gs.resolution().options[0]).to.equal('-r300');
    });
    it('should set resolution option', () => {
      const gs = new Ghostscript();
      expect(gs.resolution(600).options[0]).to.equal('-r600');
    });
  });
  describe('#firstPage', () => {
    it('should set firstPage option', () => {
      const gs = new Ghostscript();
      expect(gs.firstPage(1).options[0]).to.equal('-dFirstPage=1');
    });
  });
  describe('#lastPage', () => {
    it('should set lastPage option', () => {
      const gs = new Ghostscript();
      expect(gs.lastPage(1).options[0]).to.equal('-dLastPage=1');
    });
  });
  describe('#input', () => {
    it('should set input option', () => {
      const gs = new Ghostscript();
      expect(gs.input('fileInput.pdf')._input).to.deep.equal(['fileInput.pdf']);
    });
  });
  describe('#output', () => {
    it('should set output option', () => {
      const gs = new Ghostscript();
      expect(gs.output('fileOutput.tiff').options[0]).to.equal('-sOutputFile=fileOutput.tiff');
    });
  });
  describe('#compatibility', () => {
    it('should set compatibility option', () => {
      const gs = new Ghostscript();
      expect(gs.compatibility(1.4).options[0]).to.equal('-dCompatibility=1.4');
    });
  });
  describe('#pdfsettings', () => {
    it('should set pdfsettings default option', () => {
      const gs = new Ghostscript();
      expect(gs.pdfsettings('/ebook').options[0]).to.equal('-dPDFSETTINGS=/ebook');
    });
  });
  describe('#autoRotatePages', () => {
    it('should set autoRotatePages default option (All)', () => {
      const gs = new Ghostscript();
      expect(gs.autoRotatePages().options[0]).to.equal('-dAutoRotatePages=/All');
    });

    it('should set autoRotatePages option', () => {
      const gs = new Ghostscript();
      expect(gs.autoRotatePages('PageByPage').options[0]).to.equal('-dAutoRotatePages=/PageByPage');
    });
  });
  describe('#antiAliasColorImage', () => {
    it('should set antiAliasColorImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasColorImage(false).options[0]).to.equal('-dAntiAliasColorImage=false');
    });

    it('should set antiAliasColorImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasColorImage('false').options[0]).to.equal('-dAntiAliasColorImage=false');
    });
  });
  describe('#antiAliasGrayImage', () => {
    it('should set antiAliasGrayImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasGrayImage(false).options[0]).to.equal('-dAntiAliasGrayImage=false');
    });

    it('should set antiAliasGrayImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasGrayImage('false').options[0]).to.equal('-dAntiAliasGrayImage=false');
    });
  });
  describe('#antiAliasMonoImage', () => {
    it('should set antiAliasMonoImage option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasMonoImage(false).options[0]).to.equal('-dAntiAliasMonoImage=false');
    });

    it('should set antiAliasMonoImage option with string', () => {
      const gs = new Ghostscript();
      expect(gs.antiAliasMonoImage('false').options[0]).to.equal('-dAntiAliasMonoImage=false');
    });
  });
  describe('#autoFilterColorImages', () => {
    it('should set autoFilterColorImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterColorImages(false).options[0]).to.equal('-dAutoFilterColorImages=false');
    });

    it('should set autoFilterColorImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterColorImages('false').options[0]).to.equal('-dAutoFilterColorImages=false');
    });
  });
  describe('#colorImageFilter', () => {
    it('should set colorImageFilter option with string', () => {
      const gs = new Ghostscript();
      expect(gs.colorImageFilter('FlateEncode').options[0]).to.equal('-dColorImageFilter=/FlateEncode');
    });
  });
  describe('#autoFilterGrayImages', () => {
    it('should set autoFilterGrayImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterGrayImages(false).options[0]).to.equal('-dAutoFilterGrayImages=false');
    });

    it('should set autoFilterGrayImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.autoFilterGrayImages('false').options[0]).to.equal('-dAutoFilterGrayImages=false');
    });
  });
  describe('#grayImageFilter', () => {
    it('should set grayImageFilter option', () => {
      const gs = new Ghostscript();
      expect(gs.grayImageFilter('FlateEncode').options[0]).to.equal('-dGrayImageFilter=/FlateEncode');
    });
  });
  describe('#downsampleColorImages', () => {
    it('should set downsampleColorImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleColorImages(false).options[0]).to.equal('-dDownsampleColorImages=false');
    });

    it('should set downsampleColorImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleColorImages('false').options[0]).to.equal('-dDownsampleColorImages=false');
    });
  });
  describe('#downsampleGrayImages', () => {
    it('should set downsampleGrayImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleGrayImages(false).options[0]).to.equal('-dDownsampleGrayImages=false');
    });

    it('should set downsampleGrayImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleGrayImages('false').options[0]).to.equal('-dDownsampleGrayImages=false');
    });
  });
  describe('#downsampleMonoImages', () => {
    it('should set downsampleMonoImages option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleMonoImages(false).options[0]).to.equal('-dDownsampleMonoImages=false');
    });

    it('should set downsampleMonoImages option with string', () => {
      const gs = new Ghostscript();
      expect(gs.downsampleMonoImages('false').options[0]).to.equal('-dDownsampleMonoImages=false');
    });
  });
  describe('#colorConversionStrategy', () => {
    it('should set colorConversionStrategy option', () => {
      const gs = new Ghostscript();
      expect(gs.colorConversionStrategy('LeaveColorUnchanged').options[0]).to.equal('-dColorConversionStrategy=/LeaveColorUnchanged');
    });
  });
  describe('#convertCMYKImagesToRGB', () => {
    it('should set convertCMYKImagesToRGB option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.convertCMYKImagesToRGB(false).options[0]).to.equal('-dConvertCMYKImagesToRGB=false');
    });

    it('should set convertCMYKImagesToRGB option with string', () => {
      const gs = new Ghostscript();
      expect(gs.convertCMYKImagesToRGB('false').options[0]).to.equal('-dConvertCMYKImagesToRGB=false');
    });
  });
  describe('#convertImagesToIndexed', () => {
    it('should set convertImagesToIndexed option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.convertImagesToIndexed(false).options[0]).to.equal('-dConvertImagesToIndexed=false');
    });

    it('should set convertImagesToIndexed option with string', () => {
      const gs = new Ghostscript();
      expect(gs.convertImagesToIndexed('false').options[0]).to.equal('-dConvertImagesToIndexed=false');
    });
  });
  describe('#UCRandBGInfo', () => {
    it('should set UCRandBGInfo option', () => {
      const gs = new Ghostscript();
      expect(gs.UCRandBGInfo('Preserve').options[0]).to.equal('-dUCRandBGInfo=/Preserve');
    });
  });
  describe('#preserveHalftoneInfo', () => {
    it('should set preserveHalftoneInfo option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveHalftoneInfo(false).options[0]).to.equal('-dPreserveHalftoneInfo=false');
    });

    it('should set convertImagesToIndexed option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveHalftoneInfo('false').options[0]).to.equal('-dPreserveHalftoneInfo=false');
    });
  });
  describe('#preserveOPIComments', () => {
    it('should set preserveOPIComments option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOPIComments(false).options[0]).to.equal('-dPreserveOPIComments=false');
    });

    it('should set preserveOPIComments option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOPIComments('false').options[0]).to.equal('-dPreserveOPIComments=false');
    });
  });
  describe('#preserveOverprintSettings', () => {
    it('should set preserveOverprintSettings option with boolean', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOverprintSettings(false).options[0]).to.equal('-dPreserveOverprintSettings=false');
    });

    it('should set preserveOPIComments option with string', () => {
      const gs = new Ghostscript();
      expect(gs.preserveOverprintSettings('false').options[0]).to.equal('-dPreserveOverprintSettings=false');
    });
  });
  describe('#exec', () => {
    it('should convert pdf to tiff', () => {
      const gs = new Ghostscript();
      return gs.batch()
        .quiet()
        .ram()
        .nopause()
        .device()
        .resolution()
        .input(path.join(__dirname, '/data/test.pdf'))
        .output(path.join(__dirname, '/data/output/test.tif'))
        .exec();
    });

    after(() => {
      childProcess.exec(`rm ${__dirname}/data/output/test.tif`);
    });
  });
});
<|endoftext|>


<commit_before>const HeaderView = require('./import-bigquery-header-view');
const ImportDataView = require('builder/components/modals/add-layer/content/imports/import-data/import-data-view');
const headerTemplate = require('./import-bigquery-header.tpl');
const template = require('./import-bigquery.tpl');
const UploadModel = require('builder/data/upload-model');

const FormView = require('./import-bigquery-form-view');
const SelectedDataset = require('./import-bigquery-selected-dataset-view');
const ServiceTokenModel = require('../import-service/import-service-token-model');
const ServiceOauthModel = require('../import-service/import-service-oauth-model');
const ServiceLoaderView = require('../import-service/import-service-loader-view');

/**
 *  Import BigQuery panel
 *
 *  - It only accepts an query.
 *
 */

module.exports = ImportDataView.extend({
  _DATASOURCE_NAME: '',
  _WINDOW_INTERVAL: 1000, // miliseconds

  options: {
    fileExtensions: [],
    type: 'service',
    service: 'bigquery',
    acceptSync: true,
    fileEnabled: false,
    fileAttrs: {
      ext: false,
      title: '',
      description: ''
    }
  },

  initialize: function (opts) {
    if (!opts.service) throw new Error('service provider is required');
    if (!opts.userModel) throw new Error('userModel is required');
    if (!opts.configModel) throw new Error('configModel is required');

    this._userModel = opts.userModel;
    this._configModel = opts.configModel;
    this._DATASOURCE_NAME = this.options.service;

    this.model = new UploadModel(
      {
        type: this.options.type,
        service_name: this._DATASOURCE_NAME,
      },
      {
        userModel: this._userModel,
        configModel: this._configModel,
      }
    );

    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this.$el.html(template(this.options));
    this.model.set('state', 'idle');
    this._initViews();

    if (this._isServiceAuthentication()) {
      // if enabled, the oauth flow is omitted
      this._prepareModelToUpload();
    }

    return this;
  },

  _initModels: function () {
    this._serviceTokenModel = new ServiceTokenModel(null, {
      datasourceName: this._DATASOURCE_NAME,
      configModel: this._configModel
    });
    this._serviceOauthModel = new ServiceOauthModel(null, {
      datasourceName: this._DATASOURCE_NAME,
      configModel: this._configModel
    });
  },

  _initBinds: function () {
    this._initModels();
    this.model.bind('change', this._triggerChange, this);
    this.model.bind('change:state', this._checkState, this);

    if (this._isUserAuthentication()) {
      this._serviceTokenModel.bind(
        'change:oauth_valid',
        this._onOauthChange,
        this
      );
      this._serviceOauthModel.bind('change:url', this._openWindow, this);
      this.add_related_model(this._serviceOauthModel);
      this.add_related_model(this._serviceTokenModel);
    }
  },

  _initViews: function () {
    // Data header
    const headerView = new HeaderView({
      el: this.$('.ImportPanel-header'),
      model: this.model,
      userModel: this._userModel,
      collection: this.collection,
      fileEnabled: this.options.fileEnabled,
      acceptSync: this.options.acceptSync,
      template: headerTemplate
    });
    headerView.render();
    this.addView(headerView);

    if (this._isUserAuthentication()) {
      // Loader
      const loader = new ServiceLoaderView({
        el: this.$('.ServiceLoader'),
        model: this.model,
        serviceTokenModel: this._serviceTokenModel,
        serviceOauthModel: this._serviceOauthModel
      });
      loader.render();
      this.addView(loader);
    }

    // Dataset selected
    const selected = new SelectedDataset({
      el: this.$('.DatasetSelected'),
      userModel: this._userModel,
      model: this.model,
      acceptSync: this.options.acceptSync,
      fileAttrs: this.options.fileAttrs,
      configModel: this._configModel
    });
    selected.render();
    this.addView(selected);

    // Data Form
    const formView = new FormView({
      el: this.$('.ImportPanel-form'),
      userModel: this._userModel,
      configModel: this._configModel,
      model: this.model,
      fileEnabled: this.options.fileEnabled,
    });

    formView.render();
    this.addView(formView);
  },

  _onOauthChange: function () {
    if (this._serviceTokenModel.get('oauth_valid')) {
      this._prepareModelToUpload();
    }
  },

  _prepareModelToUpload: function () {
    this._checkConnection();
  },

  _checkConnection: function () {
    const version = this._configModel.urlVersion('connectors');
    const baseUrl = this._configModel.get('base_url');
    const self = this;

    fetch(baseUrl + '/api/' + version + '/connectors/bigquery/connect/')
      .then(function (response) {
        return response.json();
      })
      .then(function (data) {
        if (data && data.connected) {
          self.model.set('state', 'list');
          self.model.set('service_name', 'connector');
        } else {
          self.model.set('state', 'error');
        }
      })
      .catch(function (error) {
        console.error(error);
        self.model.set('state', 'error');
      });
  },

  _openWindow: function () {
    var url = this._serviceOauthModel.get('url');
    var self = this;
    var i = window.open(
      url,
      null,
      'menubar=no,toolbar=no,width=600,height=495'
    );
    var e = window.setInterval(function () {
      if (i && i.closed) {
        self._prepareModelToUpload();
        clearInterval(e);
      } else if (!i) {
        self.model.set('state', 'error');
        clearInterval(e);
      }
    }, this._WINDOW_INTERVAL);
  },

  _isUserAuthentication: function () {
    return !!this._configModel.get('oauth_bigquery');
  },

  _isServiceAuthentication: function () {
    return this._configModel.get('bigquery_uses_service_auth');
  }
});
<commit_msg>chore: little refactor on views creation<commit_after>const HeaderView = require('./import-bigquery-header-view');
const ImportDataView = require('builder/components/modals/add-layer/content/imports/import-data/import-data-view');
const headerTemplate = require('./import-bigquery-header.tpl');
const template = require('./import-bigquery.tpl');
const UploadModel = require('builder/data/upload-model');

const FormView = require('./import-bigquery-form-view');
const SelectedDataset = require('./import-bigquery-selected-dataset-view');
const ServiceTokenModel = require('../import-service/import-service-token-model');
const ServiceOauthModel = require('../import-service/import-service-oauth-model');
const ServiceLoaderView = require('../import-service/import-service-loader-view');

/**
 *  Import BigQuery panel
 *
 *  - It only accepts a query.
 *
 */

module.exports = ImportDataView.extend({
  _DATASOURCE_NAME: '',
  _WINDOW_INTERVAL: 1000, // miliseconds

  options: {
    fileExtensions: [],
    type: 'service',
    service: 'bigquery',
    acceptSync: true,
    fileEnabled: false,
    fileAttrs: {
      ext: false,
      title: '',
      description: ''
    }
  },

  initialize: function (opts) {
    if (!opts.service) throw new Error('service provider is required');
    if (!opts.userModel) throw new Error('userModel is required');
    if (!opts.configModel) throw new Error('configModel is required');

    this._userModel = opts.userModel;
    this._configModel = opts.configModel;
    this._DATASOURCE_NAME = this.options.service;

    this.model = new UploadModel(
      {
        type: this.options.type,
        service_name: this._DATASOURCE_NAME,
      },
      {
        userModel: this._userModel,
        configModel: this._configModel,
      }
    );

    this._initBinds();
  },

  render: function () {
    this.clearSubViews();
    this.$el.html(template(this.options));
    this.model.set('state', 'idle');
    this._initViews();

    if (this._isServiceAuthentication()) {
      // if enabled, the oauth flow is omitted
      this._prepareModelToUpload();
    }

    return this;
  },

  _initModels: function () {
    this._serviceTokenModel = new ServiceTokenModel(null, {
      datasourceName: this._DATASOURCE_NAME,
      configModel: this._configModel
    });
    this._serviceOauthModel = new ServiceOauthModel(null, {
      datasourceName: this._DATASOURCE_NAME,
      configModel: this._configModel
    });
  },

  _initBinds: function () {
    this._initModels();
    this.model.bind('change', this._triggerChange, this);
    this.model.bind('change:state', this._checkState, this);

    if (this._isUserAuthentication()) {
      this._serviceTokenModel.bind(
        'change:oauth_valid',
        this._onOauthChange,
        this
      );
      this._serviceOauthModel.bind('change:url', this._openWindow, this);
      this.add_related_model(this._serviceOauthModel);
      this.add_related_model(this._serviceTokenModel);
    }
  },

  _initViews: function () {
    this._addDataHeaderView();

    if (this._isUserAuthentication()) {
      this._addServiceLoaderView();
    }

    this._addSelectedDatasetView();
    this._addFormView();
  },

  _addDataHeaderView() {
    // Data header
    const headerView = new HeaderView({
      el: this.$('.ImportPanel-header'),
      model: this.model,
      userModel: this._userModel,
      collection: this.collection,
      fileEnabled: this.options.fileEnabled,
      acceptSync: this.options.acceptSync,
      template: headerTemplate
    });
    headerView.render();
    this.addView(headerView);
  },

  _addServiceLoaderView() {
    // Loader
    const loader = new ServiceLoaderView({
      el: this.$('.ServiceLoader'),
      model: this.model,
      serviceTokenModel: this._serviceTokenModel,
      serviceOauthModel: this._serviceOauthModel
    });
    loader.render();
    this.addView(loader);
  },

  _addSelectedDatasetView() {
    // Dataset selected
    const selected = new SelectedDataset({
      el: this.$('.DatasetSelected'),
      userModel: this._userModel,
      model: this.model,
      acceptSync: this.options.acceptSync,
      fileAttrs: this.options.fileAttrs,
      configModel: this._configModel
    });
    selected.render();
    this.addView(selected);

  },

  _addFormView() {
    // Data Form
    const formView = new FormView({
      el: this.$('.ImportPanel-form'),
      userModel: this._userModel,
      configModel: this._configModel,
      model: this.model,
      fileEnabled: this.options.fileEnabled,
    });
    formView.render();
    this.addView(formView);
  },

  _onOauthChange: function () {
    if (this._serviceTokenModel.get('oauth_valid')) {
      this._prepareModelToUpload();
    }
  },

  _prepareModelToUpload: function () {
    this._checkConnection();
  },

  _checkConnection: function () {
    const version = this._configModel.urlVersion('connectors');
    const baseUrl = this._configModel.get('base_url');
    const self = this;

    fetch(baseUrl + '/api/' + version + '/connectors/bigquery/connect/')
      .then(function (response) {
        return response.json();
      })
      .then(function (data) {
        if (data && data.connected) {
          self.model.set('state', 'list');
          self.model.set('service_name', 'connector');
        } else {
          self.model.set('state', 'error');
        }
      })
      .catch(function (error) {
        console.error(error);
        self.model.set('state', 'error');
      });
  },

  _openWindow: function () {
    var url = this._serviceOauthModel.get('url');
    var self = this;
    var i = window.open(
      url,
      null,
      'menubar=no,toolbar=no,width=600,height=495'
    );
    var e = window.setInterval(function () {
      if (i && i.closed) {
        self._prepareModelToUpload();
        clearInterval(e);
      } else if (!i) {
        self.model.set('state', 'error');
        clearInterval(e);
      }
    }, this._WINDOW_INTERVAL);
  },

  _isUserAuthentication: function () {
    return !!this._configModel.get('oauth_bigquery');
  },

  _isServiceAuthentication: function () {
    return this._configModel.get('bigquery_uses_service_auth');
  }
});
<|endoftext|>


<commit_before>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Thu Mar 24 2016 07:15:53 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'plugin/PluginBase',
    'text!./metadata.json',
    'rosmod/meta',
    'rosmod/remote_utils',
    'rosmod/modelLoader',
    'q'
], function (
    PluginConfig,
    PluginBase,
    pluginMetadata,
    MetaTypes,
    utils,
    loader,
    Q) {
    'use strict';
    pluginMetadata = JSON.parse(pluginMetadata);

    /**
     * Initializes a new instance of StopExperiment.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin StopExperiment.
     * @constructor
     */
    var StopExperiment = function () {
        // Call base class' constructor.
        PluginBase.call(this);

	this.pluginMetadata = pluginMetadata;
        this.metaTypes = MetaTypes;
    };

    StopExperiment.metadata = pluginMetadata;

    // Prototypal inheritance from PluginBase.
    StopExperiment.prototype = Object.create(PluginBase.prototype);
    StopExperiment.prototype.constructor = StopExperiment;

    StopExperiment.prototype.notify = function(level, msg) {
	var self = this;
	var prefix = self.projectId + '::' + self.projectName + '::' + level + '::';
	if (level=='error')
	    self.logger.error(msg);
	else if (level=='debug')
	    self.logger.debug(msg);
	else if (level=='info')
	    self.logger.info(msg);
	else if (level=='warning')
	    self.logger.warn(msg);
	self.createMessage(self.activeNode, msg, level);
	self.sendNotification(prefix+msg);
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    StopExperiment.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this;

        // Default fails
        self.result.success = false;

        if (typeof WebGMEGlobal !== 'undefined') {
            callback(new Error('Client-side execution is not supported'), self.result);
            return;
        }

        self.updateMETA(self.metaTypes);

	// What did the user select for our configuration?
	var currentConfig = self.getCurrentConfig();
	self.returnZip = currentConfig.returnZip;

	// will be filled out by the plugin
	self.activeHosts = [];
	self.experiment = [];
	self.rosCorePort = Math.floor((Math.random() * (65535-1024) + 1024));
	self.rosCoreIp = '';

	loader.notify = function(level, msg) {self.notify(level, msg);}
	utils.logger = self.logger;

	// the active node for this plugin is experiment -> experiments -> project
	var projectNode = self.core.getParent(self.core.getParent(self.activeNode));
	var projectName = self.core.getAttribute(projectNode, 'name');

	self.experimentName = self.core.getAttribute(self.activeNode, 'name');
	var path = require('path');
	self.root_dir = path.join(process.cwd(),
				  'generated',
				  self.project.projectId,
				  self.branchName,
				  projectName);
	self.exp_dir = path.join(self.root_dir,
				 'experiments',
				 self.experimentName);
	self.xml_dir = path.join(self.exp_dir,
				 'xml');

	self.logger.info('loading project: ' + projectName);
	utils.logger = self.logger;
	return self.getActiveHosts()
	    .then(function (ah) {
		self.activeHosts = ah;
		return self.killAllActiveHosts();
	    })
	    .then(function() {
		return self.copyLogs();
	    })
	    .then(function() {
		return self.createResults();
	    })
	    .then(function() {
		return self.cleanupExperiment();
	    })
	    .then(function() {
		return self.createZip();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		self.logger.info('saving updates to model');
		return self.save('StopExperiment updated model.');
	    })
	    .then(function (err) {
		if (err.status != 'SYNCED') {
		    callback(err, self.result);
		    return;
		}
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
        	self.logger.error(err);
        	self.createMessage(self.activeNode, err, 'error');
		self.result.setSuccess(false);
		callback(err, self.result);
	    })
		.done();
    };

    StopExperiment.prototype.getActiveHosts = function() {
	var self = this;
	return self.core.loadSubTree(self.activeNode)
	    .then(function (nodes) {
		var ah = [];
		for (var i=0; i<nodes.length; i++) {
		    var node = nodes[i];
		    if (self.core.isTypeOf(node, self.META.Host)) {
			var host = self.core.getAttribute(node, 'Host'),
			artifacts = self.core.getAttribute(node, 'Artifacts'),
			user = self.core.getAttribute(node, 'User'),
			intf = self.core.getAttribute(node, 'Interface');
			ah.push({host:host, user:user, intf:intf, artifacts: artifacts});
			self.core.deleteNode(node);
		    }
		    else if (self.core.isTypeOf(node, self.META.Container)) {
			self.core.deleteNode(node);
		    }
		    else if (self.core.isTypeOf(node, self.META.FCO) &&
			     !self.core.isTypeOf(node, self.META.Experiment) &&
			     !self.core.isTypeOf(node, self.META.Results)) {
			self.core.deleteNode(node); // delete connections
		    }
		}
		return ah;
	    });
    };

    StopExperiment.prototype.killAllActiveHosts = function() {
	var self = this;
	if (self.activeHosts.length == 0)
	    throw new String('No actively deployed experiment!');
	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var host_commands = [
		'pkill roscore',
		'pkill node_main'
	    ];
	    self.logger.info('stopping processes on: '+ user.name + '@' + ip);
	    return utils.executeOnHost(host_commands, ip, user);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.copyLogs = function() {
	var self = this;
	var path = require('path');
	var localDir = path.join(self.exp_dir, 'results');
	var mkdirp = require('mkdirp');

	var child_process = require('child_process');
	// clear out any previous config files
	child_process.execSync('rm -rf ' + utils.sanitizePath(localDir));
	// re-create it
	mkdirp.sync(localDir);

	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var remoteDir = path.join(user.Directory,
				 'experiments',
				 self.experimentName);
	    self.logger.info('Copying experiment data from ' + ip);
	    var artTasks = host.artifacts.map(function(artifact) {
		return utils.copyFromHost(remoteDir + '/' + artifact, localDir + '/.', ip, user)
		    .catch(function(err) {
			self.notify('warning', artifact + ' not found on ' + ip);
		    });

	    });
	    return Q.all(artTasks);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.createResults = function() {
	var self = this;
	var path = require('path');
	var fs = require('fs');
	var resultsNode = self.META['Results'];
	var localDir = path.join(self.exp_dir, 'results');
	var logs = fs.readdirSync(localDir);
	var rn = self.core.createNode({parent: self.activeNode, base: resultsNode});
	self.core.setRegistry(rn, 'position', {x: 100, y:50});
	var tasks = logs.map(function(log) {
	    var logName = log.split('/').slice(-1)[0].replace(/\./g, '_');
	    self.logger.info('setting meta attr for '+logName);
	    self.core.setAttributeMeta(rn, logName, {'type':'asset'});
	    self.logger.info('set meta attr for '+logName);
	    return self.blobClient.putFile(log, fs.readFileSync(localDir + '/' + log, 'utf8'))
		.then(function (hash) {
		    self.logger.info('got hash for '+log+': ' + hash);
		    self.core.setAttribute(rn, logName, hash);
		});
	});
	return Q.all(tasks)
	    .then(() => {
		var d = new Date();
		self.core.setAttribute(rn, 'name', 'Results-'+d.toUTCString());
	    });
    };

    StopExperiment.prototype.cleanupExperiment = function() {
	var self = this;
	var path = require('path');
	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var remoteDir = path.join(user.Directory,
				      'experiments');
	    self.logger.info('Removing experiment data on ' + ip);
	    return utils.executeOnHost(['rm -rf ' + remoteDir], ip, user);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.createZip = function() {
	var self = this;

	if (!self.returnZip) {
            self.createMessage(self.activeNode, 'Skipping compression.');
	    return;
	}

	return new Promise(function(resolve, reject) {
	    var zlib = require('zlib'),
	    tar = require('tar'),
	    fstream = require('fstream'),
	    input = self.exp_dir;

	    self.logger.info('zipping ' + input);

	    var bufs = [];

	    var packer = tar.Pack()
		.on('error', function(e) { reject(e); });

	    var gzipper = zlib.Gzip()
		.on('error', function(e) { reject(e); })
		.on('data', function(d) { bufs.push(d); })
		.on('end', function() {
		    self.logger.debug('gzip ended.');
		    var buf = Buffer.concat(bufs);
		    var name = self.projectName + '+' + self.experimentName + '+Results';
		    self.blobClient.putFile(name+'.tar.gz',buf)
			.then(function (hash) {
			    self.result.addArtifact(hash);
			    self.logger.info('compression complete');
			    resolve();
			})
			.catch(function(err) {
			    reject(err);
			})
			    .done();
		});

	    var reader = fstream.Reader({ 'path': input, 'type': 'Directory' })
		.on('error', function(e) { reject(e); });

	    reader
		.pipe(packer)
		.pipe(gzipper);
	})
	    .then(function() {
		self.createMessage(self.activeNode, 'Created archive.');
	    });
    };

    return StopExperiment;
});
<commit_msg>Updated stop experiment plugin to not accidentally delete documentation nodes.<commit_after>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Thu Mar 24 2016 07:15:53 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'plugin/PluginBase',
    'text!./metadata.json',
    'rosmod/meta',
    'rosmod/remote_utils',
    'rosmod/modelLoader',
    'q'
], function (
    PluginConfig,
    PluginBase,
    pluginMetadata,
    MetaTypes,
    utils,
    loader,
    Q) {
    'use strict';
    pluginMetadata = JSON.parse(pluginMetadata);

    /**
     * Initializes a new instance of StopExperiment.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin StopExperiment.
     * @constructor
     */
    var StopExperiment = function () {
        // Call base class' constructor.
        PluginBase.call(this);

	this.pluginMetadata = pluginMetadata;
        this.metaTypes = MetaTypes;
    };

    StopExperiment.metadata = pluginMetadata;

    // Prototypal inheritance from PluginBase.
    StopExperiment.prototype = Object.create(PluginBase.prototype);
    StopExperiment.prototype.constructor = StopExperiment;

    StopExperiment.prototype.notify = function(level, msg) {
	var self = this;
	var prefix = self.projectId + '::' + self.projectName + '::' + level + '::';
	if (level=='error')
	    self.logger.error(msg);
	else if (level=='debug')
	    self.logger.debug(msg);
	else if (level=='info')
	    self.logger.info(msg);
	else if (level=='warning')
	    self.logger.warn(msg);
	self.createMessage(self.activeNode, msg, level);
	self.sendNotification(prefix+msg);
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    StopExperiment.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this;

        // Default fails
        self.result.success = false;

        if (typeof WebGMEGlobal !== 'undefined') {
            callback(new Error('Client-side execution is not supported'), self.result);
            return;
        }

        self.updateMETA(self.metaTypes);

	// What did the user select for our configuration?
	var currentConfig = self.getCurrentConfig();
	self.returnZip = currentConfig.returnZip;

	// will be filled out by the plugin
	self.activeHosts = [];
	self.experiment = [];
	self.rosCorePort = Math.floor((Math.random() * (65535-1024) + 1024));
	self.rosCoreIp = '';

	loader.notify = function(level, msg) {self.notify(level, msg);}
	utils.logger = self.logger;

	// the active node for this plugin is experiment -> experiments -> project
	var projectNode = self.core.getParent(self.core.getParent(self.activeNode));
	var projectName = self.core.getAttribute(projectNode, 'name');

	self.experimentName = self.core.getAttribute(self.activeNode, 'name');
	var path = require('path');
	self.root_dir = path.join(process.cwd(),
				  'generated',
				  self.project.projectId,
				  self.branchName,
				  projectName);
	self.exp_dir = path.join(self.root_dir,
				 'experiments',
				 self.experimentName);
	self.xml_dir = path.join(self.exp_dir,
				 'xml');

	self.logger.info('loading project: ' + projectName);
	utils.logger = self.logger;
	return self.getActiveHosts()
	    .then(function (ah) {
		self.activeHosts = ah;
		return self.killAllActiveHosts();
	    })
	    .then(function() {
		return self.copyLogs();
	    })
	    .then(function() {
		return self.createResults();
	    })
	    .then(function() {
		return self.cleanupExperiment();
	    })
	    .then(function() {
		return self.createZip();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		self.logger.info('saving updates to model');
		return self.save('StopExperiment updated model.');
	    })
	    .then(function (err) {
		if (err.status != 'SYNCED') {
		    callback(err, self.result);
		    return;
		}
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
        	self.logger.error(err);
        	self.createMessage(self.activeNode, err, 'error');
		self.result.setSuccess(false);
		callback(err, self.result);
	    })
		.done();
    };

    StopExperiment.prototype.getActiveHosts = function() {
	var self = this;
	return self.core.loadSubTree(self.activeNode)
	    .then(function (nodes) {
		var ah = [];
		for (var i=0; i<nodes.length; i++) {
		    var node = nodes[i];
		    if (self.core.isTypeOf(node, self.META.Host)) {
			var host = self.core.getAttribute(node, 'Host'),
			artifacts = self.core.getAttribute(node, 'Artifacts'),
			user = self.core.getAttribute(node, 'User'),
			intf = self.core.getAttribute(node, 'Interface');
			ah.push({host:host, user:user, intf:intf, artifacts: artifacts});
			self.core.deleteNode(node);
		    }
		    else if (self.core.isTypeOf(node, self.META.Container)) {
			self.core.deleteNode(node);
		    }
		    else if (self.core.isTypeOf(node, self.META.FCO) &&
			     !self.core.isTypeOf(node, self.META.Experiment) &&
			     !self.core.isTypeOf(node, self.META.Documentation) &&
			     !self.core.isTypeOf(node, self.META.Results)) {
			self.core.deleteNode(node); // delete connections
		    }
		}
		return ah;
	    });
    };

    StopExperiment.prototype.killAllActiveHosts = function() {
	var self = this;
	if (self.activeHosts.length == 0)
	    throw new String('No actively deployed experiment!');
	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var host_commands = [
		'pkill roscore',
		'pkill node_main'
	    ];
	    self.logger.info('stopping processes on: '+ user.name + '@' + ip);
	    return utils.executeOnHost(host_commands, ip, user);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.copyLogs = function() {
	var self = this;
	var path = require('path');
	var localDir = path.join(self.exp_dir, 'results');
	var mkdirp = require('mkdirp');

	var child_process = require('child_process');
	// clear out any previous config files
	child_process.execSync('rm -rf ' + utils.sanitizePath(localDir));
	// re-create it
	mkdirp.sync(localDir);

	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var remoteDir = path.join(user.Directory,
				 'experiments',
				 self.experimentName);
	    self.logger.info('Copying experiment data from ' + ip);
	    var artTasks = host.artifacts.map(function(artifact) {
		return utils.copyFromHost(remoteDir + '/' + artifact, localDir + '/.', ip, user)
		    .catch(function(err) {
			self.notify('warning', artifact + ' not found on ' + ip);
		    });

	    });
	    return Q.all(artTasks);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.createResults = function() {
	var self = this;
	var path = require('path');
	var fs = require('fs');
	var resultsNode = self.META['Results'];
	var localDir = path.join(self.exp_dir, 'results');
	var logs = fs.readdirSync(localDir);
	var rn = self.core.createNode({parent: self.activeNode, base: resultsNode});
	self.core.setRegistry(rn, 'position', {x: 100, y:50});
	var tasks = logs.map(function(log) {
	    var logName = log.split('/').slice(-1)[0].replace(/\./g, '_');
	    self.logger.info('setting meta attr for '+logName);
	    self.core.setAttributeMeta(rn, logName, {'type':'asset'});
	    self.logger.info('set meta attr for '+logName);
	    return self.blobClient.putFile(log, fs.readFileSync(localDir + '/' + log, 'utf8'))
		.then(function (hash) {
		    self.logger.info('got hash for '+log+': ' + hash);
		    self.core.setAttribute(rn, logName, hash);
		});
	});
	return Q.all(tasks)
	    .then(() => {
		var d = new Date();
		self.core.setAttribute(rn, 'name', 'Results-'+d.toUTCString());
	    });
    };

    StopExperiment.prototype.cleanupExperiment = function() {
	var self = this;
	var path = require('path');
	var tasks = self.activeHosts.map(function(host) {
	    var ip = host.intf.IP;
	    var user = host.user;
	    var remoteDir = path.join(user.Directory,
				      'experiments');
	    self.logger.info('Removing experiment data on ' + ip);
	    return utils.executeOnHost(['rm -rf ' + remoteDir], ip, user);
	});
	return Q.all(tasks);
    };

    StopExperiment.prototype.createZip = function() {
	var self = this;

	if (!self.returnZip) {
            self.createMessage(self.activeNode, 'Skipping compression.');
	    return;
	}

	return new Promise(function(resolve, reject) {
	    var zlib = require('zlib'),
	    tar = require('tar'),
	    fstream = require('fstream'),
	    input = self.exp_dir;

	    self.logger.info('zipping ' + input);

	    var bufs = [];

	    var packer = tar.Pack()
		.on('error', function(e) { reject(e); });

	    var gzipper = zlib.Gzip()
		.on('error', function(e) { reject(e); })
		.on('data', function(d) { bufs.push(d); })
		.on('end', function() {
		    self.logger.debug('gzip ended.');
		    var buf = Buffer.concat(bufs);
		    var name = self.projectName + '+' + self.experimentName + '+Results';
		    self.blobClient.putFile(name+'.tar.gz',buf)
			.then(function (hash) {
			    self.result.addArtifact(hash);
			    self.logger.info('compression complete');
			    resolve();
			})
			.catch(function(err) {
			    reject(err);
			})
			    .done();
		});

	    var reader = fstream.Reader({ 'path': input, 'type': 'Directory' })
		.on('error', function(e) { reject(e); });

	    reader
		.pipe(packer)
		.pipe(gzipper);
	})
	    .then(function() {
		self.createMessage(self.activeNode, 'Created archive.');
	    });
    };

    return StopExperiment;
});
<|endoftext|>


<commit_before>var Board = function(args) {
  this.board = new Matrix;
  this.board.matrix = START_BOARD.map(function(row) {
    return row.split("");
  });
}
Board.prototype.getCell = function(args) {
  return this.board.matrix[args.y][args.x];
};
Board.prototype.setCell = function(args) {
  this.board.matrix[args.y][args.x] = args.value;
};
Board.prototype.find = function(value) {
  return this.board.find(value);
};
Board.prototype.convertToTile = function(coords) {
  return {
    x: Math.floor(coords.x / 8),
    y: Math.floor(coords.y / 8) - 3
  }
};
Board.prototype.predictCollision = function(coords, direction) {
  var yOffset = 0;
  var xOffset = 0;
  switch(direction) {
    case "up": yOffset -= 4.5; break;
    case "down": yOffset += 4.5; break;
    case "left": xOffset -= 4.5; break;
    case "right": xOffset += 4.5; break;
  }

  var tile = this.getCell({
    x: Math.floor((coords.x + xOffset) / 8),
    y: Math.floor((coords.y + yOffset) / 8) - 3
  });
  switch(tile) {
    case "x": return "wall"; break;
    case "-": return "door"; break;
    case ".": return "pellet"; break;
    case "o": return "powerPellet"; break;
    case "/": return "ghostBlocker"; break;
    case " ": return "none"; break;
    default: return "none"; break;
  }
};
Board.prototype.getAdjacentTiles = function(args) {
  var x = Math.floor(args.x / 8);
  var y = Math.floor(args.y / 8) - 3;
  return {
    up: {y: y - 1, x: x},
    down: {y: y + 1, x: x},
    left: {y: y, x: x - 1},
    right: {y: y, x: x + 1}
  }
};
Board.prototype.getEmptyTiles = function(tiles) {
  var emptyTiles = new Object;
  for(var prop in tiles) {
    if(tiles[prop] && tiles.hasOwnProperty(prop)) {
      var x = tiles[prop].x;
      var y = tiles[prop].y;
      var currentTile = this.getCell({x: x, y: y});
      if(!(prop == "up" && currentTile == "/") && EMPTY_TILES.includes(currentTile)) {
        emptyTiles[prop] = tiles[prop];
      }
    }
  }
  return emptyTiles;
};
Board.prototype.getTileDistance = function(tile1, tile2) {
  var width = Math.abs(tile1.x - tile2.x);
  var height = Math.abs(tile1.y - tile2.y);

  return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
};
Board.prototype.getClosestTile = function(args) {
  var tiles = args.tiles;
  var closestTile;
  for(var prop in tiles) {
    if(tiles.hasOwnProperty(prop)) {
      var currentDistance = this.getTileDistance(args.target, tiles[prop]);
      if(closestTile) {
        closestTile = currentDistance < this.getTileDistance(args.target, closestTile) ? tiles[prop] : closestTile;
      }
      else {
        closestTile = tiles[prop];
      }
    }
  }
  return closestTile;
};
<commit_msg>Update Board functions to use convertToTile<commit_after>var Board = function(args) {
  this.board = new Matrix;
  this.board.matrix = START_BOARD.map(function(row) {
    return row.split("");
  });
}
Board.prototype.getCell = function(args) {
  return this.board.matrix[args.y][args.x];
};
Board.prototype.setCell = function(args) {
  this.board.matrix[args.y][args.x] = args.value;
};
Board.prototype.find = function(value) {
  return this.board.find(value);
};
Board.prototype.convertToTile = function(coords) {
  return {
    x: Math.floor(coords.x / 8),
    y: Math.floor(coords.y / 8) - 3
  }
};
Board.prototype.predictCollision = function(coords, direction) {
  var yOffset = 0;
  var xOffset = 0;
  switch(direction) {
    case "up": yOffset -= 4.5; break;
    case "down": yOffset += 4.5; break;
    case "left": xOffset -= 4.5; break;
    case "right": xOffset += 4.5; break;
  }

  var tile = this.getCell(this.convertToTile({
    x: coords.x + xOffset,
    y: coords.y + yOffset
  }));
  switch(tile) {
    case "x": return "wall"; break;
    case "-": return "door"; break;
    case ".": return "pellet"; break;
    case "o": return "powerPellet"; break;
    case "/": return "ghostBlocker"; break;
    case " ": return "none"; break;
    default: return "none"; break;
  }
};
Board.prototype.getAdjacentTiles = function(args) {
  var coords = this.convertToTile({x: args.x, y: args.y});
  return {
    up: {y: coords.y - 1, x: coords.x},
    down: {y: coords.y + 1, x: coords.x},
    left: {y: coords.y, x: coords.x - 1},
    right: {y: coords.y, x: coords.x + 1}
  }
};
Board.prototype.getEmptyTiles = function(tiles) {
  var emptyTiles = new Object;
  for(var prop in tiles) {
    if(tiles[prop] && tiles.hasOwnProperty(prop)) {
      var x = tiles[prop].x;
      var y = tiles[prop].y;
      var currentTile = this.getCell({x: x, y: y});
      if(!(prop == "up" && currentTile == "/") && EMPTY_TILES.includes(currentTile)) {
        emptyTiles[prop] = tiles[prop];
      }
    }
  }
  return emptyTiles;
};
Board.prototype.getTileDistance = function(tile1, tile2) {
  var width = Math.abs(tile1.x - tile2.x);
  var height = Math.abs(tile1.y - tile2.y);

  return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
};
Board.prototype.getClosestTile = function(args) {
  var tiles = args.tiles;
  var closestTile;
  for(var prop in tiles) {
    if(tiles.hasOwnProperty(prop)) {
      var currentDistance = this.getTileDistance(args.target, tiles[prop]);
      if(closestTile) {
        closestTile = currentDistance < this.getTileDistance(args.target, closestTile) ? tiles[prop] : closestTile;
      }
      else {
        closestTile = tiles[prop];
      }
    }
  }
  return closestTile;
};
<|endoftext|>


<commit_before>var mongoService = module.exports = {};

var _ = require('lodash');
var q = require('q');
var mongoose = require('mongoose');

mongoService.find = function(dao, search, limit, offset, sort){
    var deferred = q.defer();
    dao.find(search).limit(limit).skip(limit * offset).sort(sort).exec(function(err, results){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(results);
        }
    });
    return deferred.promise;
};

mongoService.findAsStream = function(dao, search, limit, offset, sort){
    return dao.find(search).limit(limit).skip(limit * offset).sort(sort).stream();
};

mongoService.count = function(dao, search){
    var deferred = q.defer();
    dao.count(search).exec(function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.oneExists = function(dao, search){
  return mongoService.findOne(dao, search).then(function(result){
      return result != null;
  });
};

mongoService.findOne = function(dao, search){
    var deferred = q.defer();
    dao.findOne(search, function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.createSave = function(doc){
    var deferred = q.defer();
    doc.save(function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.createMany = function(docs){
    return q.all(docs.forEach(mongoService.createSave));
};

mongoService.findOneAndCreate = function(dao, search, doc){
    var deferred = q.defer();
    var findOptions = {
        upsert: true
    };
    dao.findOneAndUpdate(search, doc, findOptions, function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.findOneById = function(dao, id){
    var deferred = q.defer();
    dao.findById(id, function (err, model) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(model);
        }
    });
    return deferred.promise;
};

mongoService.removePromise = function(entity){
    var deferred = q.defer();
    entity.remove(function(err, removedModel){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(removedModel);
        }
    });
    return deferred.promise;
};

mongoService.savePromise = function(entity){
    var deferred = q.defer();
    entity.save(function(err, entity){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(entity);
        }
    });
    return deferred.promise;
};

mongoService.aggregatePromise = function(dao, aggregations){
    var deferred = q.defer();
    dao.aggregate(aggregations, function(err, entities){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(entities);
        }
    });
    return deferred.promise;
};

mongoService.aggregateSinglePromise = function(dao, aggregations){
    var deferred = q.defer();
    dao.aggregate(aggregations, function(err, entities){
        if (err){
            deferred.reject(err);
        } else {
            if (entities != null && entities.length == 1){
                deferred.resolve(entities[0]);
            } else {
                deferred.resolve(entities);
            }
        }
    });
    return deferred.promise;
};

mongoService.removeAll = function(dao, query){
    var deferred = q.defer();
    dao.remove(query, function (err) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(true);
        }
    });
    return deferred.promise;
};

mongoService.clearAwayChildren = function(dao, field, model){
    var deferred = q.defer();
    var query = {};
    query[field] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    return mongoService.removePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.cleanFk = function(dao, field, model){
    var deferred = q.defer();
    var query = {};
    query[field] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    entity[field] = null;
                    return mongoService.savePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.updateFlyweight = function(dao, ref, flyweightField, model){
    var deferred = q.defer();
    var query = {};
    query[ref] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    entity[flyweightField] = model;
                    return mongoService.savePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.saveAll = function(entities){
    return _.reduce(entities, function(previousResult, currentValue) {
            return previousResult.then(function(){
                return mongoService.savePromise(currentValue);
            });
        },
        q()
    );
};

mongoService.saveAllAtOnce = function(entities){
    var proms = entities.map(function(entity){
        return mongoService.savePromise(entity);
    });
    return q.allSettled(proms);
};

mongoService.getCollectionCount = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(collectionExists){
        if (collectionExists){
            dao.collection.count(function(err, totalCount){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(totalCount);
                }
            });
        } else {
            deferred.resolve(0);
        }
    });
    return deferred.promise;
};

mongoService.getCollectionStats = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(collectionExists){
        if (collectionExists){
            dao.collection.stats(function(err, results){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(results);
                }
            });
        } else {
            deferred.resolve({});
        }
    });
    return deferred.promise;
};

mongoService.dropCollection = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(result){
        if (result){
            dao.collection.drop(function(err, results){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(results);
                }
            });
        } else {
            deferred.resolve(true);
        }
    });
    return deferred.promise;
};

mongoService.collectionExists = function(collectionName){
    var deferred = q.defer();
    var db = mongoose.connection.db;
    db.collections(function(err, collections){
        if(err){
            deferred.reject(err);
        } else {
            var collectionNames = _.map(collections, function(collection){
                return collection.collectionName;
            });
            var result = _.includes(collectionNames, collectionName);
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.findDistinctByField = function(dao, field, query){
    var deferred = q.defer();
    if (query == null){
        query = {};
    }
    dao.distinct(field, query, function(err, results){
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(results);
        }
    });
    return deferred.promise;
};

<commit_msg>Imported promise lib<commit_after>var mongoService = module.exports = {};

var _ = require('lodash');
var q = require('q');
var mongoose = require('mongoose');

mongoose.Promise = Promise;

mongoService.find = function(dao, search, limit, offset, sort){
    var deferred = q.defer();
    dao.find(search).limit(limit).skip(limit * offset).sort(sort).exec(function(err, results){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(results);
        }
    });
    return deferred.promise;
};

mongoService.findAsStream = function(dao, search, limit, offset, sort){
    return dao.find(search).limit(limit).skip(limit * offset).sort(sort).stream();
};

mongoService.count = function(dao, search){
    var deferred = q.defer();
    dao.count(search).exec(function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.oneExists = function(dao, search){
  return mongoService.findOne(dao, search).then(function(result){
      return result != null;
  });
};

mongoService.findOne = function(dao, search){
    var deferred = q.defer();
    dao.findOne(search, function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.createSave = function(doc){
    var deferred = q.defer();
    doc.save(function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.createMany = function(docs){
    return q.all(docs.forEach(mongoService.createSave));
};

mongoService.findOneAndCreate = function(dao, search, doc){
    var deferred = q.defer();
    var findOptions = {
        upsert: true
    };
    dao.findOneAndUpdate(search, doc, findOptions, function(err, result){
        if(err){
            deferred.reject(err);
        } else {
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.findOneById = function(dao, id){
    var deferred = q.defer();
    dao.findById(id, function (err, model) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(model);
        }
    });
    return deferred.promise;
};

mongoService.removePromise = function(entity){
    var deferred = q.defer();
    entity.remove(function(err, removedModel){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(removedModel);
        }
    });
    return deferred.promise;
};

mongoService.savePromise = function(entity){
    var deferred = q.defer();
    entity.save(function(err, entity){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(entity);
        }
    });
    return deferred.promise;
};

mongoService.aggregatePromise = function(dao, aggregations){
    var deferred = q.defer();
    dao.aggregate(aggregations, function(err, entities){
        if (err){
            deferred.reject(err);
        } else {
            deferred.resolve(entities);
        }
    });
    return deferred.promise;
};

mongoService.aggregateSinglePromise = function(dao, aggregations){
    var deferred = q.defer();
    dao.aggregate(aggregations, function(err, entities){
        if (err){
            deferred.reject(err);
        } else {
            if (entities != null && entities.length == 1){
                deferred.resolve(entities[0]);
            } else {
                deferred.resolve(entities);
            }
        }
    });
    return deferred.promise;
};

mongoService.removeAll = function(dao, query){
    var deferred = q.defer();
    dao.remove(query, function (err) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(true);
        }
    });
    return deferred.promise;
};

mongoService.clearAwayChildren = function(dao, field, model){
    var deferred = q.defer();
    var query = {};
    query[field] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    return mongoService.removePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.cleanFk = function(dao, field, model){
    var deferred = q.defer();
    var query = {};
    query[field] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    entity[field] = null;
                    return mongoService.savePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.updateFlyweight = function(dao, ref, flyweightField, model){
    var deferred = q.defer();
    var query = {};
    query[ref] = model._id;
    dao.find(query).exec(function(err, entities){
        if (err) {
            deferred.reject("cannot query this dao");
        } else {
            if (entities.length > 0){
                var promises = _.map(entities, function(entity){
                    entity[flyweightField] = model;
                    return mongoService.savePromise(entity);
                });
                deferred.resolve(
                    q.all(promises).then(function(){
                        return model;
                    })
                );
            } else {
                deferred.resolve(q(model));
            }
        }
    });
    return deferred.promise;
};

mongoService.saveAll = function(entities){
    return _.reduce(entities, function(previousResult, currentValue) {
            return previousResult.then(function(){
                return mongoService.savePromise(currentValue);
            });
        },
        q()
    );
};

mongoService.saveAllAtOnce = function(entities){
    var proms = entities.map(function(entity){
        return mongoService.savePromise(entity);
    });
    return q.allSettled(proms);
};

mongoService.getCollectionCount = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(collectionExists){
        if (collectionExists){
            dao.collection.count(function(err, totalCount){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(totalCount);
                }
            });
        } else {
            deferred.resolve(0);
        }
    });
    return deferred.promise;
};

mongoService.getCollectionStats = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(collectionExists){
        if (collectionExists){
            dao.collection.stats(function(err, results){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(results);
                }
            });
        } else {
            deferred.resolve({});
        }
    });
    return deferred.promise;
};

mongoService.dropCollection = function(dao){
    var deferred = q.defer();
    mongoService.collectionExists(dao.collection.name).then(function(result){
        if (result){
            dao.collection.drop(function(err, results){
                if(err){
                    deferred.reject(err);
                } else {
                    deferred.resolve(results);
                }
            });
        } else {
            deferred.resolve(true);
        }
    });
    return deferred.promise;
};

mongoService.collectionExists = function(collectionName){
    var deferred = q.defer();
    var db = mongoose.connection.db;
    db.collections(function(err, collections){
        if(err){
            deferred.reject(err);
        } else {
            var collectionNames = _.map(collections, function(collection){
                return collection.collectionName;
            });
            var result = _.includes(collectionNames, collectionName);
            deferred.resolve(result);
        }
    });
    return deferred.promise;
};

mongoService.findDistinctByField = function(dao, field, query){
    var deferred = q.defer();
    if (query == null){
        query = {};
    }
    dao.distinct(field, query, function(err, results){
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(results);
        }
    });
    return deferred.promise;
};

<|endoftext|>


<commit_before>/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

import cockpit from "cockpit";
const _ = cockpit.gettext;

var cpu_ram_info_promises = { };

export function cpu_ram_info(address) {
    var pr = cpu_ram_info_promises[address];
    var dfd;
    if (!pr) {
        dfd = cockpit.defer();
        cpu_ram_info_promises[address] = pr = dfd.promise();

        cockpit.spawn(["cat", "/proc/meminfo", "/proc/cpuinfo"], { host: address })
                .done(function(text) {
                    var info = { };
                    var match = text.match(/MemTotal:[^0-9]*([0-9]+) [kK]B/);
                    var total_kb = match && parseInt(match[1], 10);
                    if (total_kb)
                        info.memory = total_kb * 1024;

                    var available_match = text.match(/MemAvailable:[^0-9]*([0-9]+) [kK]B/);
                    var available_kb = available_match && parseInt(available_match[1], 10);
                    if (available_kb)
                        info.available_memory = available_kb * 1024;

                    var swap_match = text.match(/SwapTotal:[^0-9]*([0-9]+) [kK]B/);
                    var swap_total_kb = swap_match && parseInt(swap_match[1], 10);
                    if (swap_total_kb)
                        info.swap = swap_total_kb * 1024;

                    match = text.match(/^model name\s*:\s*(.*)$/m);
                    if (match)
                        info.cpu_model = match[1];

                    info.cpus = 0;
                    var re = /^processor/gm;
                    while (re.test(text))
                        info.cpus += 1;
                    dfd.resolve(info);
                })
                .fail(function() {
                    dfd.reject();
                });
    }
    return pr;
}

// https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
const chassis_types = [
    undefined,
    _("Other"),
    _("Unknown"),
    _("Desktop"),
    _("Low Profile Desktop"),
    _("Pizza Box"),
    _("Mini Tower"),
    _("Tower"),
    _("Portable"),
    _("Laptop"),
    _("Notebook"),
    _("Hand Held"),
    _("Docking Station"),
    _("All In One"),
    _("Sub Notebook"),
    _("Space-saving Computer"),
    _("Lunch Box"), /* 0x10 */
    _("Main Server Chassis"),
    _("Expansion Chassis"),
    _("Sub Chassis"),
    _("Bus Expansion Chassis"),
    _("Peripheral Chassis"),
    _("RAID Chassis"),
    _("Rack Mount Chassis"),
    _("Sealed-case PC"),
    _("Multi-system Chassis"),
    _("Compact PCI"), /* 0x1A */
    _("Advanced TCA"),
    _("Blade"),
    _("Blade enclosure"),
    _("Tablet"),
    _("Convertible"),
    _("Detachable"), /* 0x20 */
    _("IoT Gateway"),
    _("Embedded PC"),
    _("Mini PC"),
    _("Stick PC"),
];

function parseDMIFields(text) {
    var info = {};
    text.split("\n").map(line => {
        const sep = line.indexOf(':');
        if (sep <= 0)
            return;
        const key = line.slice(0, sep).slice(line.lastIndexOf('/') + 1);
        const value = line.slice(sep + 1);
        info[key] = value;

        if (key === "chassis_type")
            info[key + "_str"] = chassis_types[parseInt(value)] || chassis_types[2]; // fall back to "Unknown"
    });
    return info;
}

var dmi_info_promises = { };

export function dmi_info(address) {
    var pr = dmi_info_promises[address];
    var dfd;
    if (!pr) {
        dfd = cockpit.defer();
        dmi_info_promises[address] = pr = dfd.promise();

        cockpit.spawn(["grep", "-r", ".", "/sys/class/dmi/id"], { err: "message", superuser: "try" })
                .done(output => dfd.resolve(parseDMIFields(output)))
                .fail((exception, output) => {
                    // the grep often/usually exits with 2, that's okay as long as we find *some* information
                    if (!exception.problem && output)
                        dfd.resolve(parseDMIFields(output));
                    else
                        dfd.reject(exception.message);
                });
    }
    return pr;
}

/* we expect udev db paragraphs like this:
 *
   P: /devices/virtual/mem/null
   N: null
   E: DEVMODE=0666
   E: DEVNAME=/dev/null
   E: SUBSYSTEM=mem
*/

const udevPathRE = /^P: (.*)$/;
const udevPropertyRE = /^E: (\w+)=(.*)$/;

function parseUdevDB(text) {
    var info = {};
    text.split("\n\n").map(paragraph => {
        let syspath = null;
        const props = {};

        paragraph = paragraph.trim();
        if (!paragraph)
            return;

        paragraph.split("\n").map(line => {
            let match = line.match(udevPathRE);
            if (match) {
                syspath = match[1];
            } else {
                match = line.match(udevPropertyRE);
                if (match)
                    props[match[1]] = match[2];
            }
        });

        if (syspath)
            info[syspath] = props;
        else
            console.log("udev database paragraph is missing P:", paragraph);
    });
    return info;
}

var udev_info_promises = { };

export function udev_info(address) {
    var pr = udev_info_promises[address];
    var dfd;
    if (!pr) {
        dfd = cockpit.defer();
        udev_info_promises[address] = pr = dfd.promise();

        cockpit.spawn(["udevadm", "info", "--export-db"], { err: "message" })
                .done(output => dfd.resolve(parseUdevDB(output)))
                .fail(exception => dfd.reject(exception.message));
    }
    return pr;
}

const memoryRE = /^([ \w]+): (.*)/;

// Process the dmidecode output and create a mapping of locator to DIMM properties
function parseMemoryInfo(text) {
    var info = {};
    text.split("\n\n").map(paragraph => {
        let locator = null;
        const props = {};
        paragraph = paragraph.trim();
        if (!paragraph)
            return;

        paragraph.split("\n").map(line => {
            line = line.trim();
            const match = line.match(memoryRE);
            if (match)
                props[match[1]] = match[2];
        });

        locator = props.Locator;
        if (locator)
            info[locator] = props;
    });
    return processMemory(info);
}

// Select the useful properties to display
function processMemory(info) {
    const memoryArray = [];

    for (const dimm in info) {
        const memoryProperty = info[dimm];

        let memorySize = memoryProperty.Size;
        if (memorySize.includes("MB")) {
            const memorySizeValue = parseInt(memorySize, 10);
            memorySize = memorySizeValue / 1024 + " GB";
        }

        let memoryTechnology = memoryProperty["Memory Technology"];
        if (!memoryTechnology || memoryTechnology == "<OUT OF SPEC>")
            memoryTechnology = _("Unknown");

        let memoryRank = memoryProperty.Rank;
        if (memoryRank == 1)
            memoryRank = _("Single Rank");
        if (memoryRank == 2)
            memoryRank = _("Dual Rank");

        memoryArray.push({
            locator: memoryProperty.Locator,
            technology: memoryTechnology,
            type: memoryProperty.Type,
            size: memorySize,
            state: memoryProperty["Total Width"] == "Unknown" ? _("Absent") : _("Present"),
            rank: memoryRank,
            speed: memoryProperty.Speed
        });
    }

    return memoryArray;
}

var memory_info_promises = {};

export function memory_info(address) {
    var pr = memory_info_promises[address];

    if (!pr) {
        memory_info_promises[address] = pr = new Promise((resolve, reject) => {
            cockpit.spawn(["dmidecode", "-t", "memory"],
                          { environ: ["LC_ALL=C"], err: "message", superuser: "try" })
                    .done(output => resolve(parseMemoryInfo(output)))
                    .fail(exception => reject(exception.message));
        });
    }

    return pr;
}
<commit_msg>lib: Stop caching machine-info promise<commit_after>/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

import cockpit from "cockpit";
const _ = cockpit.gettext;

export function cpu_ram_info(address) {
    var pr;
    var dfd;
    dfd = cockpit.defer();
    pr = dfd.promise();

    cockpit.spawn(["cat", "/proc/meminfo", "/proc/cpuinfo"], { host: address })
            .done(function(text) {
                var info = { };
                var match = text.match(/MemTotal:[^0-9]*([0-9]+) [kK]B/);
                var total_kb = match && parseInt(match[1], 10);
                if (total_kb)
                    info.memory = total_kb * 1024;

                var available_match = text.match(/MemAvailable:[^0-9]*([0-9]+) [kK]B/);
                var available_kb = available_match && parseInt(available_match[1], 10);
                if (available_kb)
                    info.available_memory = available_kb * 1024;

                var swap_match = text.match(/SwapTotal:[^0-9]*([0-9]+) [kK]B/);
                var swap_total_kb = swap_match && parseInt(swap_match[1], 10);
                if (swap_total_kb)
                    info.swap = swap_total_kb * 1024;

                match = text.match(/^model name\s*:\s*(.*)$/m);
                if (match)
                    info.cpu_model = match[1];

                info.cpus = 0;
                var re = /^processor/gm;
                while (re.test(text))
                    info.cpus += 1;
                dfd.resolve(info);
            })
            .fail(function(ex) {
                dfd.reject();
            });

    return pr;
}

// https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
const chassis_types = [
    undefined,
    _("Other"),
    _("Unknown"),
    _("Desktop"),
    _("Low Profile Desktop"),
    _("Pizza Box"),
    _("Mini Tower"),
    _("Tower"),
    _("Portable"),
    _("Laptop"),
    _("Notebook"),
    _("Hand Held"),
    _("Docking Station"),
    _("All In One"),
    _("Sub Notebook"),
    _("Space-saving Computer"),
    _("Lunch Box"), /* 0x10 */
    _("Main Server Chassis"),
    _("Expansion Chassis"),
    _("Sub Chassis"),
    _("Bus Expansion Chassis"),
    _("Peripheral Chassis"),
    _("RAID Chassis"),
    _("Rack Mount Chassis"),
    _("Sealed-case PC"),
    _("Multi-system Chassis"),
    _("Compact PCI"), /* 0x1A */
    _("Advanced TCA"),
    _("Blade"),
    _("Blade enclosure"),
    _("Tablet"),
    _("Convertible"),
    _("Detachable"), /* 0x20 */
    _("IoT Gateway"),
    _("Embedded PC"),
    _("Mini PC"),
    _("Stick PC"),
];

function parseDMIFields(text) {
    var info = {};
    text.split("\n").map(line => {
        const sep = line.indexOf(':');
        if (sep <= 0)
            return;
        const key = line.slice(0, sep).slice(line.lastIndexOf('/') + 1);
        const value = line.slice(sep + 1);
        info[key] = value;

        if (key === "chassis_type")
            info[key + "_str"] = chassis_types[parseInt(value)] || chassis_types[2]; // fall back to "Unknown"
    });
    return info;
}

var dmi_info_promises = { };

export function dmi_info(address) {
    var pr = dmi_info_promises[address];
    var dfd;
    if (!pr) {
        dfd = cockpit.defer();
        dmi_info_promises[address] = pr = dfd.promise();

        cockpit.spawn(["grep", "-r", ".", "/sys/class/dmi/id"], { err: "message", superuser: "try" })
                .done(output => dfd.resolve(parseDMIFields(output)))
                .fail((exception, output) => {
                    // the grep often/usually exits with 2, that's okay as long as we find *some* information
                    if (!exception.problem && output)
                        dfd.resolve(parseDMIFields(output));
                    else
                        dfd.reject(exception.message);
                });
    }
    return pr;
}

/* we expect udev db paragraphs like this:
 *
   P: /devices/virtual/mem/null
   N: null
   E: DEVMODE=0666
   E: DEVNAME=/dev/null
   E: SUBSYSTEM=mem
*/

const udevPathRE = /^P: (.*)$/;
const udevPropertyRE = /^E: (\w+)=(.*)$/;

function parseUdevDB(text) {
    var info = {};
    text.split("\n\n").map(paragraph => {
        let syspath = null;
        const props = {};

        paragraph = paragraph.trim();
        if (!paragraph)
            return;

        paragraph.split("\n").map(line => {
            let match = line.match(udevPathRE);
            if (match) {
                syspath = match[1];
            } else {
                match = line.match(udevPropertyRE);
                if (match)
                    props[match[1]] = match[2];
            }
        });

        if (syspath)
            info[syspath] = props;
        else
            console.log("udev database paragraph is missing P:", paragraph);
    });
    return info;
}

var udev_info_promises = { };

export function udev_info(address) {
    var pr = udev_info_promises[address];
    var dfd;
    if (!pr) {
        dfd = cockpit.defer();
        udev_info_promises[address] = pr = dfd.promise();

        cockpit.spawn(["udevadm", "info", "--export-db"], { err: "message" })
                .done(output => dfd.resolve(parseUdevDB(output)))
                .fail(exception => dfd.reject(exception.message));
    }
    return pr;
}

const memoryRE = /^([ \w]+): (.*)/;

// Process the dmidecode output and create a mapping of locator to DIMM properties
function parseMemoryInfo(text) {
    var info = {};
    text.split("\n\n").map(paragraph => {
        let locator = null;
        const props = {};
        paragraph = paragraph.trim();
        if (!paragraph)
            return;

        paragraph.split("\n").map(line => {
            line = line.trim();
            const match = line.match(memoryRE);
            if (match)
                props[match[1]] = match[2];
        });

        locator = props.Locator;
        if (locator)
            info[locator] = props;
    });
    return processMemory(info);
}

// Select the useful properties to display
function processMemory(info) {
    const memoryArray = [];

    for (const dimm in info) {
        const memoryProperty = info[dimm];

        let memorySize = memoryProperty.Size;
        if (memorySize.includes("MB")) {
            const memorySizeValue = parseInt(memorySize, 10);
            memorySize = memorySizeValue / 1024 + " GB";
        }

        let memoryTechnology = memoryProperty["Memory Technology"];
        if (!memoryTechnology || memoryTechnology == "<OUT OF SPEC>")
            memoryTechnology = _("Unknown");

        let memoryRank = memoryProperty.Rank;
        if (memoryRank == 1)
            memoryRank = _("Single Rank");
        if (memoryRank == 2)
            memoryRank = _("Dual Rank");

        memoryArray.push({
            locator: memoryProperty.Locator,
            technology: memoryTechnology,
            type: memoryProperty.Type,
            size: memorySize,
            state: memoryProperty["Total Width"] == "Unknown" ? _("Absent") : _("Present"),
            rank: memoryRank,
            speed: memoryProperty.Speed
        });
    }

    return memoryArray;
}

var memory_info_promises = {};

export function memory_info(address) {
    var pr = memory_info_promises[address];

    if (!pr) {
        memory_info_promises[address] = pr = new Promise((resolve, reject) => {
            cockpit.spawn(["dmidecode", "-t", "memory"],
                          { environ: ["LC_ALL=C"], err: "message", superuser: "try" })
                    .done(output => resolve(parseMemoryInfo(output)))
                    .fail(exception => reject(exception.message));
        });
    }

    return pr;
}
<|endoftext|>