graph.go 1010 Bytes
package main

import (
	"net/http"
	"strconv"
	"strings"

	"github.com/labstack/echo/v4"
)

func (app *App) GetGraph(c echo.Context) error {
	sep := strings.Split(c.Param("extractions"), ",")
	extractions := map[string]uint64{}
	for _, s := range sep {
		var phone string
		app.db.Get(&phone, "SELECT phone FROM extractions WHERE `no`=?", s)
		extractions[phone], _ = strconv.ParseUint(s, 10, 64)
	}

	calls := map[[2]string]int{}
	for p, e := range extractions {
		rows, _ := app.db.Query("SELECT `number`, COUNT(1) FROM calls WHERE extraction_no=? GROUP BY `number`", e)
		for rows.Next() {
			var number string
			var count int
			rows.Scan(&number, &count)

			if _, ok := calls[[2]string{p, number}]; ok {
				continue
			}

			calls[[2]string{p, number}] = count
			calls[[2]string{number, p}] = count
		}
	}

	result := []interface{}{}
	for k, c := range calls {
		if k[0] < k[1] && c >= 5 {
			result = append(result, echo.Map{"numbers": k, "calls": c})
		}
	}

	return c.JSON(http.StatusOK, result)
}