json_utility.go 6.76 KB
package webutility

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"sync"
	"time"

	"git.to-net.rs/marko.tikvic/gologger"
)

var (
	mu        = &sync.Mutex{}
	metadata  = make(map[string]Payload)
	updateQue = make(map[string][]byte)

	metadataDB    *sql.DB
	activeProject string

	inited bool
)

var logger *gologger.Logger

func init() {
	var err error
	logger, err = gologger.New("metadata", gologger.MaxLogSize100KB)
	if err != nil {
		fmt.Printf("webutility: %s\n", err.Error())
	}
}

type LangMap map[string]map[string]string

type Field struct {
	Parameter string `json:"param"`
	Type      string `json:"type"`
	Visible   bool   `json:"visible"`
	Editable  bool   `json:"editable"`
}

type CorrelationField struct {
	Result   string   `json:"result"`
	Elements []string `json:"elements"`
	Type     string   `json:"type"`
}

type Translation struct {
	Language     string            `json:"language"`
	FieldsLabels map[string]string `json:"fieldsLabels"`
}

type Payload struct {
	Method       string             `json:"method"`
	Params       map[string]string  `json:"params"`
	Lang         []Translation      `json:"lang"`
	Fields       []Field            `json:"fields"`
	Correlations []CorrelationField `json:"correlationFields"`
	IdField      string             `json:"idField"`

	// Data holds JSON payload. It can't be used for itteration.
	Data interface{} `json:"data"`
}

// NewPayload returs a payload sceleton for entity described with etype.
func NewPayload(r *http.Request, etype string) Payload {
	pload := metadata[etype]
	pload.Method = r.Method + " " + r.RequestURI
	return pload
}

// DecodeJSON decodes JSON data from r to v.
// Returns an error if it fails.
func DecodeJSON(r io.Reader, v interface{}) error {
	return json.NewDecoder(r).Decode(v)
}

// LoadPayloadsdetaData loads all payloads' information into 'metadata' variable.
func LoadPayloadsMetadata(db *sql.DB, project string) error {
	metadataDB = db
	activeProject = project

	mu.Lock()
	defer mu.Unlock()
	err := initMetadata(project)
	if err != nil {
		return err
	}
	inited = true

	return nil
}

func EnableHotloading(interval int) {
	if interval > 0 {
		go hotload(interval)
	}
}

func GetMetadataForAllEntities() map[string]Payload {
	return metadata
}

func GetMetadataForEntity(t string) (Payload, bool) {
	p, ok := metadata[t]
	return p, ok
}

func QueEntityModelUpdate(entityType string, v interface{}) {
	updateQue[entityType], _ = json.Marshal(v)
}

func initMetadata(project string) error {
	rows, err := metadataDB.Query(`select
		entity_type,
		metadata
		from entities
		where projekat = ` + fmt.Sprintf("'%s'", project))
	if err != nil {
		return err
	}
	defer rows.Close()

	count := 0
	success := 0
	if len(metadata) > 0 {
		metadata = nil
	}
	metadata = make(map[string]Payload)
	for rows.Next() {
		var name, load string
		rows.Scan(&name, &load)

		p := Payload{}
		err := json.Unmarshal([]byte(load), &p)
		if err != nil {
			logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), load)
		} else {
			success++
			metadata[name] = p
		}
		count++
	}
	perc := float32(success/count) * 100.0
	logger.Log("loaded %d/%d (%.1f%%) entities\n", success, count, perc)

	return nil
}

func hotload(n int) {
	entityScan := make(map[string]int64)
	firstCheck := true
	for {
		time.Sleep(time.Duration(n) * time.Second)
		rows, err := metadataDB.Query(`select
			ora_rowscn,
			entity_type
			from entities where projekat = ` + fmt.Sprintf("'%s'", activeProject))
		if err != nil {
			logger.Log("hotload failed: %v\n", err)
			time.Sleep(time.Duration(n) * time.Second)
			continue
		}

		var toRefresh []string
		for rows.Next() {
			var scanID int64
			var entity string
			rows.Scan(&scanID, &entity)
			oldID, ok := entityScan[entity]
			if !ok || oldID != scanID {
				entityScan[entity] = scanID
				toRefresh = append(toRefresh, entity)
			}
		}
		rows.Close()

		if rows.Err() != nil {
			logger.Log("hotload rset error: %v\n", rows.Err())
			time.Sleep(time.Duration(n) * time.Second)
			continue
		}

		if len(toRefresh) > 0 && !firstCheck {
			mu.Lock()
			refreshMetadata(toRefresh)
			mu.Unlock()
		}
		if firstCheck {
			firstCheck = false
		}
	}
}

func refreshMetadata(entities []string) {
	for _, e := range entities {
		fmt.Printf("refreshing %s\n", e)
		rows, err := metadataDB.Query(`select
			metadata
			from entities
			where projekat = ` + fmt.Sprintf("'%s'", activeProject) +
			` and entity_type = ` + fmt.Sprintf("'%s'", e))

		if err != nil {
			logger.Log("webutility: refresh: prep: %v\n", err)
			rows.Close()
			continue
		}

		for rows.Next() {
			var load string
			rows.Scan(&load)
			p := Payload{}
			err := json.Unmarshal([]byte(load), &p)
			if err != nil {
				logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load)
			} else {
				metadata[e] = p
			}
		}
		rows.Close()
	}
}

/*
func UpdateEntityModels(command string) (total, upd, add int, err error) {
	if command != "force" && command != "missing" {
		return total, 0, 0, errors.New("uknown command: " + command)
	}

	if !inited {
		return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.")
	}

	total = len(updateQue)

	forUpdate := make([]string, 0)
	forAdd := make([]string, 0)

	for k, _ := range updateQue {
		if _, exists := metadata[k]; exists {
			if command == "force" {
				forUpdate = append(forUpdate, k)
			}
		} else {
			forAdd = append(forAdd, k)
		}
	}

	for _, k := range forUpdate {
		_, err := metadataDB.PrepAndExe(`update entities set
			entity_model = :1
			where entity_type = :2`,
			string(updateQue[k]),
			k)

		if err != nil {
			logger.Log("webutility: update metadata: prep and exe: %v\n", err)
			continue
		}
		upd++
		logger.Log("webutility: updated %s payload model\n", k)
	}

	blankPayload, _ := json.Marshal(Payload{})
	for _, k := range forAdd {
		_, err := metadataDB.PrepAndExe(`insert into entities
			(projekat, metadata, entity_type, entity_model)
			values(:1, :2, :3, :4)`,
			activeProject,
			string(blankPayload),
			k,
			string(updateQue[k]))

		if err != nil {
			logger.Log("webutility: add metadata: prep and exe: %v\n", err)
			continue
		}
		metadata[k] = Payload{}
		add++
		logger.Log("webutility: added %s to the payload models\n", k)

	}

	return total, upd, add, nil
}

func ModifyMetadataForEntity(entityType string, p *Payload) error {
	md, err := json.Marshal(*p)
	if err != nil {
		return err
	}

	mu.Lock()
	defer mu.Unlock()
	_, err = metadataDB.PrepAndExe(`update entities set
		metadata = :1
		where projekat = :2
		and entity_type = :3`,
		string(md),
		activeProject,
		entityType)
	if err != nil {
		return err
	}
	return nil
}

func DeleteEntityModel(entityType string) error {
	_, err := metadataDB.PrepAndExe("delete from entities where entity_type = :1", entityType)
	if err == nil {
		mu.Lock()
		delete(metadata, entityType)
		mu.Unlock()
	}
	return err
}
*/