GithubHelp home page GithubHelp logo

jinagamvasubabu / ltreevisualizer Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 1.0 300 KB

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

License: Apache License 2.0

Go 95.90% Makefile 4.10%
hacktoberfest golang postgresql ltree-extension ltree display visualization

ltreevisualizer's Introduction

Ltree Visualizer

Build Status Go Report Card GoDoc License Visits Badge

A golang library to visualize or display postgres ltree type data directly from Postgres DB 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

Config:

//Visualizer config
type Visualizer struct {
    LogLevel    log.Level
    RankDir     string
    PostgresURI string //Example postgresql://postgres:postgres@localhost:5432/taxonomy?sslmode=disable
    Query       string //select id, name, path from table1 //columns specified in this example should match or use resultset alias if your column names are different
    FetchFromDB bool
}

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

You can generate image of your ltree data using two ways:

  • Directly fetch the data from your Postgres DB
  • using Interim JSON file

DB Way (Directly fetch the data from your Postgres DB):

//Visualizer config
type Visualizer struct {
    LogLevel    log.Level
    RankDir     string
    PostgresURI string //Example postgresql://postgres:postgres@localhost:5432/taxonomy?sslmode=disable
    Query       string //select id, name, path from table1 //columns specified in this example should match or use resultset alias if your column names are different
    FetchFromDB bool
}
  1. Provide PostgresURI which your app can connect Eg: postgresql://postgres:postgres@localhost:5432/taxonomy?sslmode=disable
  2. Provide Query to fetch the data of your Ltree and your query result set should contain id, name, path
  3. Set FetchFromDB to true
  import "github.com/jinagamvasubabu/ltreevisualizer"
  import "github.com/sirupsen/logrus"
 
  l := ltreevisualizer.Visualizer{
        PostgresURI: "postgresql://postgres:postgres@localhost:5432/taxonomy?sslmode=disable",
        Query: "select id as id, name as name,path as path from table"
  }
  resp, err := l.ConvertLtreeDataToImage(context.Background(), ltreevisualizer.VisualizerSchema{})
  fmt.Println(resp)

Using Interim Json File:

If you don't want to connect to DB and fetch the results then you can follow this way by using interim JSON file

//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

  • 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

How to test?

Refer visualizer_test.go for sample tests

ltreevisualizer's People

Contributors

fossabot avatar jinagamvasubabu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fossabot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.