<commit_before>package forms

import (
	"encoding/json"
	"fmt"

	"github.com/Jeffail/gabs"
	"github.com/bep/gr"
	"github.com/bep/gr/el"
	"github.com/bep/gr/evt"
	"github.com/murdinc/awsmDashboard/helpers"
)

type SnapshotClassForm struct {
	*gr.This
}

// Implements the StateInitializer interface
func (s SnapshotClassForm) GetInitialState() gr.State {
	return gr.State{"querying": true, "queryingOpts": true, "queryingVolumes": true, "error": "", "success": "",
		"rotate":    false,
		"propagate": false,
		"step":      1,
	}
}

// Implements the ComponentWillMount interface
func (s SnapshotClassForm) ComponentWillMount() {
	var class map[string]interface{}

	if s.Props().Interface("class") != nil {
		classJson := s.Props().Interface("class").([]byte)
		json.Unmarshal(classJson, &class)
	}

	s.SetState(class)
	s.SetState(gr.State{"queryingOpts": true, "queryingVolumes": true})

	// Get our options for the form
	go func() {
		endpoint := "//localhost:8081/api/classes/" + s.Props().String("apiType") + "/options"
		resp, err := helpers.GetAPI(endpoint)
		if !s.IsMounted() {
			return
		}
		if err != nil {
			s.SetState(gr.State{"queryingOpts": false, "error": fmt.Sprintf("Error while querying endpoint: %s", endpoint)})
			return
		}

		s.SetState(gr.State{"classOptionsResp": resp, "queryingOpts": false})
	}()

	// Get our existing instances for the form
	go func() {
		endpoint := "//localhost:8081/api/assets/volumes"
		resp, err := helpers.GetAPI(endpoint)
		if !s.IsMounted() {
			return
		}
		if err != nil {
			s.SetState(gr.State{"queryingVolumes": false, "error": fmt.Sprintf("Error while querying endpoint: %s", endpoint)})
			return
		}

		s.SetState(gr.State{"volumeOptionsResp": resp, "queryingVolumes": false})
	}()
}

func (s SnapshotClassForm) Render() gr.Component {

	state := s.State()
	props := s.Props()

	// Form placeholder
	response := el.Div()

	// Print any alerts
	helpers.ErrorElem(state.String("error")).Modify(response)
	helpers.SuccessElem(state.String("success")).Modify(response)

	if state.Int("step") == 1 {
		if state.Bool("queryingOpts") || state.Bool("queryingVolumes") {
			gr.Text("Loading...").Modify(response)
		} else {
			s.BuildClassForm(props.String("className"), state.Interface("classOptionsResp"), state.Interface("volumeOptionsResp")).Modify(response)
		}

	} else if state.Int("step") == 2 {

		if state.Bool("querying") {
			gr.Text("Saving...").Modify(response)
		} else {

			buttons := el.Div(
				gr.CSS("btn-toolbar"),
			)

			// Back
			el.Button(
				evt.Click(s.backButton).PreventDefault(),
				gr.CSS("btn", "btn-secondary"),
				gr.Text("Back"),
			).Modify(buttons)

			// Done
			el.Button(
				evt.Click(s.doneButton).PreventDefault(),
				gr.CSS("btn", "btn-primary"),
				gr.Text("Done"),
			).Modify(buttons)

			buttons.Modify(response)
		}

	}

	return response
}

func (s SnapshotClassForm) BuildClassForm(className string, optionsResp interface{}, volumeResp interface{}) *gr.Element {

	state := s.State()
	props := s.Props()

	var classOptions map[string][]string
	jsonParsed, _ := gabs.ParseJSON(optionsResp.([]byte))
	classOptionsJson := jsonParsed.S("classOptions").Bytes()
	json.Unmarshal(classOptionsJson, &classOptions)

	volumeJsonParsed, _ := gabs.ParseJSON(volumeResp.([]byte))
	volumeOptionsSlice, _ := volumeJsonParsed.S("assets").Children()

	var volumes []string
	volumesMeta := make(map[string]string)
	for _, volumeOption := range volumeOptionsSlice {
		volume := volumeOption.S("volumeID").Data().(string)
		if volume != "" {
			volumes = append(volumes, volume)
			volumesMeta[volume] = volumeOption.S("sizeHuman").Data().(string) + " " + volumeOption.S("name").Data().(string) + " " + volumeOption.S("attachment").Data().(string)
		}
	}

	classEdit := el.Div(
		el.Header3(gr.Text(className)),
		el.HorizontalRule(),
	)

	classEditForm := el.Form()

	Checkbox("Rotate", "rotate", state.Bool("rotate"), s.storeValue).Modify(classEditForm)
	if state.Bool("rotate") {
		NumberField("Retain", "retain", state.Int("retain"), s.storeValue).Modify(classEditForm)
	}
	Checkbox("Propagate", "propagate", state.Bool("propagate"), s.storeValue).Modify(classEditForm)<commit_msg>adds description to snapshot class<commit_after>package forms

import (
	"encoding/json"
	"fmt"

	"github.com/Jeffail/gabs"
	"github.com/bep/gr"
	"github.com/bep/gr/el"
	"github.com/bep/gr/evt"
	"github.com/murdinc/awsmDashboard/helpers"
)

type SnapshotClassForm struct {
	*gr.This
}

// Implements the StateInitializer interface
func (s SnapshotClassForm) GetInitialState() gr.State {
	return gr.State{"querying": true, "queryingOpts": true, "queryingVolumes": true, "error": "", "success": "",
		"rotate":    false,
		"propagate": false,
		"step":      1,
	}
}

// Implements the ComponentWillMount interface
func (s SnapshotClassForm) ComponentWillMount() {
	var class map[string]interface{}

	if s.Props().Interface("class") != nil {
		classJson := s.Props().Interface("class").([]byte)
		json.Unmarshal(classJson, &class)
	}

	s.SetState(class)
	s.SetState(gr.State{"queryingOpts": true, "queryingVolumes": true})

	// Get our options for the form
	go func() {
		endpoint := "//localhost:8081/api/classes/" + s.Props().String("apiType") + "/options"
		resp, err := helpers.GetAPI(endpoint)
		if !s.IsMounted() {
			return
		}
		if err != nil {
			s.SetState(gr.State{"queryingOpts": false, "error": fmt.Sprintf("Error while querying endpoint: %s", endpoint)})
			return
		}

		s.SetState(gr.State{"classOptionsResp": resp, "queryingOpts": false})
	}()

	// Get our existing instances for the form
	go func() {
		endpoint := "//localhost:8081/api/assets/volumes"
		resp, err := helpers.GetAPI(endpoint)
		if !s.IsMounted() {
			return
		}
		if err != nil {
			s.SetState(gr.State{"queryingVolumes": false, "error": fmt.Sprintf("Error while querying endpoint: %s", endpoint)})
			return
		}

		s.SetState(gr.State{"volumeOptionsResp": resp, "queryingVolumes": false})
	}()
}

func (s SnapshotClassForm) Render() gr.Component {

	state := s.State()
	props := s.Props()

	// Form placeholder
	response := el.Div()

	// Print any alerts
	helpers.ErrorElem(state.String("error")).Modify(response)
	helpers.SuccessElem(state.String("success")).Modify(response)

	if state.Int("step") == 1 {
		if state.Bool("queryingOpts") || state.Bool("queryingVolumes") {
			gr.Text("Loading...").Modify(response)
		} else {
			s.BuildClassForm(props.String("className"), state.Interface("classOptionsResp"), state.Interface("volumeOptionsResp")).Modify(response)
		}

	} else if state.Int("step") == 2 {

		if state.Bool("querying") {
			gr.Text("Saving...").Modify(response)
		} else {

			buttons := el.Div(
				gr.CSS("btn-toolbar"),
			)

			// Back
			el.Button(
				evt.Click(s.backButton).PreventDefault(),
				gr.CSS("btn", "btn-secondary"),
				gr.Text("Back"),
			).Modify(buttons)

			// Done
			el.Button(
				evt.Click(s.doneButton).PreventDefault(),
				gr.CSS("btn", "btn-primary"),
				gr.Text("Done"),
			).Modify(buttons)

			buttons.Modify(response)
		}

	}

	return response
}

func (s SnapshotClassForm) BuildClassForm(className string, optionsResp interface{}, volumeResp interface{}) *gr.Element {

	state := s.State()
	props := s.Props()

	var classOptions map[string][]string
	jsonParsed, _ := gabs.ParseJSON(optionsResp.([]byte))
	classOptionsJson := jsonParsed.S("classOptions").Bytes()
	json.Unmarshal(classOptionsJson, &classOptions)

	volumeJsonParsed, _ := gabs.ParseJSON(volumeResp.([]byte))
	volumeOptionsSlice, _ := volumeJsonParsed.S("assets").Children()

	var volumes []string
	volumesMeta := make(map[string]string)
	for _, volumeOption := range volumeOptionsSlice {
		volume := volumeOption.S("volumeID").Data().(string)
		if volume != "" {
			volumes = append(volumes, volume)
			volumesMeta[volume] = volumeOption.S("sizeHuman").Data().(string) + " " + volumeOption.S("name").Data().(string) + " " + volumeOption.S("attachment").Data().(string)
		}
	}

	classEdit := el.Div(
		el.Header3(gr.Text(className)),
		el.HorizontalRule(),
	)

	classEditForm := el.Form()

	TextField("Description", "description", state.String("description"), s.storeValue).Modify(classEditForm)

	Checkbox("Rotate", "rotate", state.Bool("rotate"), s.storeValue).Modify(classEditForm)
	if state.Bool("rotate") {
		NumberField("Retain", "retain", state.Int("retain"), s.storeValue).Modify(classEditForm)
	}
	Checkbox("Propagate", "propagate", state.Bool("propagate"), s.storeValue).Modify(classEditForm)
{'fraction_non_alphanumeric': 0.17928756782259966, 'fraction_numerical': 0.0028308563340410475, 'mean_word_length': 4.549083769633508, 'pattern_counts': {'":': 36, '<': 3, '<?xml version=': 0, '>': 3, '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': '4638359', 'n_tokens_mistral': 3150, 'n_tokens_neox': 2887, 'n_words': 515}