<commit_before>Model = require './model'

unpauseTemplate = require '../views/templates/vex/requestUnpause'
runTemplate = require '../views/templates/vex/requestRun'
removeTemplate = require '../views/templates/vex/requestRemove'
bounceTemplate = require '../views/templates/vex/requestBounce'

class Request extends Model
            
    parse: (data) ->
        if data.request?
            data.request.daemon = if _.isNull(data.request.daemon) then true else data.request.daemon
            data.daemon = data.request.daemon
            
            data.scheduled = utils.isScheduledRequest data.request
            data.onDemand = utils.isOnDemandRequest data.request

            data.displayState = constants.requestStates[data.state]

        data

    url: => "#{ config.apiRoot }/requests/request/#{ @get('id') }"

    deletePaused: =>
        $.ajax
            url: "#{ @url() }/paused"
            type: 'DELETE'

    unpause: =>
        $.ajax
            url: "#{ @url() }/unpause?user=#{app.getUsername()}"
            type: 'POST'

    pause: =>
        $.ajax
            url: "#{ @url() }/pause?user=#{app.getUsername()}"
            type: 'POST'

    run: (confirmedOrPromptData) ->
        options =
            url: "#{ @url() }/run?user=#{app.getUsername()}"
            type: 'POST'
            contentType: 'application/json'

        if _.isString confirmedOrPromptData
            options.data = confirmedOrPromptData
            options.processData = false
            options.contentType = 'text/plain'

        $.ajax options
        
    bounce: =>
        $.ajax
            url: "#{ @url() }/bounce?user=#{app.getUsername()}"
            type: "POST"

    destroy: =>
        $.ajax
            url: "#{ @url() }?user=#{app.getUsername()}"
            type: "DELETE"

    ###
    promptX opens a dialog asking the user to confirm an action and then does it
    ###
    promptUnpause: (callback) =>
        vex.dialog.confirm
            message: unpauseTemplate id: @get "id"
            callback: (confirmed) =>
                return unless confirmed
                @unpause().done callback

    promptRun: (callback) =>
        vex.dialog.prompt
            message: runTemplate id: @get "id"
            buttons: [
                $.extend vex.dialog.buttons.YES, text: 'Run now'
                vex.dialog.buttons.NO
            ]
            callback: (data) =>
                return if data is false
                @run(data).done callback

    promptRemove: (callback) =>
        vex.dialog.confirm
            message: removeTemplate id: @get "id"
            callback: (confirmed) =>
                return if not confirmed
                @destroy().done callback

    promptBounce: (callback) =>
        vex.dialog.confirm<commit_msg>Added pause prompt<commit_after>Model = require './model'

pauseTemplate = require '../views/templates/vex/requestPause'
unpauseTemplate = require '../views/templates/vex/requestUnpause'
runTemplate = require '../views/templates/vex/requestRun'
removeTemplate = require '../views/templates/vex/requestRemove'
bounceTemplate = require '../views/templates/vex/requestBounce'

class Request extends Model
            
    parse: (data) ->
        if data.request?
            data.request.daemon = if _.isNull(data.request.daemon) then true else data.request.daemon
            data.daemon = data.request.daemon
            
            data.scheduled = utils.isScheduledRequest data.request
            data.onDemand = utils.isOnDemandRequest data.request

            data.displayState = constants.requestStates[data.state]

        data

    url: => "#{ config.apiRoot }/requests/request/#{ @get('id') }"

    deletePaused: =>
        $.ajax
            url: "#{ @url() }/paused"
            type: 'DELETE'

    unpause: =>
        $.ajax
            url: "#{ @url() }/unpause?user=#{app.getUsername()}"
            type: 'POST'

    pause: =>
        $.ajax
            url: "#{ @url() }/pause?user=#{app.getUsername()}"
            type: 'POST'

    run: (confirmedOrPromptData) ->
        options =
            url: "#{ @url() }/run?user=#{app.getUsername()}"
            type: 'POST'
            contentType: 'application/json'

        if _.isString confirmedOrPromptData
            options.data = confirmedOrPromptData
            options.processData = false
            options.contentType = 'text/plain'

        $.ajax options
        
    bounce: =>
        $.ajax
            url: "#{ @url() }/bounce?user=#{app.getUsername()}"
            type: "POST"

    destroy: =>
        $.ajax
            url: "#{ @url() }?user=#{app.getUsername()}"
            type: "DELETE"

    ###
    promptX opens a dialog asking the user to confirm an action and then does it
    ###
    pomptPause: (callback) =>
        vex.dialog.confirm
            message: pauseTemplate id: @get "id"
            callback: (confirmed) =>
                return unless confirmed
                @pause().done callback

    promptUnpause: (callback) =>
        vex.dialog.confirm
            message: unpauseTemplate id: @get "id"
            callback: (confirmed) =>
                return unless confirmed
                @unpause().done callback

    promptRun: (callback) =>
        vex.dialog.prompt
            message: runTemplate id: @get "id"
            buttons: [
                $.extend vex.dialog.buttons.YES, text: 'Run now'
                vex.dialog.buttons.NO
            ]
            callback: (data) =>
                return if data is false
                @run(data).done callback

    promptRemove: (callback) =>
        vex.dialog.confirm
            message: removeTemplate id: @get "id"
            callback: (confirmed) =>
                return if not confirmed
                @destroy().done callback

    promptBounce: (callback) =>
        vex.dialog.confirm
{'fraction_non_alphanumeric': 0.12598156367360874, 'fraction_numerical': 0.0, 'mean_word_length': 1.7741477272727273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '4611852', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1566, 'n_words': 367}