Points = new Meteor.Collection("points");
HVAC = new Meteor.Collection("hvac");
Lighting = new Meteor.Collection("lighting");
Monitoring = new Meteor.Collection("monitoring");

if (Meteor.isServer) {
  Meteor.methods({
    querysystem: function() {
      if (Meteor.isServer) {
        console.log('called!')

      var get_source_path = function(path) { return Meteor.call('get_source_path', path); }
      var get_endpoint = function(path) { return Meteor.call('get_endpoint', path); }
      // query all HVAC system points at our current site
      _.each(['HVAC','Lighting','Monitoring'], function(system, index) {
        var query = "select * where Metadata/Site = '" + Meteor.settings.public.site + "'";
        query += " and Metadata/System = '"+system+"'";
        if (system == 'HVAC') {
          query += " and not Metadata/HVACZone = ''";
        } else if (system == 'Lighting') {
          query += " and not Metadata/LightingZone = ''";
        }

        Meteor.call('query', query, function(err, res) {
          // quick error checking
          if (err) {
            console.log("Error running query:",query, err);
            return
          }
          if (!res) {
            console.log("No results found for",query);
            return
          }
          console.log(res);
          
          /* 
           * Need to find unique paths. Unfortunately, we have full timeseries paths. Need
           * to lop off the last '/'-delimited segment and then take the unique set
           */
          var source_paths = _.map(_.pluck(res, 'Path'), get_source_path);
          var unique_paths = _.uniq(source_paths);
          console.log('unique paths', unique_paths);

          /*
           * for each unique_path, we add it to the system collection as a key. The value is
           * an object that maps each aspect (e.g. temp, temp_heat, etc) to its uuid. This
           * will facilitate lookups in the Points collection
           */
          _.each(unique_paths, function(src_path, idx) {
            // get timeseries objects for this unique_path
            var my_ts = _.filter(res, function(o) { return get_source_path(o.Path) == src_path; });
            if (system == 'HVAC') {
              var zonename = my_ts[0].Metadata.HVACZone;
            } else if (system == 'Lighting') {
              var zonename = my_ts[0].Metadata.LightingZone;
              var groupname = my_ts[0].Metadata.Group;
            } else if (system == 'Monitoring') {
              var roomname = my_ts[0].Metadata.Room;
              var lightzonename = my_ts[0].Metadata.LightingZone;
              var hvaczonename = my_ts[0].Metadata.HVACZone;
            }
            // the record we will insert
            var record = {};
            _.each(my_ts, function(val, idx) {
              var endpoint = get_endpoint(val.Path);
              record[endpoint] = val
            });

            // insert into database
              if (system == 'HVAC') {
                HVAC.upsert({'zone': zonename}, {'zone': zonename, 'timeseries': record});
              } else if (system == 'Lighting') {
                Lighting.upsert({'group': groupname}, {'group': groupname, 'zone': zonename, 'timeseries': record});
              } else if (system == 'Monitoring') {
                Monitoring.upsert({'room': roomname}, {'room': roomname, 'lightingzone': lightzonename, 'hvaczone': hvaczonename, 'timeseries': record});
              }

          });

        });
      });
      }
    },
  })
}


@@ -2176,24 +2176,79 @@
 c_path; %7D);%0A
+            var path = get_source_path(my_ts%5B0%5D.Path);%0A
             
@@ -3062,31 +3062,27 @@
 psert(%7B'
-zone': zonename
+path': path
 %7D, %7B'zon
@@ -3202,33 +3202,27 @@
 psert(%7B'
-group': groupname
+path': path
 %7D, %7B'gro
@@ -3370,27 +3370,37 @@
 t(%7B'
-room': roomname%7D, %7B
+path': path%7D, %7B'path': path, 
 'roo

insert by path to avoid overwriting