Commit cb61fd40 authored by M. Rehan Abbasi's avatar M. Rehan Abbasi
Browse files

add logic to populate grid map table in traffic manager

parent 35e6312e
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -156,6 +156,14 @@ func Init(cfg SbiCfg) (err error) {
	}
	log.Info("Created new VIS DB tables")

	// Populate VIS DB Grid Map Table
	err = sbi.trafficMgr.PopulateGridMapTable()
	if err != nil {
		log.Error("Failed to populate grid map table: ", err)
		return err
	}
	log.Info("Populated VIS DB grid map table")

	// Populate VIS DB Categories Table
	err = sbi.trafficMgr.PopulateCategoryTable()
	if err != nil {
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ go 1.16
require (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger v0.0.0
	github.com/lib/pq v1.5.2
	gopkg.in/yaml.v2 v2.4.0
)

replace github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger => ../../go-packages/meep-logger
+4 −0
Original line number Diff line number Diff line
@@ -13,3 +13,7 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+94 −3
Original line number Diff line number Diff line
@@ -19,9 +19,12 @@ package vistrafficmgr
import (
	"database/sql"
	"errors"
	"io/ioutil"
	"strings"
	"time"

	"gopkg.in/yaml.v2"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"

	_ "github.com/lib/pq"
@@ -62,6 +65,11 @@ const (
	TrafficTable  = "traffic_patterns"
)

// Grid Map file name
const gridFile = "grid_map.yaml"

var yamlFile []byte

// Category-wise Traffic Loads
var categoriesLoads = map[string]map[string]int32{
	"commercial": {
@@ -84,7 +92,7 @@ var categoriesLoads = map[string]map[string]int32{
		"1800-2100": 15,
		"2100-2400": 8,
	},
	"beach": {
	"coastal": {
		"0000-0300": 2,
		"0300-0600": 5,
		"0600-0900": 8,
@@ -193,6 +201,13 @@ func NewTrafficMgr(name, namespace, user, pwd, host, port string) (tm *TrafficMg
		return nil, err
	}

	// Open grid map file
	yamlFile, err = ioutil.ReadFile(gridFile)
	if err != nil {
		log.Error("Failed to open grid map file with err: ", err.Error())
		return nil, err
	}

	log.Info("Postgis Connector successfully created")
	tm.connected = true
	return tm, nil
@@ -301,10 +316,10 @@ func (tm *TrafficMgr) CreateTables() (err error) {

	// Grid Table
	_, err = tm.db.Exec(`CREATE TABLE ` + GridTable + ` (
		id              varchar(36)             NOT NULL,
		area            varchar(100)            NOT NULL,
		category				varchar(100)						NOT NULL,
		grid						geometry(POLYGON,4326),
		PRIMARY KEY (id)
		PRIMARY KEY (area)
	)`)
	if err != nil {
		log.Error(err.Error())
@@ -373,6 +388,43 @@ func (tm *TrafficMgr) DeleteTable(tableName string) (err error) {
	return nil
}

// CreateGridMap - Create new Grid Map
func (tm *TrafficMgr) CreateGridMap(area string, category string, grid string) (err error) {
	if profiling {
		profilingTimers["CreateGridMap"] = time.Now()
	}

	// Validate input
	if area == "" {
		return errors.New("Missing area name")
	}
	if category == "" {
		return errors.New("Missing category name")
	}
	if grid == "" {
		return errors.New("Missing grid polygon data")
	}

	// Create Grid Map entry
	query := `INSERT INTO ` + GridTable +
		` (area, category, grid)
			VALUES ($1, $2, ST_GeomFromEWKT('SRID=4326;POLYGON(` + grid + `)'))`
	_, err = tm.db.Exec(query, area, category)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	// Notify listener
	// tm.notifyListener(TypePoa, name)

	if profiling {
		now := time.Now()
		log.Debug("CreateGridMap: ", now.Sub(profilingTimers["CreateGridMap"]))
	}
	return nil
}

// CreateCategoryLoad - Create new Category Load
func (tm *TrafficMgr) CreateCategoryLoad(category string, data map[string]int32) (err error) {
	if profiling {
@@ -742,6 +794,45 @@ func (tm *TrafficMgr) DeleteAllPoaLoad() (err error) {
	return nil
}

// PopulateGridMapTable - Populate the grid_map table
func (tm *TrafficMgr) PopulateGridMapTable() (err error) {
	if profiling {
		profilingTimers["PopulateGridMapTable"] = time.Now()
	}

	// Get grid map from YAML file
	var yamlGrid map[string]map[string][]string
	err = yaml.Unmarshal(yamlFile, &yamlGrid)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	for category, areas := range yamlGrid {
		for area, points := range areas {
			polygonStr := "("
			for i, pointStr := range points {
				if i != len(points)-1 {
					polygonStr = polygonStr + pointStr + ", "
				} else {
					polygonStr = polygonStr + pointStr + ")"
				}
			}
			err = tm.CreateGridMap(area, category, polygonStr)
			if err != nil {
				log.Error(err.Error())
				return err
			}
		}
	}

	if profiling {
		now := time.Now()
		log.Debug("PopulateGridMapTable: ", now.Sub(profilingTimers["PopulateGridMapTable"]))
	}
	return nil
}

// PopulateCategoryTable - Populate the categories table
func (tm *TrafficMgr) PopulateCategoryTable() (err error) {
	if profiling {