-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
334 lines (276 loc) · 9.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
<<<<<<< HEAD
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"path/filepath"
"strings"
"sync"
"github.com/hbollon/go-edlib"
"github.com/makaires77/ppgcs/pkg/infrastructure/mongo"
"github.com/makaires77/ppgcs/pkg/infrastructure/neo4jclient"
"github.com/makaires77/ppgcs/pkg/infrastructure/scrap_lattes"
"github.com/makaires77/ppgcs/pkg/interfaces/rabbitmq"
"github.com/makaires77/ppgcs/pkg/usecase/fuzzysearch"
"github.com/makaires77/ppgcs/pkg/usecase/load_lattes"
"github.com/streadway/amqp"
)
type Researcher struct {
Name string
}
type ScrapLattes struct {
neo4jClient *neo4jclient.Neo4jClient
}
func NewScrapLattes(neo4jClient *neo4jclient.Neo4jClient) *ScrapLattes {
return &ScrapLattes{
neo4jClient: neo4jClient,
}
}
func fuzz() {
fuzzyService := fuzzysearch.NewFuzzySearchService()
// Load data from CSV files
discentesData, _ := fuzzyService.LoadCSVData("_data/powerbi/lista_docentes_colaboradores.csv", 1)
docentesData, _ := fuzzyService.LoadCSVData("_data/powerbi/publicacoes.csv", 1)
autoresData, _ := fuzzyService.LoadCSVData("_data/powerbi/publicacoes.csv", 14)
discentes := discentesData
docentes := docentesData
autores := autoresData
// Create a map to track articles by docente
docenteArtigos := make(map[string]int)
for _, docente := range docentes {
docenteArtigos[docente] += 1
}
// Conduct fuzzy search for each docente
docentePercentage := make(map[string]float64)
for _, discente := range discentes {
for i, autoresArtigo := range autores {
res, err := fuzzyService.FuzzySearchSetThreshold(discente, strings.Split(autoresArtigo, ";"), 3, 0.7, edlib.Levenshtein)
if err != nil {
fmt.Println(err)
} else if len(res) > 0 {
docentePercentage[docentes[i]] += 1
}
}
}
// Calculate percentage of articles with similarity for each docente
for docente, count := range docentePercentage {
docentePercentage[docente] = (count / float64(docenteArtigos[docente])) * 100
fmt.Printf("Percentual de artigos com co-autoria de discentes para o docente '%s': %.2f%%\n", docente, docentePercentage[docente])
}
}
// ExecuteComparison executa a comparação dos nomes
func ExecuteFuzzComparison() {
fuzz()
}
func scrapeResearcherInfo(name string) (string, error) {
return "Informações do pesquisador: " + name, nil
}
func validateFile(fileName string, file io.Reader) error {
if file == nil {
return errors.New("o arquivo está vazio")
}
if ext := filepath.Ext(fileName); !strings.EqualFold(ext, ".csv") {
return errors.New("apenas arquivos .csv são permitidos")
}
return nil
}
func main() {
// Defina as informações de conexão com o Neo4j
uri := "bolt://localhost:7687"
username := "neo4j"
password := "password"
// Create a Neo4j connection
neo4jClient, err := neo4jclient.NewNeo4jClient(uri, username, password)
if err != nil {
log.Fatalf("Falha ao criar conexão com o Neo4j: %v", err)
}
// Create a MongoDB connection
mongoWriter, err := mongo.NewMongoWriter("mongodb://localhost:27017", "username", "password")
=======
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/makaires77/ppgcs/cmd/api/handlers"
"github.com/makaires77/ppgcs/pkg/application"
"github.com/makaires77/ppgcs/pkg/infrastructure/mongo"
"github.com/makaires77/ppgcs/pkg/infrastructure/neo4jclient"
"github.com/makaires77/ppgcs/pkg/repository"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
var server *http.Server
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Erro ao carregar .env file")
}
var (
uri = os.Getenv("MONGO_URI")
database = os.Getenv("MONGO_DATABASE")
collection = os.Getenv("MONGO_COLLECTION")
neo4j_uri = os.Getenv("NEO4J_URI")
neo4j_user = os.Getenv("NEO4J_USER")
neo4j_pass = os.Getenv("NEO4J_PASS")
serverAddress = os.Getenv("SERVER_ADDRESS")
)
// Criar uma nova instância de MongoDriver a partir do mongoWriter
mongoDriver, err := mongo.NewMongoDriver(uri, database, collection)
>>>>>>> c51253137853d9681efc37ff1382c4b6f7ed1174
if err != nil {
log.Fatalf("Falha ao criar conexão com o MongoDB: %v", err)
}
<<<<<<< HEAD
// Create a ScrapLattes instance
scrapLattes := scrap_lattes.NewScrapLattes(neo4jClient)
// Create a RabbitMQ connection
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Falha ao criar conexão com o RabbitMQ: %v", err)
}
// Create a RabbitMQ consumer
consumer := rabbitmq.NewConsumer(conn, "yourQueueName", scrapLattes)
if err != nil {
log.Fatalf("Falha ao criar consumidor RabbitMQ: %v", err)
}
// Start the RabbitMQ consumer
go consumer.Start()
// Create a Neo4jWriteLattes instance
neo4jWriteLattes, err := neo4jclient.NewNeo4jWriteLattes(uri, username, password)
if err != nil {
log.Fatalf("Falha ao criar instância do Neo4jWriteLattes: %v", err)
}
// Create a load_lattes.Interactor instance
interactor := load_lattes.NewInteractor(mongoWriter, neo4jWriteLattes)
// Create a RabbitMQ enqueuer
enqueuer, err := rabbitmq.NewEnqueueLattes(interactor, conn, "yourQueueName")
if err != nil {
log.Fatalf("Falha ao criar enfileirador RabbitMQ: %v", err)
}
http.HandleFunc("/start-scraping", func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Erro ao obter o arquivo", http.StatusInternalServerError)
return
}
defer file.Close()
// Validate the file
if err := validateFile(header.Filename, file); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Create a new CSV reader
csvReader := csv.NewReader(file)
// Channel to send researcher names to goroutines
names := make(chan string)
// WaitGroup to synchronize goroutines
var wg sync.WaitGroup
// Create 4 goroutines that process researcher names
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for name := range names {
// Call the scraping service
info, err := scrapeResearcherInfo(name)
if err != nil {
fmt.Printf("Erro ao processar o nome '%s': %s\n", name, err)
continue
}
fmt.Printf("Nome processado com sucesso: %s, Informações: %s\n", name, info)
}
}()
}
// Send researcher names to the channel
for {
record, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
http.Error(w, "Erro ao ler o arquivo CSV", http.StatusInternalServerError)
return
}
// Check if each line in the CSV has one field
if len(record) != 1 {
http.Error(w, "Cada linha do arquivo CSV deve conter exatamente um campo", http.StatusBadRequest)
return
}
names <- record[0]
}
// Close the channel to signal that there are no more names to process
close(names)
// Wait for all goroutines to finish
wg.Wait()
json.NewEncoder(w).Encode(map[string]string{
"message": "O processo de scraping foi iniciado",
})
})
http.HandleFunc("/enqueue-pesquisador", func(w http.ResponseWriter, r *http.Request) {
pesquisadorID := r.FormValue("pesquisadorID")
if pesquisadorID == "" {
http.Error(w, "Parâmetro pesquisadorID ausente", http.StatusBadRequest)
return
}
err := enqueuer.EnqueuePesquisador(pesquisadorID)
if err != nil {
http.Error(w, fmt.Sprintf("Falha ao enfileirar o pesquisadorID %s: %s", pesquisadorID, err), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(map[string]string{
"message": fmt.Sprintf("PesquisadorID %s enfileirado com sucesso!", pesquisadorID),
})
})
http.ListenAndServe(":8080", nil)
=======
ctx := context.Background()
neo4jDriver, err := neo4j.NewDriver(neo4j_uri, neo4j.BasicAuth(neo4j_user, neo4j_pass, ""), func(config *neo4j.Config) {
// Defina as configurações adicionais aqui, se necessário.
})
if err != nil {
log.Fatalf("Falha ao criar conexão com o Neo4j: %v", err)
}
defer neo4jDriver.Close()
neo4jSession := neo4jDriver.NewSession(neo4j.SessionConfig{})
defer neo4jSession.Close()
researcherRepo := repository.NewMongoDBRepository(mongoDriver)
neo4jClient, err := neo4jclient.NewNeo4jClient(ctx, neo4j_uri, neo4j_user, neo4j_pass)
if err != nil {
log.Fatalf("Failed to create Neo4j client: %v", err)
}
productionRepo := repository.NewNeo4jRepository(neo4jClient)
researcherService := application.NewResearcherService(researcherRepo)
productionService := application.NewProductionService(productionRepo)
r := mux.NewRouter()
r.HandleFunc("/api/researcher/{id}", handlers.GetResearcher(researcherService)).Methods("GET")
r.HandleFunc("/api/production/{id}", handlers.GetProductionHandler(productionService)).Methods("GET")
server = &http.Server{
Addr: serverAddress,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
server.Shutdown(ctx)
log.Println("Servidor desligando...")
os.Exit(0)
>>>>>>> c51253137853d9681efc37ff1382c4b6f7ed1174
}