// Holds one line's worth of data on a multi-line chart 
var QueryModel = Backbone.Model.extend({ 
  // url: '/data',

  // Technically the fetching of data from server should happen in here

  initialize: function(queryObj){
    this.startDate = queryObj.startDate;
    this.endDate = queryObj.endDate;
    this.source = queryObj.source;
    this.keyword = queryObj.keyword;

    console.log('the start is ', this.startDate);


    // declare some variables that will be used when
    // we handleResponseData below
    this.responseData = [];
    this.articles = [];
    this.frequencyCounts = {};
    this.totalSentiments = {};
    this.averages = {};
    this.maxSentiment = 0;
    this.minSentiment = 0;

    // figure out what url to ping
    // example url:
    // "/data?startDate=20101214&endDate=20150114&source=newyorktimes&keyword=BP",
    var startDateURLFormat = this.startDate.getFullYear() + '' +
      this.startDate.getMonth() + '' +
      this.startDate.getDate();
    var endDateURLFormat = this.endDate.getFullYear() + '' +
      this.endDate.getMonth() + '' +
      this.endDate.getDate();
    var sourceURLFormat = this.source.replace(/\s+/g, '').toLowerCase();
    var keywordURLFormat = this.keyword.replace(/\s+/g, '') //.toLowerCase();

    this.url =
      '/data?startDate=' + startDateURLFormat +
      '&endDate=' + endDateURLFormat +
      '&source=' + sourceURLFormat +
      '&keyword=' + keywordURLFormat; 
    console.log(this.url);
  },

  queryServer: function(){
    var scope = this;
    $.ajax({  
      url: scope.url
    })
    .done(function( newData  ) {
      console.log('receiving data: ', newData);
      scope.set({
        responseData: newData,
      });
    });
    return this;
  },

  handleResponseData: function(){
    // calculate frequency counts and averages
    var scope = this;
    var maxSentiment = 0;
    var minSentiment = 0;

    var frequencyTally = {};
    var totalSentiment = {};
    var sentiment = {};
    
    console.log(this.get('responseData').constructor);
    var articles = this.get('responseData').map(function(obj){
      return _.pick(obj, 'published', 'sentiment', 'url', 'headline');
    });

    // could vary this function if time period is not '1 year'
    // could use a more generic word like 'timeSpan' insted of 'year'
    articles.forEach(function(article){
      var date = new Date(article['published']);
      article['displayDate'] = date.toDateString();
      article['year'] = date.getFullYear();
      article['month'] = date.getMonth();
      // article['year'] = year;
      // console.log('date:', date);
      article['date'] = date;
    });

    articles.forEach(function(article){
      var year = article['year'];
      var month = article['month'];
      var sentiment = article['sentiment'];
      // if (sentiment > maxSentiment){
      //   maxSentiment = sentiment;
      // } else if (sentiment < minSentiment){
      //   minSentiment = sentiment;
      // }
      if (!frequencyTally.hasOwnProperty(year)){
        frequencyTally[year] = 0;
        totalSentiment[year] = 0;
      }
      frequencyTally[year] = frequencyTally[year] + 1;
      totalSentiment[year] = totalSentiment[year] + sentiment;

    });

    // put summary data into array format to graph it
    var summaryDataPoints = _.reduce(frequencyTally, function(memo, tally, year){
      var dataPoint = {};
      dataPoint.count = tally;
      dataPoint.year = year;
      var midPeriodDate = new Date(year + "-07-01");
      dataPoint.date = new Date(Math.min(scope.endDate, midPeriodDate));
      dataPoint.sentiment = totalSentiment[ year ] / tally;
      return memo.concat( [dataPoint] );
    }, [] );

    this.set('articles', articles);
    this.set('maxSentiment', maxSentiment);
    this.set('minSentiment', minSentiment);
    this.set('summaryDataPoints', summaryDataPoints);
  },


}); 
@@ -1973,67 +1973,42 @@
 %7B%7D;%0A
-    
 %0A    
-console.log(this.get('responseData').constructor);
+var timeBucket = 'year';%0A    
 %0A   
@@ -2417,24 +2417,116 @@
 teString();%0A
+      if (timeBucket = 'year')%7B%0A        article%5B'timeBucket'%5D = date.getFullYear();%0A      %7D%0A
       articl
@@ -2554,32 +2554,35 @@
 ullYear();%0A     
+ //
  article%5B'month'
@@ -2761,20 +2761,23 @@
     var 
-year
+tBucket
  = artic
@@ -2780,20 +2780,26 @@
 rticle%5B'
-year
+timeBucket
 '%5D;%0A    
@@ -2795,24 +2795,27 @@
 ket'%5D;%0A     
+ //
  var month =
@@ -3090,20 +3090,23 @@
 roperty(
-year
+tBucket
 ))%7B%0A    
@@ -3120,28 +3120,31 @@
 quencyTally%5B
-year
+tBucket
 %5D = 0;%0A     
@@ -3157,28 +3157,31 @@
 alSentiment%5B
-year
+tBucket
 %5D = 0;%0A     
@@ -3204,20 +3204,23 @@
 cyTally%5B
-year
+tBucket
 %5D = freq
@@ -3230,20 +3230,23 @@
 cyTally%5B
-year
+tBucket
 %5D + 1;%0A 
@@ -3265,20 +3265,23 @@
 ntiment%5B
-year
+tBucket
 %5D = tota
@@ -3291,20 +3291,23 @@
 ntiment%5B
-year
+tBucket
 %5D + sent
@@ -3452,20 +3452,23 @@
  tally, 
-year
+tBucket
 )%7B%0A     
@@ -3539,30 +3539,93 @@
 int.
-year = year;%0A      var
+tBucket = tBucket;%0A      var midPeriodDate;%0A      if (timeBucket === 'year')%7B%0A       
  mid
@@ -3646,20 +3646,26 @@
 ew Date(
-year
+timeBucket
  + %22-07-
@@ -3670,16 +3670,27 @@
 7-01%22);%0A
+      %7D   %0A
       da
@@ -3798,20 +3798,23 @@
 timent%5B 
-year
+tBucket
  %5D / tal

Allow summaryDataPoints to use other timeframes