Skip to content

Commit 97f2ba2

Browse files
committed
feat: write .csv functions
1 parent 0f92e64 commit 97f2ba2

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

csv/write.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package csv
2+
3+
import (
4+
"encoding/csv"
5+
"github.com/DanielFillol/DataJUD_API_CALLER/models"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strconv"
10+
)
11+
12+
// Write writes a CSV file with the given file name and folder name, and the data from the responses.
13+
func Write(fileName string, folderName string, responses []models.ResponseBody) error {
14+
// Create a slice to hold all the rows for the CSV file
15+
var rows [][]string
16+
17+
// Add the headers to the slice
18+
rows = append(rows, generateHeaders())
19+
20+
// Add the data rows to the slice
21+
for _, response := range responses {
22+
rows = append(rows, generateRow(response)...)
23+
}
24+
25+
// Create the CSV file
26+
cf, err := createFile(folderName + "/" + fileName + "requests.csv")
27+
if err != nil {
28+
log.Println(err)
29+
return err
30+
}
31+
32+
// Close the file when the function completes
33+
defer cf.Close()
34+
35+
// Create a new CSV writer
36+
w := csv.NewWriter(cf)
37+
38+
// Write all the rows to the CSV file
39+
err = w.WriteAll(rows)
40+
if err != nil {
41+
log.Println(err)
42+
return err
43+
}
44+
45+
return nil
46+
}
47+
48+
// createFile function takes in a file path and creates a file in the specified directory. It returns a pointer to the created file and an error if there is any.
49+
func createFile(p string) (*os.File, error) {
50+
if err := os.MkdirAll(filepath.Dir(p), 0770); err != nil {
51+
log.Println(err)
52+
return nil, err
53+
}
54+
return os.Create(p)
55+
}
56+
57+
// generateHeaders function returns a slice of strings containing the header values for the CSV file.
58+
//
59+
// I ignore here all the movements, i will to this extraction latter
60+
func generateHeaders() []string {
61+
return []string{
62+
"Took",
63+
"Time Out",
64+
"Shards Total",
65+
"Shards Successful",
66+
"Shards Skipped",
67+
"Shards failed",
68+
"Hits Total Value",
69+
"Hits Total Relation",
70+
"Hits Max Score",
71+
// Here its an [] return
72+
"Index",
73+
"Type",
74+
"ID",
75+
"Score",
76+
"LawsuitNumber",
77+
"Class Code",
78+
"Class",
79+
"System Code",
80+
"System",
81+
"Format Code",
82+
"Format",
83+
"Court",
84+
"Last Update",
85+
"Degree",
86+
"Update Document",
87+
"Distribution Date",
88+
"Id",
89+
"Secrecy Level",
90+
"County Code IBGE",
91+
"County Code",
92+
"County",
93+
// Here its an [] return
94+
"Subjects Codes",
95+
"Subjects",
96+
}
97+
}
98+
99+
// generateRow 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.
100+
func generateRow(response models.ResponseBody) [][]string {
101+
var rows [][]string
102+
103+
// Append subjects Codes
104+
var subjectsCodes string
105+
for _, hit := range response.Hit.Hits {
106+
for i, s := range hit.Source.Subjects {
107+
if i != 0 {
108+
subjectsCodes += " | " + strconv.Itoa(s.Code)
109+
} else {
110+
subjectsCodes += strconv.Itoa(s.Code)
111+
}
112+
}
113+
}
114+
115+
// Append subjects
116+
117+
var subjects string
118+
for _, hit := range response.Hit.Hits {
119+
for i, s := range hit.Source.Subjects {
120+
if i != 0 {
121+
subjects += " | " + s.Name
122+
} else {
123+
subjects += s.Name
124+
}
125+
}
126+
}
127+
128+
for _, lawsuit := range response.Hit.Hits {
129+
row := []string{
130+
// All those that repeat
131+
strconv.Itoa(response.Took),
132+
strconv.FormatBool(response.TimedOut),
133+
strconv.Itoa(response.Shards.Total),
134+
strconv.Itoa(response.Shards.Successful),
135+
strconv.Itoa(response.Shards.Skipped),
136+
strconv.Itoa(response.Shards.Failed),
137+
strconv.Itoa(response.Hit.Total.Value),
138+
response.Hit.Total.Relation,
139+
strconv.Itoa(int(response.Hit.MaxScore)),
140+
}
141+
142+
// All those are unique
143+
row = append(row, lawsuit.Index)
144+
row = append(row, lawsuit.Type)
145+
row = append(row, lawsuit.Id)
146+
row = append(row, strconv.Itoa(int(lawsuit.Score)))
147+
row = append(row, lawsuit.Source.LawsuitNumber)
148+
row = append(row, strconv.Itoa(lawsuit.Source.Class.Code))
149+
row = append(row, lawsuit.Source.Class.Name)
150+
row = append(row, strconv.Itoa(lawsuit.Source.System.Code))
151+
row = append(row, lawsuit.Source.System.Name)
152+
row = append(row, strconv.Itoa(lawsuit.Source.Format.Code))
153+
row = append(row, lawsuit.Source.Format.Name)
154+
row = append(row, lawsuit.Source.Court)
155+
row = append(row, lawsuit.Source.DateLastUpdate.String())
156+
row = append(row, lawsuit.Source.Degree)
157+
row = append(row, lawsuit.Source.Timestamp.String())
158+
row = append(row, lawsuit.Source.DistributionDate.String())
159+
row = append(row, lawsuit.Source.Id)
160+
row = append(row, strconv.Itoa(lawsuit.Source.SecrecyLevel))
161+
row = append(row, strconv.Itoa(lawsuit.Source.CourtInstance.CountyCodeIBGE))
162+
row = append(row, strconv.Itoa(lawsuit.Source.CourtInstance.Code))
163+
row = append(row, lawsuit.Source.CourtInstance.Name)
164+
row = append(row, subjectsCodes)
165+
row = append(row, subjects)
166+
rows = append(rows, row)
167+
}
168+
169+
return rows
170+
}

0 commit comments

Comments
 (0)