<commit_before>define [
  'jquery'
  'backbone'
  'amcharts'
  'amstock'
], ($, Backbone, amcharts)-> Backbone.View.extend
  el: '#soil-moisture-chart'


  getData: ->
    [
      timestamp: "2015-12-04T11:28:17.946Z"
      temp: "8.4"
    ,
      timestamp: "2015-12-04T11:29:17.946Z"
      temp: "8.0"
    ,
      timestamp: "2015-12-04T11:30:17.946Z"
      temp: "8.9"
    ]

  
  initialize:->
    @chart = amcharts.makeChart @el, @chartSettings()
    @listenTo @collection, 'add', @updateChart
    # do @hardcoded


  hardcoded: ->
    console.log do @getData
    @chart.dataSets.push
      dataProvider: do @getData
      title: 'my chart'
      fieldMappings: 
        [
          fromField: 'temp',
          toField: 'value'
        ]
      categoryField: "timestamp"
      compared: true
    do @chart.validateData

  updateChart: (node) ->
    node.observations.on 'add', (obs) =>
      console.log node.id
      if node.id == 'AF'
        console.log do node.observations.toJSON
        sensor = "soil_1"
        title = node.id + " " + sensor
        dataProvider = _.findWhere(@chart.dataSets, {'title': title})
        if not dataProvider
          console.log 'adding'
          @chart.dataSets.push
            dataProvider: do node.observations.toJSON
            title: title
            fieldMappings: 
              [
                fromField: 'air_temp_1'
                toField: 'value'
              ]
            categoryField: 'timestamp'
            compared: true
        else
          if obs.get('air_temp_1')
            console.log 'updating'
            console.log obs.toJSON()
            console.log dataProvider.dataProvider
            dataProvider.dataProvider.push obs.toJSON()
          else
            console.log 'attribute not found'
    do @chart.validateData


  chartSettings: ->
    categoryAxesSettings:
      minPeriod: 'ss'
      groupToPeriods: [
        'mm'
        '10mm'
        '30mm'
        'hh'
      ]
      maxSeries: 100
    panelsSettings: recalculateToPercents: 'never'
    type: 'stock'
    panels: [ {
      showCategoryAxis: true
      title: ''
      percentHeight: 70
      stockGraphs: [ {
        id: 'g1'
        type: 'smoothedLine'
        valueField: 'value'
        comparable: true<commit_msg>Latest soil moisture data across 3 depth profiles for a single node plotted on chart<commit_after>define [
  'underscore'
  'jquery'
  'backbone'
  'amcharts'
  'amstock'
], (_, $, Backbone, amcharts)-> Backbone.View.extend
  el: '#soil-moisture-chart'

  sensors: [
      'field':'soil_moisture_1', 'title':'Deep'
    ,
      'field':'soil_moisture_2', 'title':'Surface'
    ,
      'field':'soil_moisture_3', 'title':'Shallow'
  ]

  initialize:->
    @chart = amcharts.makeChart @el, @chartSettings()
    @listenTo @collection, 'add', @updateChart

  ###
  Plot the moisture data for the currently selected node.  If no node has been selected then use
  the first one that data is recieved from.
  ###
  updateChart: (node) ->
    if not @nodeid?
      @nodeid = node.id

    if node.id == @nodeid
      node.observations.on 'add', (obs) =>
        sensorType = obs.get 'sensor'
        _.chain(@sensors).filter((f) -> obs.has(f.field) ).each (sensor) =>
          datasetTitle = node.id + " " + sensor.title
          dataProvider = _.findWhere(@chart.dataSets, {'title': datasetTitle})
          if not dataProvider
            @addToChart obs, node, datasetTitle, sensor
          else
            @endDate = new Date(obs.get('timestamp'))
            dataProvider.dataProvider.push obs.toJSON()
          do @chart.validateData
          @chart.zoom @startDate, @endDate

  ###
  Add a dataset to the chart
  ###
  addToChart: (obs, node, datasetTitle, sensor) ->
    @startDate = new Date(obs.get('timestamp'))
    @endDate = new Date(obs.get('timestamp'))
    @chart.dataSets.push
      dataProvider: do node.observations.toJSON
      title: datasetTitle
      fieldMappings: 
        [
          fromField: sensor.field
          toField: 'value'
        ]
      categoryField: 'timestamp'
      compared: true

  chartSettings: ->
    categoryAxesSettings:
      minPeriod: 'ss'
      groupToPeriods: [
        'mm'
        '10mm'
        '30mm'
        'hh'
      ]
      maxSeries: 100
    panelsSettings: recalculateToPercents: 'never'
    type: 'stock'
    panels: [ {
      showCategoryAxis: true
      title: ''
      percentHeight: 70
      stockGraphs: [ {
        id: 'g1'
        type: 'smoothedLine'
        valueField: 'value'
        comparable: true
{'fraction_non_alphanumeric': 0.11983289357959542, 'fraction_numerical': 0.018469656992084433, 'mean_word_length': 2.03874415497662, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '4813949', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1426, 'n_words': 321}