Skip to content

Commit

Permalink
Working on batch file selection for batch workloads. Messy.
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Oct 12, 2023
1 parent 0c8a579 commit e778b1c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
5 changes: 4 additions & 1 deletion core/workflow_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ func (wb *WorkflowBatch) checkRequiredTags(record *util.NameValuePairList, lineN

func (wb *WorkflowBatch) ToForm() *Form {
form := NewForm("WorkflowBatch", "ID not applicable to this type", wb.Errors)
form.AddField("PathToCSVFile", "CSV Batch File", wb.PathToCSVFile, true)

// TODO: Rename field PathToCSVFile in form.
csvField := form.AddField("PathToCSVFile", "CSV Batch File", wb.PathToCSVFile, true)
csvField.Attrs["accept"] = ".csv"
workflowID := ""
if wb.Workflow != nil {
workflowID = wb.Workflow.ID
Expand Down
1 change: 1 addition & 0 deletions core/workflow_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestNewWorkflowBatchForm(t *testing.T) {

// Make sure path field is present and set correctly
assert.Equal(t, "/path/to/file.csv", form.Fields["PathToCSVFile"].Value)
assert.Equal(t, ".csv", form.Fields["PathToCSVFile"].Attrs["accept"])

// Now test the workflow choices. This should appear in alpha
// order, and Workflow 3 should be selected.
Expand Down
41 changes: 39 additions & 2 deletions server/controllers/workflows_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"time"

"github.com/APTrust/dart-runner/constants"
Expand Down Expand Up @@ -194,14 +197,48 @@ func WorkflowShowBatchForm(c *gin.Context) {
// POST /workflows/batch/validate
func WorkflowBatchValidate(c *gin.Context) {
workflowID := c.PostForm("WorkflowID")
pathToCSVFile := c.PostForm("PathToCSVFile")

//pathToCSVFile := c.PostForm("PathToCSVFile")

// TODO: Rename field PathToCSVFile in form.
csvFileHeader, err := c.FormFile("PathToCSVFile")
if err != nil {
// TODO: Ensure proper json format
AbortWithErrorJSON(c, http.StatusBadRequest, err)
return
}

tempFile := path.Join(os.TempDir(), csvFileHeader.Filename)
err = c.SaveUploadedFile(csvFileHeader, tempFile)
if err != nil {
AbortWithErrorJSON(c, http.StatusInternalServerError, err)
return
}

// csvFile, err := csvFileHeader.Open()
// if err != nil {
// AbortWithErrorHTML(c, http.StatusBadRequest, err)
// return
// }
// csvData := make([]byte, csvFileHeader.Size)
// _, err = csvFile.Read(csvData)
// if err != nil {
// AbortWithErrorHTML(c, http.StatusBadRequest, err)
// return
// }

workflow := core.ObjFind(workflowID).Workflow() // may be nil if workflowID is empty
wb := core.NewWorkflowBatch(workflow, pathToCSVFile)
wb := core.NewWorkflowBatch(workflow, tempFile)
status := http.StatusOK
data := gin.H{}
if !wb.Validate() {
status = http.StatusBadRequest
data["errors"] = wb.Errors
} else {
queryParams := url.Values{}
queryParams.Set("WorkflowID", workflowID)
queryParams.Set("PathToCSVFile", tempFile)
data["location"] = fmt.Sprintf("/workflows/batch/run?%s", queryParams.Encode())
}
data["status"] = status
c.JSON(status, data)
Expand Down
8 changes: 4 additions & 4 deletions server/views/partials/input_file.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{ define "partials/input_file.html" }}

<div class="custom-file {{ .field.FormGroupClass }}">
<div class="form-group {{ .field.FormGroupClass }}">
<label for="{{ .field.ID }}" class="control-label {{ if .field.Help }}clickable{{ end }}" for="{{ .field.ID }}"
{{ if .field.Help }}data-toggle="popover" data-trigger="hover" title="{{ .field.Label }}" data-content="{{ .field.Help }}"{{ end }}>{{ .field.Label }} {{ if .field.Attrs.required}}<i class="fas fa-asterisk fa-xs text-danger"></i>{{ end }} {{ if .field.Help }}<i class="fas fa-question-circle text-primary"></i>{{ end }}</label>
<input type="file" id="{{ .field.ID }}" name="{{ .field.Name }}"
class="custom-file-input {{ template "partials/classes.html" .field }}"
class="form-control {{ template "partials/classes.html" .field }}"
{{ template "partials/attrs.html" .field }} />
<label for="{{ .field.ID }}" class="custom-file-label {{ if .field.Help }}clickable{{ end }}" for="{{ .field.ID }}"
{{ if .field.Help }}data-toggle="popover" data-trigger="hover" title="{{ .field.Label }}" data-content="{{ .field.Help }}"{{ end }}>{{ .field.Label }} {{ if .field.Attrs.required}}<i class="fas fa-asterisk fa-xs text-danger"></i>{{ end }} {{ if .field.Help }}<i class="fas fa-question-circle text-primary"></i>{{ end }}</label>
<small id="{{ .field.ID }}Error" class="form-text text-danger">
{{ .field.Error }}
</small>
Expand Down
14 changes: 7 additions & 7 deletions server/views/workflow/batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ <h2>Workflow Batch</h2>
<a href="#Help/openExternal?url=https://aptrust.github.io/dart-docs/users/workflows/batch_jobs/">
batch workflow documentation.</a></p>

<form method="post" action="/workflows/batch/validate" id="workflowBatchForm">
<form method="post" action="/workflows/batch/validate" id="workflowBatchForm" enctype="multipart/form-data">

{{ template "partials/input_select.html" dict "field" .form.Fields.WorkflowID }}

<!-- // TODO: The input below should be some kind of file chooser -->

{{ template "partials/input_text.html" dict "field" .form.Fields.PathToCSVFile }}
{{ template "partials/input_file.html" dict "field" .form.Fields.PathToCSVFile }}

<div class="bottom-buttons">
<div class="pull-right">
Expand Down Expand Up @@ -63,10 +63,10 @@ <h3>Results</h3>
clearErrors()
let formId = '#workflowBatchForm'
let onSuccess = function(response) {
let url = '/workflows/batch/run?' + $(formId).serialize()
//console.log(response)
console.log(url)
runJob(url, '')
//let url = '/workflows/batch/run?' + $(formId).serialize()
console.log(response)
//console.log(url)
runJob(response.location, '')
}
let onFail = function(xhr, status, err) {
// display form validation errors below form controls
Expand All @@ -81,6 +81,7 @@ <h3>Results</h3>
}
displayValidationErrors(data.errors)
}
// TODO: Form needs to handle file submission.
submitFormInBackground(formId, onSuccess, onFail)
}

Expand Down Expand Up @@ -117,7 +118,6 @@ <h3>Results</h3>
$('#workflowBatchForm').on('submit', function() { validateAndRunBatch(); return false; })
});

// runJob('/workflows/batch/run/', '{{ .workflowBatchId }}')
</script>


Expand Down

0 comments on commit e778b1c

Please sign in to comment.