-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.go
612 lines (541 loc) · 15.4 KB
/
program.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package main
import (
"encoding/csv"
"fmt"
"os"
"bufio"
"strconv"
"log"
"sync"
)
type Covid struct {
Region string `csv:"Region"` // .csv column headers
CodigoRegion string `csv:"Codigo region"`
Comuna string `csv:"Comuna"`
CodigoComuna string `csv:"Codigo comuna"`
Poblacion float64 `csv:"Poblacion"`
Fecha string `csv:"Fecha"`
Casos float64 `csv:"Casos confirmados"`
}
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}
func outputWriter_GA(final_result []<-chan map[string]float64){
var wg_ sync.WaitGroup
file, err := os.Create("result.csv")
checkError("Cannot create file", err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
wg_.Add(1)
for i := 0; i < 1; i++{
go func(n int, c <-chan map[string]float64) {
for rows := range c {
for key, val := range rows{
_ = writer.Write([]string{key, fmt.Sprintf("%f", val)})
}
}
defer wg_.Done()
}(i, final_result[i])
}
wg_.Wait()
}
func outputWriter_Selection(final_result []<- chan []Covid){
var wg_ sync.WaitGroup
file, err := os.Create("result.csv")
checkError("Cannot create file", err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
wg_.Add(1)
for i := 0; i < 1; i++{
go func(n int, c <-chan []Covid) {
for rows := range c {
for _, row := range rows{
//fmt.Println(row)
_ = writer.Write([]string{row.Region, row.CodigoRegion, row.Comuna, row.CodigoComuna, strconv.FormatFloat(row.Poblacion, 'f', -1, 64), row.Fecha, strconv.FormatFloat(row.Casos, 'f', -1, 64)})
}
}
defer wg_.Done()
}(i, final_result[i])
}
wg_.Wait()
}
func outputWriter_Projection(final_result []<-chan [][]string){
var wg_ sync.WaitGroup
file, err := os.Create("result.csv")
checkError("Cannot create file", err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
wg_.Add(1)
for i := 0; i < len(final_result); i++{
go func(n int, c <-chan [][]string) {
for rows := range c {
for _, row := range rows{
_ = writer.Write(row)
}
}
defer wg_.Done()
}(i, final_result[i])
}
wg_.Wait()
}
func Map_Select(datos [][]string) []Covid {
list := []Covid{}
for _, lista := range datos{
poblacion, _ := strconv.ParseFloat(lista[4], 64)
casos, _ := strconv.ParseFloat(lista[6], 64)
list = append(list, Covid{
Region: lista[0],
CodigoRegion: lista[1],
Comuna: lista[2],
CodigoComuna: lista[3],
Poblacion: poblacion,
Fecha: lista[5],
Casos: casos,
})
}
return list
}
func Map_Projection(datos [][]string, col_pedidas []string) [][]string {
filtered := [][]string{}
for _, dato := range datos{
new_line := []string{}
for _, word := range col_pedidas {
if word == "Region" {
new_line = append(new_line, dato[0])
}
if word == "Codigo region" {
new_line = append(new_line, dato[1])
}
if word == "Comuna" {
new_line = append(new_line, dato[2])
}
if word == "Codigo comuna" {
new_line = append(new_line, dato[3])
}
if word == "Poblacion" {
new_line = append(new_line, dato[4])
}
if word == "Fecha" {
new_line = append(new_line, dato[5])
}
if word == "Casos confirmados" {
new_line = append(new_line, dato[6])
}
}
filtered = append(filtered, new_line)
}
return filtered
}
func Reduce_GA(mapList chan map[string][]string, sendFinalValue chan map[string]float64, stdin_[]string){
reduccion := map[string]float64{}
operacion := stdin_[3]
if operacion == "AVG" {
for list := range mapList{
for key, value := range list{
var suma_total float64 = 0
for _, valor := range value{
cant, _ := strconv.ParseFloat(valor, 64)
suma_total = suma_total + cant
}
total_numeros := float64(len(value))
reduccion[key] = suma_total / total_numeros
//fmt.Println(reduccion[key])
}
}
}
if operacion == "MIN" {
for list := range mapList{
for key, value := range list{
var min_value float64
for _, valor := range value{
cant, _ := strconv.ParseFloat(valor, 64)
if cant < min_value {
min_value = cant
}
}
reduccion[key] = min_value
//fmt.Println(reduccion[key])
}
}
}
if operacion == "MAX" {
for list := range mapList{
for key, value := range list{
var max_value float64
for _, valor := range value{
cant, _ := strconv.ParseFloat(valor, 64)
if cant > max_value {
max_value = cant
}
}
reduccion[key] = max_value
//fmt.Println(reduccion[key])
}
}
}
if operacion == "SUM" {
for list := range mapList{
for key, value := range list{
var suma_tot float64
for _, valor := range value{
cant, _ := strconv.ParseFloat(valor, 64)
suma_tot = suma_tot + cant
}
reduccion[key] = suma_tot
//fmt.Println(reduccion[key])
}
}
}
sendFinalValue <- reduccion
close(sendFinalValue)
//wg.Done()
//fmt.Println(reduccion)
}
func Map_GA(datos [][]string, col_pedidas []string) map[string][]string {
mapeo := map[string][]string{}
//fmt.Println(col_pedidas)
columna := col_pedidas[0]
posicion := 0
// Sacar columna para agrupar
if columna == "Region" {
posicion = 0
}
if columna == "Codigo region" {
posicion = 1
}
if columna == "Comuna" {
posicion = 2
}
if columna == "Codigo comuna" {
posicion = 3
}
if columna == "Poblacion" {
posicion = 4
}
if columna == "Fecha" {
posicion = 5
}
if columna == "Casos confirmados" {
posicion = 6
}
// Sacar columna para agregar
col_agregacion := col_pedidas[2]
posicion_2 := 0
if col_agregacion == "Region" {
posicion_2 = 0
}
if col_agregacion == "Codigo region" {
posicion_2 = 1
}
if col_agregacion == "Comuna" {
posicion_2 = 2
}
if col_agregacion == "Codigo comuna" {
posicion_2 = 3
}
if col_agregacion == "Poblacion" {
posicion_2 = 4
}
if col_agregacion == "Fecha" {
posicion_2 = 5
}
if col_agregacion == "Casos confirmados" {
posicion_2 = 6
}
//fmt.Println("mapeo")
for _, dato := range datos{
mapeo[dato[posicion]] = append(mapeo[dato[posicion]], dato[posicion_2])
}
//fmt.Println(mapeo)
return mapeo
}
func Reducer_Projection(mapList chan [][]string, sendFinalValue chan [][]string){
filtered := [][]string{}
//valor_guardar := []string{}
var found int = 1
var found_2 int = 1
for list := range mapList {
// list: e.g.: [[Biobío Alto Biobio]]
for _, value := range list {
found = 1
// value: e.g.: [Biobío Alto Biobio]
for _, lista := range filtered {
found_2 = 1
for index := range lista{
if lista[index] != value[index] {
found_2 = 0
}
}
if found_2 == 1 {
found = 0
}
}
if found == 1 {
filtered = append(filtered, value)
}
}
}
//fmt.Println(filtered)
sendFinalValue <- filtered
close(sendFinalValue)
}
func Reducer_Select(mapList chan []Covid, sendFinalValue chan []Covid, data []string){
final := []Covid{}
col_name := data[0]
filter := data[1]
filter_value := data[2]
for list := range mapList {
for _, value := range list {
switch col_name {
// --------------------------- Comuna --------------------------------
case "Comuna":
switch filter {
case "==":
if value.Comuna == filter_value {
final = append(final, value)
}
case "!=":
if value.Comuna != filter_value {
final = append(final, value)
}
}
// --------------------------- Region --------------------------------
case "Region":
switch filter {
case "==":
if value.Region == filter_value {
final = append(final, value)
}
case "!=":
if value.Region != filter_value {
final = append(final, value)
}
}
// --------------------------- Casos --------------------------------
case "Casos confirmados":
var Casos_Int int = int(value.Casos)
filtro_int, _ := strconv.Atoi(filter_value)
switch filter {
case "==":
if Casos_Int == filtro_int {
final = append(final, value)
}
case "!=":
if Casos_Int != filtro_int {
final = append(final, value)
}
case "<":
if Casos_Int < filtro_int {
final = append(final, value)
}
case ">":
if Casos_Int > filtro_int {
final = append(final, value)
}
case "<=":
if Casos_Int <= filtro_int {
final = append(final, value)
}
case ">=":
if Casos_Int >= filtro_int {
final = append(final, value)
}
}
// --------------------------- Fecha --------------------------------
case "Fecha":
switch filter {
case "==":
if value.Fecha == filter_value {
final = append(final, value)
}
case "!=":
if value.Fecha != filter_value {
final = append(final, value)
}
case "<":
if value.Fecha < filter_value {
final = append(final, value)
}
case ">":
if value.Fecha > filter_value {
final = append(final, value)
}
case "<=":
if value.Fecha <= filter_value {
final = append(final, value)
}
case ">=":
if value.Fecha >= filter_value {
final = append(final, value)
}
}
// --------------------------- CodigoRegion --------------------------------
case "CodigoRegion":
switch filter {
case "==":
if value.CodigoRegion == filter_value {
final = append(final, value)
}
case "!=":
if value.CodigoRegion != filter_value {
final = append(final, value)
}
}
// --------------------------- CodigoComuna --------------------------------
case "CodigoComuna":
switch filter {
case "==":
if value.CodigoComuna == filter_value {
final = append(final, value)
}
case "!=":
if value.CodigoComuna != filter_value {
final = append(final, value)
}
}
}
}
}
sendFinalValue <- final
close(sendFinalValue)
}
func main() {
numThreads_txt := os.Args[1]
fmt.Println("Threads:", numThreads_txt)
numThreads, _ := strconv.Atoi(numThreads_txt)
// Read CSV
in, err := os.Open("./Covid-19_std.csv")
if err != nil {panic(err)}
defer in.Close()
lines, err := csv.NewReader(in).ReadAll()
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Comience entregando instrucciones: \n")
scanner.Scan()
text := scanner.Text()
var wg sync.WaitGroup
if text == "SELECT" {
wg.Add(numThreads)
// Crear canal
lists := make(chan []Covid)
finalValue := make(chan []Covid)
for counter := 0; counter < numThreads; counter++ {
if counter == numThreads - 1 {
go func(dato [][]string){
defer wg.Done()
lists <- Map_Select(dato)
}(lines[counter * (len(lines) / numThreads):])
}
if counter < numThreads - 1 {
go func(dato [][]string){
defer wg.Done()
lists <- Map_Select(dato)
}(lines[counter * (len(lines) / numThreads) : (counter + 1) * (len(lines) / numThreads)])
}
}
fmt.Print("Ha elegido SELECT. Indique Col_Name, Filtro y Valor \n")
// Manejo de inputs
inputs_select := make([]string, 0)
scanner.Scan()
COL_NAME := scanner.Text()
inputs_select = append(inputs_select, COL_NAME)
scanner.Scan()
FILTER := scanner.Text()
inputs_select = append(inputs_select, FILTER)
scanner.Scan()
VALUE := scanner.Text()
inputs_select = append(inputs_select, VALUE)
// Reduce
go Reducer_Select(lists, finalValue, inputs_select)
// Esperar y cerrar canal
wg.Wait()
close(lists)
outputWriter_Selection([]<-chan []Covid{finalValue})
}
if text == "PROJECTION"{
wg.Add(numThreads)
fmt.Print("Ha elegido PROJECTION. Indique N_Columnas y las N Columnas\n")
lists := make(chan [][]string)
finalValue := make(chan [][]string)
inputs_projection := make([]string, 0)
scanner.Scan()
N_COL := scanner.Text()
N_COL_INT, _ := strconv.Atoi(N_COL)
for i := 0; i < N_COL_INT; i++ {
scanner.Scan()
COL_N := scanner.Text()
inputs_projection = append(inputs_projection, COL_N)
}
//fmt.Println(lines[1 * (len(lines) / 2):])
for counter := 0; counter < numThreads; counter++ {
//fmt.Println("Hola")
if counter == numThreads - 1 {
go func(datos [][]string){
defer wg.Done()
lists <- Map_Projection(datos, inputs_projection)
}(lines[counter * (len(lines) / numThreads):])
}
if counter < numThreads - 1 {
go func(datos [][]string){
defer wg.Done()
//fmt.Println(datos)
lists <- Map_Projection(datos, inputs_projection)
}(lines[counter * (len(lines) / numThreads) : (counter + 1) * (len(lines) / numThreads)])
}
}
go Reducer_Projection(lists, finalValue)
wg.Wait()
close(lists)
outputWriter_Projection([]<-chan [][]string{finalValue})
//fmt.Println(<-finalValue)
}
if text == "GROUP"{
wg.Add(numThreads)
fmt.Print("Ha elegido GROUP. Indique COL_0 / AGGREGATE / COL_1 / FUNCTION\n")
inputs_group := make([]string, 0)
lista_mapeo := make(chan map[string][]string)
finalValue := make(chan map[string]float64)
scanner.Scan()
COL_NAME_0 := scanner.Text()
scanner.Scan()
AGGREGATE := scanner.Text()
scanner.Scan()
COL_NAME_1 := scanner.Text()
scanner.Scan()
FUNCTION := scanner.Text() // MIN, MAX,AVG,SUM.
inputs_group = append(inputs_group, COL_NAME_0)
inputs_group = append(inputs_group, AGGREGATE)
inputs_group = append(inputs_group, COL_NAME_1)
inputs_group = append(inputs_group, FUNCTION)
//fmt.Println(lines[1 * (len(lines) / 2):])
for counter := 0; counter < numThreads; counter++ {
if counter == numThreads - 1 {
go func(datos [][]string){
defer wg.Done()
lista_mapeo <- Map_GA(datos, inputs_group)
}(lines[counter * (len(lines) / numThreads):])
}
if counter < numThreads - 1 {
go func(datos [][]string){
defer wg.Done()
lista_mapeo <- Map_GA(datos, inputs_group)
}(lines[counter * (len(lines) / numThreads) : (counter + 1) * (len(lines) / numThreads)])
}
}
go Reduce_GA(lista_mapeo, finalValue, inputs_group)
wg.Wait()
close(lista_mapeo)
outputWriter_GA([]<-chan map[string]float64{finalValue})
}
}
// Read CSV borrowed from https://stackoverflow.com/questions/24999079/reading-csv-file-in-go
// Read STDin from https://stackoverflow.com/questions/20895552/how-to-read-from-standard-input-in-the-console
// MapReduce example from
// Example N-2