ltreevisualizer

package module
Version: v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

Ltree Visualizer

Build Status Go Report Card GoDoc License Visits Badge

A golang library to visualize postgres ltree type data using DOT language and Graphviz

alt text

What is Ltree?

Ltree is a data type which is used to represent the hierarchical tree-like structures in flat rows and columns in postgres DB For more info-refer this https://www.postgresql.org/docs/9.1/ltree.html

Sample Hierarchy: alt text

why do we need this library ?

Ltree Labels are separated using Dot . like 1.2.3.4 and it is not easy to visualize like a tree.

This Library can produce output into two different formats:

  • DOT Graph
  • Image

DOT Graph:

DOT is a graph description language, using this language we can represent Directed, Undirected and FlowCharts. https://en.wikipedia.org/wiki/DOT_(graph_description_language)

digraph graphname {
    a -> b -> c;
    b -> d;
}

alt text

How to use?

  • get LtreeVisualizer
  go get github.com/jinagamvasubabu/ltreevisualizer
  • import and use it like below for to generate the output in DOT graph string:
  import "github.com/jinagamvasubabu/ltreevisualizer"
  import "github.com/sirupsen/logrus"
 
  l := ltreevisualizer.Visualizer{}
  resp, err := l.GenerateDotGraph(context.Background(), //json data)
  fmt.Println(resp)
  • import and use it like below for to generate the output as an image:
  import "github.com/jinagamvasubabu/ltreevisualizer"
  import "github.com/sirupsen/logrus"
 
  l := ltreevisualizer.Visualizer{}
  err := l.ConvertLtreeDataToImage(context.Background(), //json data)

Note: This will create a graph.png image if you don't specify Filepath

You can refer examples directory for more info

Config:

//Visualizer config
type Visualizer struct {
	LogLevel log.Level
	RankDir  string
	FilePath string
}

RankDir: Sets the direction of tree layout(https://www.graphviz.org/docs/attrs/rankdir/) and supported values are

  • TB (Top to Bottom)
  • RL (Right to Left)
  • LR (Left to Right)
  • BT (Bottom to Top) Note: Default is TB

FilePath: FilePath to save the image, this parameter is optional for GenerateDotGraph. Note: Default value of FilePath is graph.png

Input:

Ltree Visualizer accepts data in this format

//VisualizerSchema Contract to send to ltreevisualizer
type VisualizerSchema struct {
	Data []data `json:"data"`
}

type data struct {
	ID   int32  `json:"id"`
	Name string `json:"name"`
	Path string `json:"path"`
	Type string `json:"type"`
}

Refer data.json file under examples directory for sample data

How to test?

Refer visualizer_test.go for sample tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateTimeTaken

func CalculateTimeTaken(ctx context.Context, start time.Time, name string)

CalculateTimeTaken calculates the time taken to complete the execution of a method

func Contains

func Contains(list []string, searchTerm string) bool

Contains Check if a search term is available in a slice, returns bool

func GetSupportedRankDir

func GetSupportedRankDir() []string

GetSupportedRankDir Supported RankDirs

Types

type IVisualizer

type IVisualizer interface {
	GenerateDotGraph(ctx context.Context, ltreeData VisualizerSchema) (string, error)
	ConvertLtreeDataToImage(ctx context.Context, ltreeData VisualizerSchema) error
}

IVisualizer interface for to interact ltree visualizer

type Visualizer

type Visualizer struct {
	LogLevel log.Level
	RankDir  string
}

Visualizer config

func (*Visualizer) ConvertLtreeDataToImage

func (v *Visualizer) ConvertLtreeDataToImage(ctx context.Context, ltreeData VisualizerSchema) error

ConvertLtreeDataToImage Converts Ltree Data to an image

func (*Visualizer) GenerateDotGraph

func (v *Visualizer) GenerateDotGraph(ctx context.Context, ltreeData VisualizerSchema) (string, error)

GenerateDotGraph generates a DOT graph string

type VisualizerSchema

type VisualizerSchema struct {
	Data []data `json:"data"`
}

VisualizerSchema Contract to send to ltreevisualizer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL