<commit_before>import Ember from 'ember';
import request from '../utils/request';

export default Ember.Route.extend({
  model() {
    const store = this.store;

    const roomList = store.createRecord('room-list');
    const rooms    = roomList.get('rooms');

    request.send({ op: 'rooms' }, 'json').then(function(rawRooms) {
      _.each(rawRooms, function(rawRoom, roomName) {
        // FIXME: Something odd happens with spaces being replaced by +es,
        //        so force it here.
        roomName = roomName.replace(' ', '+');

        store.query('room', { label: roomName }).then(function(matches) {
          let room;

          if (!matches.get('length')) {
            room = store.createRecord('room', { label: roomName });
          } else {
            // FIXME: Should we really be doing this?
            //        Duplicates shouldn't be introduced.
            //        Use the roomName as the ID?
            matches.forEach(function(matchedRoom) {
              if (!room) {
                room = matchedRoom;
              } else {
                // Remove duplicates.
                matchedRoom.destroyRecord();
              }
            });
          }

          rooms.pushObject(room);

          const devices = room.get('devices');

          // FIXME: Don't always recreate it?
          devices.clear();

          _.each(rawRoom, function(rawDevice, deviceName) {
            store.query('device', { label: deviceName }).then(function(deviceMatches) {
              let device;

              if (!deviceMatches.get('length')) {
                device = store.createRecord('device', {
                  label:    deviceName,
                  type:     rawDevice.type,
                  state:    rawDevice.state,
                  temp:     rawDevice.temperature,
                  setPoint: rawDevice.setpoint,
                });

                device.save();
              } else {
                // FIXME: Should we really be doing this?
                //        Duplicates shouldn't be introduced.
                //        Use the deviceName as the ID?
                matches.forEach(function(matchedDevice) {
                  if (!device) {
                    device = matchedDevice;
                  } else {
                    // Remove duplicates.
                    matchedDevice.destroyRecord();
                  }
                });
              }

              devices.pushObject(device);
            });
          });
        });
      });

      rooms.forEach(function(room) {
        room.save();
      });
    });

    const scheduleList = store.createRecord('schedule-list');
    const schedules    = scheduleList.get('schedules');

    store.findAll('schedule').then(function(matches) {
      matches.forEach(function(schedule) {
        schedules.pushObject(schedule);
      });
    });

    return {
      roomList:     roomList,
      scheduleList: scheduleList,
    };
  },
});<commit_msg>Don't fix the extra +es.<commit_after>import Ember from 'ember';
import request from '../utils/request';

export default Ember.Route.extend({
  model() {
    const store = this.store;

    const roomList = store.createRecord('room-list');
    const rooms    = roomList.get('rooms');

    request.send({ op: 'rooms' }, 'json').then(function(rawRooms) {
      _.each(rawRooms, function(rawRoom, roomName) {
        store.query('room', { label: roomName }).then(function(matches) {
          let room;

          if (!matches.get('length')) {
            room = store.createRecord('room', { label: roomName });
          } else {
            // FIXME: Should we really be doing this?
            //        Duplicates shouldn't be introduced.
            //        Use the roomName as the ID?
            matches.forEach(function(matchedRoom) {
              if (!room) {
                room = matchedRoom;
              } else {
                // Remove duplicates.
                matchedRoom.destroyRecord();
              }
            });
          }

          rooms.pushObject(room);

          const devices = room.get('devices');

          // FIXME: Don't always recreate it?
          devices.clear();

          _.each(rawRoom, function(rawDevice, deviceName) {
            store.query('device', { label: deviceName }).then(function(deviceMatches) {
              let device;

              if (!deviceMatches.get('length')) {
                device = store.createRecord('device', {
                  label:    deviceName,
                  type:     rawDevice.type,
                  state:    rawDevice.state,
                  temp:     rawDevice.temperature,
                  setPoint: rawDevice.setpoint,
                });

                device.save();
              } else {
                // FIXME: Should we really be doing this?
                //        Duplicates shouldn't be introduced.
                //        Use the deviceName as the ID?
                matches.forEach(function(matchedDevice) {
                  if (!device) {
                    device = matchedDevice;
                  } else {
                    // Remove duplicates.
                    matchedDevice.destroyRecord();
                  }
                });
              }

              devices.pushObject(device);

              room.save();
            });
          });
        });
      });
    });

    const scheduleList = store.createRecord('schedule-list');
    const schedules    = scheduleList.get('schedules');

    store.findAll('schedule').then(function(matches) {
      matches.forEach(function(schedule) {
        schedules.pushObject(schedule);
      });
    });

    return {
      roomList:     roomList,
      scheduleList: scheduleList,
    };
  },
});
{'fraction_non_alphanumeric': 0.11850564726324934, 'fraction_numerical': 0.0, 'mean_word_length': 1.423578947368421, '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': '4849757', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1383, 'n_words': 309}