Пример #1
0
func (ec *ExportController) Export(w http.ResponseWriter, r *http.Request) ([]byte, error) {
	vars := mux.Vars(r)
	fileGUID := vars["fileGUID"]
	templateGUID := vars["templateGUID"]

	fs := files.NewFileService(database.GetDatabase())
	fileObj, err := fs.GetFile(fileGUID)

	ts := templates.NewTemplateService(database.GetDatabase())
	templateObj, err := ts.GetTemplate(templateGUID)

	var result []string
	switch fileObj.Extension {
	case "csv":
		{
			fileName, _ := fileObj.DownloadFromUrl("storage")
			csv := csv.NewCSVService(fileName, true)
			for _, field := range templateObj.Fields {
				col := csv.GetColumn(field.Field)
				result = transformations.ApplyTransformations(col, field.Transformations)
				fmt.Println(result)
			}
		}
	}

	//tj,_ := json.Marshal(t)
	fj, _ := json.Marshal(fileObj)
	return fj, err
}
Пример #2
0
func (fs *FileService) UpdateFile(subdom string, fileGUID string, templateGUID string) ([]*templates.TransformationUpdateParams, error) {
	var newTrans []*templates.TransformationUpdateParams
	fileObj, err := fs.GetFile(fileGUID)
	ts := templates.NewTemplateService(database.GetDatabase())
	templateObj, err := ts.GetTemplate(templateGUID)

	switch fileObj.Extension {
	case "csv":
		{
			tempFile, _ := ioutil.TempFile("storage", "temp")
			fileName, _ := fileObj.DownloadFromUrl("storage")

			//Open file and apply transformations
			finalCSVData := make([][]string, len(templateObj.Fields)+1)
			csv := csvElement.NewCSVService(fileName, true)
			for i, field := range templateObj.Fields {
				col := csv.GetColumn(field.Field)
				result, allNewDS := transformations.ApplyTransformations(col, field.Transformations, templateGUID)

				if len(allNewDS) > 0 {
					for _, newDS := range allNewDS {
						//create and store new Datasource
						var settings []datasources.Setting
						for _, aMap := range newDS {
							newSetting := datasources.Setting{
								Label: aMap.Old,
								Value: aMap.New,
							}
							settings = append(settings, newSetting)
						}
						ds := datasources.NewDatasourceService(database.GetDatabase())
						dsGUID := ds.CreateDatasource(subdom, settings)
						/* Can't update template; other datasets might use it.  Instead, save the data in the queue
						//update Template
						ts := templates.NewTemplateService(database.GetDatabase())
						ts.AddTransformation(templateGUID, field.Field, "map", dsGUID)
						*/
						newTrans = append(newTrans, templates.NewTransformationUpdateParams(templateGUID, field.Field, "map", dsGUID))
					}
				}

				finalCSVData[i] = make([]string, len(result))
				for j, finalResult := range result {
					finalCSVData[i][j] = finalResult
				}
			}

			finalCSVData[len(templateObj.Fields)] = csv.GetColumn("ID")

			//Create local temporary file with transformed data
			newcsv := csvElement.NewCSVService(tempFile.Name(), false)
			newcsv.Headers = csv.Headers
			finalCSVData = transpose(finalCSVData)
			newcsv.Write(finalCSVData, true)

			//Send To AWS S3
			fs.SendToS3(fileObj.Path, tempFile.Name())

			//Delete temporary files
			os.Remove(fileName)
			os.Remove(tempFile.Name())
		}
	}
	return newTrans, err
}