<commit_before># An object with functions for interacting with API (rails controllers)

MCM.Client =
  # transform DDL, so it is easier to template using handlebars.js
  # and create the options for jquery.validation plugin
  # this is called from a successful requestDdl ajax call (below), then
  # the result sent out as a action:receiveDdl event on App.vent for
  # any view to receive
  transformDdl : (ddl, agent, action) ->
    actionDdl = ddl['actions'][action]
    
    if !actionDdl
      return
      
    validationRules = { }

    for own ddlKey of actionDdl.input
      validationRules[ddlKey] = {
        remote : {
          type : "POST"
          dataType : "text"
          url : "/ddls/"+agent+"/validate/"+action
        }
      }
      
      if actionDdl.input[ddlKey]['optional']
        validationRules[ddlKey]['required'] = !(actionDdl.input[ddlKey]['optional'])
      
      if actionDdl.input[ddlKey]['maxlength']
        validationRules[ddlKey]['maxlength'] = actionDdl.input[ddlKey]['maxlength']
          
      if actionDdl.input[ddlKey]['minlength']
        validationRules[ddlKey]['minlength'] = actionDdl.input[ddlKey]['maxlength']

      switch actionDdl.input[ddlKey].type
        when "string" then actionDdl.input[ddlKey]['isString'] = 1
        when "boolean" then actionDdl.input[ddlKey]['isBool'] = 1
        when "integer" then actionDdl.input[ddlKey]['isInteger'] = 1
        
    
    # when displaying result rows, this is used to make sure the columns
    # align properly (we don't have to rely on the same fields and order)
    columns = []
    outputs = actionDdl.output
    for own output of outputs
      columns.push(_.extend({key : output}, outputs[output]))

    return {
      agent : agent,
      action : action,
      validationRules : validationRules
      meta : ddl['meta']
      actionDdl : actionDdl
      columns : columns
    }

  # request a DDL, transform it with transformDdl, then send it out the event aggregator
  # as action:requestDdl
  requestDdl: (agent, action) ->
    that = @
    $.ajax({
      type: "GET"
      url: "/mcagents/" + agent
      success: (data) ->
        MCM.vent.trigger("action:receiveDdl", MCM.Client.transformDdl(data.ddl, agent, action))
        
      dataType: "json"
    })
  
  # gather the fields from the form and save them
  # into submission.args. convert booleans from strings
  # if the DDL says the field is a bool.
  
  submitActionForm: (submission) ->
    that = @
    submission.args = submission.args || {}
    _.each(submission.form.serializeArray(), (x) ->
      input_type = submission.ddl.input[x.name].type
      if input_type == "boolean"
        if x.value == "true"
          x.value = true
        else if x.value == "false"
          x.value = false
      else if input_type == "integer"
        x.value = parseInt(x.value) 
          
      submission.args[x.name] = x.value
    )
    
    @submitAction(submission)
<commit_msg>issue 6 and 11 - handle number DDL type as well as integer<commit_after># An object with functions for interacting with API (rails controllers)

MCM.Client =
  # transform DDL, so it is easier to template using handlebars.js
  # and create the options for jquery.validation plugin
  # this is called from a successful requestDdl ajax call (below), then
  # the result sent out as a action:receiveDdl event on App.vent for
  # any view to receive
  transformDdl : (ddl, agent, action) ->
    actionDdl = ddl['actions'][action]
    
    if !actionDdl
      return
      
    validationRules = { }

    for own ddlKey of actionDdl.input
      validationRules[ddlKey] = {
        remote : {
          type : "POST"
          dataType : "text"
          url : "/ddls/"+agent+"/validate/"+action
        }
      }
      
      if actionDdl.input[ddlKey]['optional']
        validationRules[ddlKey]['required'] = !(actionDdl.input[ddlKey]['optional'])
      
      if actionDdl.input[ddlKey]['maxlength']
        validationRules[ddlKey]['maxlength'] = actionDdl.input[ddlKey]['maxlength']
          
      if actionDdl.input[ddlKey]['minlength']
        validationRules[ddlKey]['minlength'] = actionDdl.input[ddlKey]['maxlength']

      switch actionDdl.input[ddlKey].type
        when "string" then actionDdl.input[ddlKey]['isString'] = 1
        when "boolean" then actionDdl.input[ddlKey]['isBool'] = 1
        when "integer", "number" then
          actionDdl.input[ddlKey]['isInteger'] = 1
          validationRules[ddlKey]['dataType'] = "number"

    # when displaying result rows, this is used to make sure the columns
    # align properly (we don't have to rely on the same fields and order)
    columns = []
    outputs = actionDdl.output
    for own output of outputs
      columns.push(_.extend({key : output}, outputs[output]))

    return {
      agent : agent,
      action : action,
      validationRules : validationRules
      meta : ddl['meta']
      actionDdl : actionDdl
      columns : columns
    }

  # request a DDL, transform it with transformDdl, then send it out the event aggregator
  # as action:requestDdl
  requestDdl: (agent, action) ->
    that = @
    $.ajax({
      type: "GET"
      url: "/mcagents/" + agent
      success: (data) ->
        MCM.vent.trigger("action:receiveDdl", MCM.Client.transformDdl(data.ddl, agent, action))
        
      dataType: "json"
    })
  
  # gather the fields from the form and save them
  # into submission.args. convert booleans from strings
  # if the DDL says the field is a bool.
  
  submitActionForm: (submission) ->
    that = @
    submission.args = submission.args || {}
    _.each(submission.form.serializeArray(), (x) ->
      input_type = submission.ddl.input[x.name].type
      if input_type == "boolean"
        if x.value == "true"
          x.value = true
        else if x.value == "false"
          x.value = false
      else if input_type in [ "integer", "number" ]
        x.value = parseInt(x.value) 
          
      submission.args[x.name] = x.value
    )
    
    @submitAction(submission)

{'fraction_non_alphanumeric': 0.11171887949610476, 'fraction_numerical': 0.0014917951268025858, 'mean_word_length': 2.5327868852459017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1076339', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1813, 'n_words': 508}