<commit_before>package neo4j

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	// "koding/tools/config"
	"log"
	"net/http"
	"strings"
)

var (
	// todo update this constants, here must be only config file related strings after config files updated  
	// BASE_URL         = config.Current.Neo4j.Url + config.Current.Neo4j.Port  // "http://localhost:7474"
	BASE_URL         = "http://localhost:7474"
	NODE_PATH        = "/db/data/index/node/koding/"
	UNIQUE_NODE_PATH = "/db/data/index/node/koding?unique"
	INDEX_PATH       = "/db/data/index/node"
)

// Gets URL and string data to be sent and makes POST request
// reads response body and returns as string
func sendRequest(requestType, url, data string) string {

	//convert string into bytestream
	dataByte := strings.NewReader(data)
	req, err := http.NewRequest(requestType, url, dataByte)

	// read response body
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	body, _ := ioutil.ReadAll(res.Body)

	defer res.Body.Close()

	return string(body)

}

// connect source and target with relation property
func CreateRelationship(relation, source, target string) map[string]interface{} {

	relationshipData := fmt.Sprintf(`{"to" : "%s", "type" : "%s" }`, target, relation)
	relRes := sendRequest("POST", fmt.Sprintf("%s", source), relationshipData)

	relNode, err := jsonDecode(relRes)
	if err != nil {
		fmt.Println("Problem with relation response", relRes)
	}

	return relNode
}

// creates a unique node with given id and node name
func CreateUniqueNode(id string, name string) map[string]interface{} {

	url := BASE_URL + UNIQUE_NODE_PATH

	postData := generatePostJsonData(id, name)

	response := sendRequest("POST", url, postData)

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique node with given id and node name
func DeleteNode(id string) map[string]interface{} {

	url := BASE_URL + NODE_PATH

	response := sendRequest("DELETE", url, "")

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique tree head node to hold all nodes
// it is called once during runtime while initializing
func CreateUniqueIndex(name string) {
	//create unique index
	url := BASE_URL + INDEX_PATH

	bd := sendRequest("POST", url, `{"name":"`+name+`"}`)

	fmt.Println("Created unique index for data", bd)
}

// This is a custom json string generator as http request body to neo4j
func generatePostJsonData(id, name string) string {
	return fmt.Sprintf(`{ "key" : "id", "value" : "%s", "properties" : { "id" : "%s", "name" : "%s" } }`, id, id, name)
}
<commit_msg>[go][koding][database][neo4j] moved node deletion method<commit_after>package neo4j

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	// "koding/tools/config"
	"log"
	"net/http"
	"strings"
)

var (
	// todo update this constants, here must be only config file related strings after config files updated  
	// BASE_URL         = config.Current.Neo4j.Url + config.Current.Neo4j.Port  // "http://localhost:7474"
	BASE_URL         = "http://localhost:7474"
	INDEX_NODE_PATH  = "/db/data/index/node/koding"
	UNIQUE_NODE_PATH = "/db/data/index/node/koding?unique"
	INDEX_PATH       = "/db/data/index/node"
	NODE_URL         = "/db/data/node"
)

// Gets URL and string data to be sent and makes POST request
// reads response body and returns as string
func sendRequest(requestType, url, data string) string {

	//convert string into bytestream
	dataByte := strings.NewReader(data)
	req, err := http.NewRequest(requestType, url, dataByte)

	// read response body
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	body, _ := ioutil.ReadAll(res.Body)

	defer res.Body.Close()

	return string(body)

}

// connect source and target with relation property
func CreateRelationship(relation, source, target string) map[string]interface{} {

	relationshipData := fmt.Sprintf(`{"to" : "%s", "type" : "%s" }`, target, relation)
	relRes := sendRequest("POST", fmt.Sprintf("%s", source), relationshipData)

	relNode, err := jsonDecode(relRes)
	if err != nil {
		fmt.Println("Problem with relation response", relRes)
	}

	return relNode
}

// creates a unique node with given id and node name
func CreateUniqueNode(id string, name string) map[string]interface{} {

	url := BASE_URL + UNIQUE_NODE_PATH

	postData := generatePostJsonData(id, name)

	response := sendRequest("POST", url, postData)

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

func DeleteNodeRelationships(nodeUrl string) {
	relationshipsURL := nodeUrl + "/relationships/all"

	response := sendRequest("GET", relationshipsURL, "")

	relationships, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	fmt.Println(relationships)

}

func GetNode(id string) map[string]interface{} {

	url := BASE_URL + INDEX_NODE_PATH + "/id/" + id

	response := sendRequest("GET", url, "")

	nodeData, err := jsonDecode(response)
	if err != nil {
		fmt.Println("Problem with response", response)
	}

	return nodeData
}

// creates a unique node with given id and node name
func DeleteNode(id string) bool {

	// url := BASE_URL + INDEX_NODE_PATH + "/id/" + id

	node := GetNode(id)

	nodeURL := node["self"]

	DeleteNodeRelationships(fmt.Sprintf("%s", nodeURL))

	// response := sendRequest("DELETE", url, "")

	// nodeData, err := jsonDecode(response)
	// if err != nil {
	// 	fmt.Println("Problem with response", response)
	// }

	return true
}

// creates a unique tree head node to hold all nodes
// it is called once during runtime while initializing
func CreateUniqueIndex(name string) {
	//create unique index
	url := BASE_URL + INDEX_PATH

	bd := sendRequest("POST", url, `{"name":"`+name+`"}`)

	fmt.Println("Created unique index for data", bd)
}

// This is a custom json string generator as http request body to neo4j
func generatePostJsonData(id, name string) string {
	return fmt.Sprintf(`{ "key" : "id", "value" : "%s", "properties" : { "id" : "%s", "name" : "%s" } }`, id, id, name)
}

{'fraction_non_alphanumeric': 0.14171875, 'fraction_numerical': 0.00390625, 'mean_word_length': 4.100398406374502, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7043767', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2074, 'n_words': 602}