<commit_before>// Use EnabledRoute for routes that require the app to be enabled (configured and healthy)
App.EnabledRoute = Ember.Route.extend({
  beforeModel: function () {
    // TODO: Make this check async-capable
    //if (!this.controllerFor('application').get('isEnabled')) this.transitionTo('index');
  }
});

// Add authentication to all routes (unless isAuthenticated flag is false)
Ember.Route.reopen({
  isAuthenticated: true,
  beforeModel: function (transition) {
    //if (!this.controllerFor('login').get('token')) {
    if (localStorage.loggedIn) this.controllerFor('application').set('loggedIn', true);
    var loggedIn = this.controllerFor('application').get('loggedIn');
    if (this.get('isAuthenticated') && !loggedIn) {
      this.redirectToLogin(transition);
    }
  },
  redirectToLogin: function (transition) {
    // Log out user
    localStorage.removeItem('loggedIn');
    this.controllerFor('login').set('loggedIn', false);
    this.controllerFor('login').set('username', null);
    this.controllerFor('login').set('password', <PASSWORD>);
    // Save attempted route transition
    if (transition) this.controllerFor('login').set('attemptedTransition', transition);
    // Redirect to login route
    this.transitionTo('login');
  },
  events: {
    error: function (reason, transition) {
      if (reason.status === 401) {
        this.redirectToLogin(transition);
      }
    }
  }
});

// Application
App.ApplicationRoute = Ember.Route.extend({
  isAuthenticated: false,
  init: function () {
    App.store = this.store;
    App.route = this;
  },
  setupController: function () {
    this.controllerFor('vms').set('model', this.store.find('vm'));
  },
  model: function () {
    var self = this;
    return this.controllerFor('statuses').updateCurrentStatus().then(function () {
      // Status API has responded
      var configurationPromise = Ember.RSVP.hash({
        nova: App.nova.check(),
        openrc: App.openrc.check(),
        quantum: App.quantum.check()
      });
      return configurationPromise.then(function () {
        // SAM is configured
        self.store.find('sloTemplate');
        self.store.find('slo');
        self.store.find('sla');
        self.store.find('flavor');
        self.store.find('vm');
        App.mtWilson.check().then(function () {
          if (App.mtWilson.get('isInstalled')) {
            self.store.find('trustMle');
            self.store.find('trustNode');
            self.store.find('node');
          } else {
            self.store.find('node');
          }
        }, function () {
          self.store.find('node');
        }),
        App.network.check();
        self.store.find('action');
        //self.store.find('user');
        //App.settingsLog.fetch();
      }, function () {
        // SAM is not configured
        App.network.check();
        //self.store.find('user');
        // Don't block loading if SAM is not configured
        return new Ember.RSVP.Promise(function (resolve, reject) { resolve(); });
      });

    }, function () {
      // Status API is not responding
      var confirmed = confirm('The Status API is not responding. Would you like to try to load the application again?');
      if (confirmed) {
        location.reload();
      }
      // Block loading if Status API fails
      return new Ember.RSVP.Promise(function (resolve, reject) { reject(); });
    });
  },
  setupController: function (controller, model) {
    // Set models for these controllers on app load (instead of waiting for route transitions)
    this.controllerFor('slas').set('model', this.store.filter('sla', function (sla) {
      return !sla.get('deleted');
    }));
    this.controllerFor('flavors').set('model', this.store.all('flavor'), function (flavor) {
      return !flavor.get('deleted');
    });
    this.controllerFor('vms').set('model', this.store.all('vm'));
    this.controllerFor('nodes').set('model', this.store.all('node'));
  },
  actions: {
    logout: function() {
      this.redirectToLogin();

      // TODO: Migrate Sunil's authentication code
      /*
      var cleanup = function() {
        location.href = '/';
      };
      App.session.deleteRecord();
      var handlers = {'didDelete' : {postFun:cleanup, nextRoute:'login'}};
      App.modelhelper.doTransaction(App.session, this.controller, this, handlers);
      */
    },
    showModal: function (modalName, controllerName) {
      App.ModalView.create({
        templateName: 'modals/' + modalName,
        controller: this.controllerFor(controllerName)
      }).append();
    }
  }
});

// TODO: Migrate Sunil's authentication code
/*
// Index
App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    var route = App.state.get('route');
    var context = App.state.get('context');
    if (context != null) {
      this.transitionTo(route, context);
    } else {
      this.transitionTo(route);
    }
  }
});
*/

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('dashboard');
  }
});

App.LoginRoute = Ember.Route.extend({
  isAuthenticated: false,
  actions: {
    login: function () {
      localStorage.loggedIn = true;
      this.controllerFor('application').set('loggedIn', true);
      var attemptedTransition = this.controllerFor('login').get('attemptedTransition');
      if (attemptedTransition) {
        if (typeof attemptedTransition === 'string') {
          this.transitionTo(attemptedTransition);
        } else {
          this.controllerFor('login').get('attemptedTransition').retry();
        }
        this.controllerFor('login').set('attemptedTransition', null);
      } else {
        this.transitionTo('index');
      }
      //this.controller.createSession(this);
    }
  }
});

App.DashboardRoute = Ember.Route.extend();


App.TempPasswordRoute = Ember.Route.extend({
  actions: {
    generate_password: function() {
      this.controller.generatePassword(this);
    }
  }<commit_msg>Base route entry on nesting instead of flag.<commit_after>// Use EnabledRoute for routes that require the app to be enabled (configured and healthy)
App.EnabledRoute = Ember.Route.extend({
  beforeModel: function () {
    // TODO: Make this check async-capable
    //if (!this.controllerFor('application').get('isEnabled')) this.transitionTo('index');
  }
});

// Application
App.ApplicationRoute = Ember.Route.extend({
  init: function () {
    App.store = this.store;
    App.route = this;
  },
  beforeModel: function (transition) {
    if (localStorage.loggedIn) this.controllerFor('application').set('loggedIn', true);
  },
  model: function () {
    var self = this;
    return this.controllerFor('statuses').updateCurrentStatus().then(function () {
      // Status API has responded
      var configurationPromise = Ember.RSVP.hash({
        nova: App.nova.check(),
        openrc: App.openrc.check(),
        quantum: App.quantum.check()
      });
      return configurationPromise.then(function () {
        // SAM is configured
        self.store.find('sloTemplate');
        self.store.find('slo');
        self.store.find('sla');
        self.store.find('flavor');
        self.store.find('vm');
        App.mtWilson.check().then(function () {
          if (App.mtWilson.get('isInstalled')) {
            self.store.find('trustMle');
            self.store.find('trustNode');
            self.store.find('node');
          } else {
            self.store.find('node');
          }
        }, function () {
          self.store.find('node');
        }),
        App.network.check();
        self.store.find('action');
        //self.store.find('user');
        //App.settingsLog.fetch();
      }, function () {
        // SAM is not configured
        App.network.check();
        //self.store.find('user');
        // Don't block loading if SAM is not configured
        return new Ember.RSVP.Promise(function (resolve, reject) { resolve(); });
      });

    }, function () {
      // Status API is not responding
      var confirmed = confirm('The Status API is not responding. Would you like to try to load the application again?');
      if (confirmed) {
        location.reload();
      }
      // Block loading if Status API fails
      return new Ember.RSVP.Promise(function (resolve, reject) { reject(); });
    });
  },
  setupController: function (controller, model) {
    // Set models for these controllers on app load (instead of waiting for route transitions)
    this.controllerFor('slas').set('model', this.store.filter('sla', function (sla) {
      return !sla.get('deleted');
    }));
    this.controllerFor('flavors').set('model', this.store.all('flavor'), function (flavor) {
      return !flavor.get('deleted');
    });
    this.controllerFor('vms').set('model', this.store.all('vm'));
    this.controllerFor('nodes').set('model', this.store.all('node'));
  },
  actions: {
    redirectToLogin: function (transition) {
      console.log('redirectToLogin');
      // Log out user
      localStorage.removeItem('loggedIn');
      this.controllerFor('login').set('loggedIn', false);
      this.controllerFor('login').set('username', null);
      this.controllerFor('login').set('password', <PASSWORD>);
      // Save attempted route transition
      if (transition) this.controllerFor('login').set('attemptedTransition', transition);
      // Redirect to login route
      this.transitionTo('login');
    },
    logout: function() {
      this.send('redirectToLogin');

      // TODO: Migrate Sunil's authentication code
      /*
      var cleanup = function() {
        location.href = '/';
      };
      App.session.deleteRecord();
      var handlers = {'didDelete' : {postFun:cleanup, nextRoute:'login'}};
      App.modelhelper.doTransaction(App.session, this.controller, this, handlers);
      */
    },
    showModal: function (modalName, controllerName) {
      App.ModalView.create({
        templateName: 'modals/' + modalName,
        controller: this.controllerFor(controllerName)
      }).append();
    }
  }
});

// TODO: Migrate Sunil's authentication code
/*
// Index
App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    var route = App.state.get('route');
    var context = App.state.get('context');
    if (context != null) {
      this.transitionTo(route, context);
    } else {
      this.transitionTo(route);
    }
  }
});
*/

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('dashboard');
  }
});

App.LoginRoute = Ember.Route.extend({
  actions: {
    login: function () {
      localStorage.loggedIn = true;
      this.controllerFor('application').set('loggedIn', true);
      var attemptedTransition = this.controllerFor('login').get('attemptedTransition');
      if (attemptedTransition) {
        if (typeof attemptedTransition === 'string') {
          this.transitionTo(attemptedTransition);
        } else {
          this.controllerFor('login').get('attemptedTransition').retry();
        }
        this.controllerFor('login').set('attemptedTransition', null);
      } else {
        this.transitionTo('index');
      }
      //this.controller.createSession(this);
    }
  }
});

App.TempPasswordRoute = Ember.Route.extend({
  actions: {
    generate_password: function() {
      this.controller.generatePassword(this);
    }
  }
});

// Add authentication to routes under /app
App.AppRoute = Ember.Route.extend({
  beforeModel: function (transition) {
    var loggedIn = this.controllerFor('application').get('loggedIn');
    if (!loggedIn) {
      console.log('test 1');
      transition.send('redirectToLogin', transition);
    }
  },
  events: {
    error: function (reason, transition) {
      if (reason.status === 401) {
        transition.send('redirectToLogin', transition);
      }
    }
  }
});

App.DataRoute = Ember.Route.extend({
  beforeModel: function () {
    console.log('DataRoute. isEnabled:', this.controllerFor('application').get('isEnabled'));
  }
{'fraction_non_alphanumeric': 0.15742682210852973, 'fraction_numerical': 0.0005871005619391093, 'mean_word_length': 2.8340836012861734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1844971', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3390, 'n_words': 688}