<commit_before>'use strict';

var CreateScript = require('./CreateScript');
var Datastore = require('sivart-data/Datastore');
var Instance = require('sivart-GCE/Instance');
var Q = require('q');

var Build = function(args, rawBuildRequest) {
  for (var key in args) {
    this[key] = args[key];
  }
  this.createScript = new CreateScript(this);
  this.datastore = new Datastore(this.repoName);
  this.rawBuildRequest = rawBuildRequest;
};

// Take a startup script and create a VM for it
Build.prototype.createInstance = function(script, cb) {
  var newBuildVM = Instance.Factory('slave');

  // Stash instance name
  script.metadata.instanceName = newBuildVM.instanceName;
  newBuildVM.build(script.script, function(err) {
    script.metadata.created = new Date().getTime();
    script.metadata.state = 'running';
    cb(err, script.metadata);
  });
};

Build.prototype.doBuilds = function(cb) {
  var me = this;
  Q.ninvoke(this.datastore, 'getNextBuildNumber')
    .then(function(buildId) {
      Q.ninvoke(me.createScript, 'getScripts', buildId)
        .then(function(scripts) {
          Q.allSettled(scripts.map(function(script) {
              return Q.ninvoke(me, 'createInstance', script);
          }))
          .then(function(results) {
            var runs =
              results
                .map(function(val) {
                  return val.state === 'fulfilled' ? val.value : val.reason;
                });

            var successes =
              results
                .filter(function(resp) {
                  return resp.state === 'fulfilled';
                })
                .sort(function(a, b) {
                  return a.value.buildNumber - b.value.buildNumber;
                })
                .map(function(val) {
                  return val.value;
                });

            var failures =
              results
                .filter(function(resp) {
                  return resp.state === 'rejected';
                })
                .map(function(val) {
                  return val.reason;
                });

            Q.ninvoke(
                me.datastore,
                'saveInitialData',
                runs,
                me.rawBuildRequest,
                {
                  kind: me.eventName,
                  created: new Date().getTime(),
                  state: 'running',
                  id: buildId,
                  branch: me.branch
                }
            ).then(
              function() {
                if (failures.length) {
                  cb(failures, successes);
                } else {
                  cb(null, successes);
                }
              }<commit_msg>store reponame in runs too<commit_after>'use strict';

var CreateScript = require('./CreateScript');
var Datastore = require('sivart-data/Datastore');
var Instance = require('sivart-GCE/Instance');
var Q = require('q');

var Build = function(args, rawBuildRequest) {
  for (var key in args) {
    this[key] = args[key];
  }
  this.createScript = new CreateScript(this);
  this.datastore = new Datastore(this.repoName);
  this.rawBuildRequest = rawBuildRequest;
};

// Take a startup script and create a VM for it
Build.prototype.createInstance = function(script, cb) {
  var newBuildVM = Instance.Factory('slave');

  // Stash instance name
  script.metadata.instanceName = newBuildVM.instanceName;
  newBuildVM.build(script.script, function(err) {
    script.metadata.created = new Date().getTime();
    script.metadata.state = 'running';
    cb(err, script.metadata);
  });
};

Build.prototype.doBuilds = function(cb) {
  var me = this;
  Q.ninvoke(this.datastore, 'getNextBuildNumber')
    .then(function(buildId) {
      Q.ninvoke(me.createScript, 'getScripts', buildId)
        .then(function(scripts) {
          Q.allSettled(scripts.map(function(script) {
              return Q.ninvoke(me, 'createInstance', script);
          }))
          .then(function(results) {
            var runs =
              results
                .map(function(val) {
                  return val.state === 'fulfilled' ? val.value : val.reason;
                });

            var successes =
              results
                .filter(function(resp) {
                  return resp.state === 'fulfilled';
                })
                .sort(function(a, b) {
                  return a.value.buildNumber - b.value.buildNumber;
                })
                .map(function(val) {
                  return val.value;
                });

            var failures =
              results
                .filter(function(resp) {
                  return resp.state === 'rejected';
                })
                .map(function(val) {
                  return val.reason;
                });

            Q.ninvoke(
                me.datastore,
                'saveInitialData',
                runs,
                me.rawBuildRequest,
                {
                  kind: me.eventName,
                  created: new Date().getTime(),
                  state: 'running',
                  id: buildId,
                  repoName: me.repoName,
                  branch: me.branch
                }
            ).then(
              function() {
                if (failures.length) {
                  cb(failures, successes);
                } else {
                  cb(null, successes);
                }
              }
{'fraction_non_alphanumeric': 0.11659275683665928, 'fraction_numerical': 0.0, 'mean_word_length': 1.485307621671258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1124820', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1423, 'n_words': 286}