<commit_before>#= require ../components/commonsense/lib/commonsense.js
#= require ../components/JavaScript-MD5/md5.min.js
#= require graph-1.3.0/graph-min.js

sense = null
graph = null

checkForSenseSession = () ->
  if $.cookie('session_id')?
    $('#authenticate form button').html 'Authenticated'
    $('#authenticate form .username').val $.cookie('username')
    sense = new Sense $.cookie('session_id')
    showDevices()
  else
    sense = new Sense


showDevices = (list) ->
  fillDevices list, ->
      $('#devices').fadeIn()

fillDevices = (list, cb) ->
  if typeof list is 'function'
    cb = list
    list = null
  list ||= '#devices ul'

  sense.devices (err, resp) ->
    list_html = ''
    list_html += "<li><a href='' data-id='#{device.id}' data-display='#{device.type}'>#{device.type} (#{device.id})</a></li>" for device in resp.object.devices
    $(list).html list_html

    # Simulate click of only a single device
    if resp.object.devices.length == 1
      $(list).find('a').click()

    cb() if cb?

showSensors = (device, list) ->
  fillSensors device, list, ->
    $('#sensors').fadeIn()

fillSensors = (device, list, cb) ->
  if typeof list is 'function'
    cb = list
    list = null
  list ||= '#sensors ul'

  sense.deviceSensors device, (err, resp) ->
    sorted_sensors = resp.object.sensors.sortBy (sensor) -> sensor.display_name
    list_html = ''
    list_html += "<li><a href='' data-id='#{sensor.id}' data-display='#{sensor.display_name}'>#{sensor.display_name} (#{sensor.id})</a></li>" for sensor in sorted_sensors
    $(list).html list_html

    # Simulate click, for dev purposes
    $(list).find('a').first().click()

    cb() if cb?


retrieveSensorTimespan = (id, cb) ->

  first_datapoint_call = ->
    defer = $.Deferred()
    sense.sensorData id, {per_page: 1}, (err, resp) ->
      defer.resolve resp.object.data[0]
    return defer.promise()

  last_datapoint_call = ->
    defer = $.Deferred()
    sense.sensorData id, {last: true}, (err, resp) ->
      defer.resolve resp.object.data[0]
    return defer.promise()

  $.when(first_datapoint_call(), last_datapoint_call()).done (first, last) ->
    console.log 'All data received', first, last


plotSensorData = (id) ->
  sense.sensorData id, (err, resp) ->
    
    data = []
    data.push {date: new Date(datum.date*1000), value: JSON.parse(datum.value)['x-axis']} for datum in resp.object.data

    graph.draw [label: 'X-axis', data: data], {min: data[0].date, max: data[data.length-1].date}
    
    $('#actions').fadeIn()

callSegmentation = (sensor, cb) ->

  $.ajax<commit_msg>Plot multiple [numeric] axis<commit_after>#= require ../components/commonsense/lib/commonsense.js
#= require ../components/JavaScript-MD5/md5.min.js
#= require graph-1.3.0/graph-min.js

sense = null
graph = null

checkForSenseSession = () ->
  if $.cookie('session_id')?
    $('#authenticate form button').html 'Authenticated'
    $('#authenticate form .username').val $.cookie('username')
    sense = new Sense $.cookie('session_id')
    showDevices()
  else
    sense = new Sense


showDevices = (list) ->
  fillDevices list, ->
      $('#devices').fadeIn()

fillDevices = (list, cb) ->
  if typeof list is 'function'
    cb = list
    list = null
  list ||= '#devices ul'

  sense.devices (err, resp) ->
    list_html = ''
    list_html += "<li><a href='' data-id='#{device.id}' data-display='#{device.type}'>#{device.type} (#{device.id})</a></li>" for device in resp.object.devices
    $(list).html list_html

    # Simulate click of only a single device
    if resp.object.devices.length == 1
      $(list).find('a').click()

    cb() if cb?

showSensors = (device, list) ->
  fillSensors device, list, ->
    $('#sensors').fadeIn()

fillSensors = (device, list, cb) ->
  if typeof list is 'function'
    cb = list
    list = null
  list ||= '#sensors ul'

  sense.deviceSensors device, (err, resp) ->
    sorted_sensors = resp.object.sensors.sortBy (sensor) -> sensor.display_name
    list_html = ''
    list_html += "<li><a href='' data-id='#{sensor.id}' data-display='#{sensor.display_name}'>#{sensor.display_name} (#{sensor.id})</a></li>" for sensor in sorted_sensors
    $(list).html list_html

    # Simulate click, for dev purposes
    $(list).find('a').first().click()

    cb() if cb?


retrieveSensorTimespan = (id, cb) ->

  first_datapoint_call = ->
    defer = $.Deferred()
    sense.sensorData id, {per_page: 1}, (err, resp) ->
      defer.resolve resp.object.data[0]
    return defer.promise()

  last_datapoint_call = ->
    defer = $.Deferred()
    sense.sensorData id, {last: true}, (err, resp) ->
      defer.resolve resp.object.data[0]
    return defer.promise()

  $.when(first_datapoint_call(), last_datapoint_call()).done (first, last) ->
    console.log 'All data received', first, last


plotSensorData = (id) ->
  sense.sensorData id, (err, resp) ->
    
    # data = []
    # data.push {date: new Date(datum.date*1000), value: JSON.parse(datum.value)['x-axis']} for datum in resp.object.data

    datasets = []

    # Check for single or multi-valued data points
    first_object = JSON.parse(resp.object.data[0].value)
    
    if typeof first_object.value is "object"
      # Multiple values, assume numeric
      for key of first_object.value

        data = []
        data.push {date: new Date(datum.date*1000), value: JSON.parse(datum.value)[key]} for datum in resp.object.data
        datasets.push {label: key, data: data}

    else
      # Single value (assume numeric)
      data = []
      data.push {date: new Date(datum.date*1000), value: JSON.parse(datum.value)} for datum in resp.object.data
      datasets.push {label: 'Sensor ' + id, data: data }

    graph.draw datasets, {min: data[0].date, max: data[data.length-1].date}
    graph.setValueRangeAuto()
    $('#actions').fadeIn()

callSegmentation = (sensor, cb) ->

  $.ajax
{'fraction_non_alphanumeric': 0.17198975234842015, 'fraction_numerical': 0.006660973526900086, 'mean_word_length': 3.252723311546841, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, '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': '5731541', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1965, 'n_words': 469}