|
| 1 | +package csv |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "github.com/DanielFillol/DataJUD_API_CALLER/models" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +// WriteCode writes two CSV files with the given file name and folder name, and the data from the responses. One file for the Lawsuits and another for the lawsuit movements |
| 11 | +func WriteCode(fileName string, folderName string, responses []models.ResponseBodyNextPage) error { |
| 12 | + err := writeCode(fileName, folderName, responses) |
| 13 | + if err != nil { |
| 14 | + return err |
| 15 | + } |
| 16 | + return nil |
| 17 | +} |
| 18 | + |
| 19 | +// writeCode writes a CSV file with the given file name and folder name, and the data from the responses. |
| 20 | +func writeCode(fileName string, folderName string, responses []models.ResponseBodyNextPage) error { |
| 21 | + // Create a slice to hold all the rows for the CSV file |
| 22 | + var rows [][]string |
| 23 | + |
| 24 | + // Add the headers to the slice |
| 25 | + rows = append(rows, generateHeadersCode()) |
| 26 | + |
| 27 | + // Add the data rows to the slice |
| 28 | + for _, response := range responses { |
| 29 | + rows = append(rows, generateRowCode(response)...) |
| 30 | + } |
| 31 | + |
| 32 | + // Create the CSV file |
| 33 | + cf, err := createFile(folderName + "/" + fileName + "requestsCode.csv") |
| 34 | + if err != nil { |
| 35 | + log.Println(err) |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + // Close the file when the function completes |
| 40 | + defer cf.Close() |
| 41 | + |
| 42 | + // Create a new CSV writer |
| 43 | + w := csv.NewWriter(cf) |
| 44 | + |
| 45 | + // WriteLawsuits all the rows to the CSV file |
| 46 | + err = w.WriteAll(rows) |
| 47 | + if err != nil { |
| 48 | + log.Println(err) |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// generateHeadersCode function returns a slice of strings containing the header values for the CSV file. |
| 56 | +func generateHeadersCode() []string { |
| 57 | + return []string{ |
| 58 | + "Took", |
| 59 | + "Time Out", |
| 60 | + "Shards Total", |
| 61 | + "Shards Successful", |
| 62 | + "Shards Skipped", |
| 63 | + "Shards failed", |
| 64 | + "Hits Total Value", |
| 65 | + "Hits Total Relation", |
| 66 | + "Hits Max Score", |
| 67 | + "Index", |
| 68 | + "Type", |
| 69 | + "ID", |
| 70 | + "Score", |
| 71 | + "Class Code", |
| 72 | + "Class", |
| 73 | + "LawsuitNumber", |
| 74 | + "System Code", |
| 75 | + "System", |
| 76 | + "Format Code", |
| 77 | + "Format", |
| 78 | + "Court", |
| 79 | + "Last Update", |
| 80 | + "Degree", |
| 81 | + "Update Document", |
| 82 | + "Distribution Date", |
| 83 | + "Id", |
| 84 | + "Secrecy Level", |
| 85 | + "County Code IBGE", |
| 86 | + "County Code", |
| 87 | + "County", |
| 88 | + "Subjects Codes", |
| 89 | + "Subjects", |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// generateRowCode function takes in a single models.WriteStruct argument and returns a slice of strings containing the values to be written in a row of the CSV file. |
| 94 | +func generateRowCode(response models.ResponseBodyNextPage) [][]string { |
| 95 | + var rows [][]string |
| 96 | + |
| 97 | + for i, lawsuit := range response.Hit.Hits { |
| 98 | + row := []string{ |
| 99 | + // All those that repeat |
| 100 | + strconv.Itoa(response.Took), |
| 101 | + strconv.FormatBool(response.TimedOut), |
| 102 | + strconv.Itoa(response.Shards.Total), |
| 103 | + strconv.Itoa(response.Shards.Successful), |
| 104 | + strconv.Itoa(response.Shards.Skipped), |
| 105 | + strconv.Itoa(response.Shards.Failed), |
| 106 | + strconv.Itoa(response.Hit.Total.Value), |
| 107 | + response.Hit.Total.Relation, |
| 108 | + strconv.Itoa(int(response.Hit.MaxScore)), |
| 109 | + } |
| 110 | + |
| 111 | + // All those are unique |
| 112 | + row = append(row, lawsuit.Index) |
| 113 | + row = append(row, lawsuit.Type) |
| 114 | + row = append(row, lawsuit.Id) |
| 115 | + row = append(row, strconv.Itoa(int(lawsuit.Score))) |
| 116 | + row = append(row, strconv.Itoa(lawsuit.Source.Class.Code)) |
| 117 | + row = append(row, lawsuit.Source.Class.Name) |
| 118 | + row = append(row, lawsuit.Source.LawsuitNumber) |
| 119 | + row = append(row, strconv.Itoa(lawsuit.Source.System.Code)) |
| 120 | + row = append(row, lawsuit.Source.System.Name) |
| 121 | + row = append(row, strconv.Itoa(lawsuit.Source.Format.Code)) |
| 122 | + row = append(row, lawsuit.Source.Format.Name) |
| 123 | + row = append(row, lawsuit.Source.Court) |
| 124 | + row = append(row, lawsuit.Source.DateLastUpdate.String()) |
| 125 | + row = append(row, lawsuit.Source.Degree) |
| 126 | + row = append(row, lawsuit.Source.Timestamp.String()) |
| 127 | + row = append(row, lawsuit.Source.DistributionDate.String()) |
| 128 | + row = append(row, lawsuit.Source.Id) |
| 129 | + row = append(row, strconv.Itoa(lawsuit.Source.SecrecyLevel)) |
| 130 | + row = append(row, strconv.Itoa(lawsuit.Source.CourtInstance.CountyCodeIBGE)) |
| 131 | + row = append(row, strconv.Itoa(lawsuit.Source.CourtInstance.Code)) |
| 132 | + row = append(row, lawsuit.Source.CourtInstance.Name) |
| 133 | + var subjectsCodes string |
| 134 | + var subjects string |
| 135 | + for j, s := range lawsuit.Source.Subjects { |
| 136 | + if j != 0 { |
| 137 | + subjects += " | " + s.Name |
| 138 | + subjectsCodes += " | " + strconv.Itoa(s.Code) |
| 139 | + } else { |
| 140 | + subjects += s.Name |
| 141 | + subjectsCodes += strconv.Itoa(s.Code) |
| 142 | + } |
| 143 | + } |
| 144 | + row = append(row, subjectsCodes) |
| 145 | + row = append(row, subjects) |
| 146 | + |
| 147 | + rows = append(rows, row) |
| 148 | + } |
| 149 | + |
| 150 | + return rows |
| 151 | +} |
0 commit comments