<commit_before>package data

import (
	"net/http"
	"encoding/xml"
	"io"
	"strconv"
	"time"
	"database/sql"
	"os"
	_ "github.com/jbarham/gopgsqldriver"
)

var db_connection = "user=adminficeuc6 dbname=codejam2013 password=<PASSWORD> host="+os.Getenv("OPENSHIFT_POSTGRESQL_DB_HOST")+" port="+os.Getenv("OPENSHIFT_POSTGRESQL_DB_PORT")
const db_provider = "postgres"

const apikey = "<KEY>"

const day = "day"
const month = "month"

const quarter = (15*time.Minute)

func Monitor () (chan bool) {
	msg := make(chan bool, 5)
	go func () {
		db_init()
		//getPastUnit(month) //Initialize the db with the past month's data (Currently provides broken data...)
		for {
			getPastUnit(day)
			msg <- true //tell Predicate to update
			time.Sleep(quarter) //wait for another 15 mins
		}
	} ()
	return msg
}

func db_init() {
	var db, err = sql.Open(db_provider, db_connection)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	_, err = db.Exec("CREATE TABLE IF NOT EXISTS Records (ID SERIAL PRIMARY KEY UNIQUE,Time TIMESTAMP WITH TIME ZONE UNIQUE NOT NULL, Radiation DOUBLE precision, Humidity DOUBLE precision, Temperature DOUBLE precision, Wind DOUBLE precision, Power DOUBLE precision);")
	_, err = db.Exec("CREATE FUNCTION merge_db(key timestamp with time zone, rad DOUBLE precision, humid DOUBLE precision, temp DOUBLE precision, w DOUBLE precision, pow DOUBLE precision) RETURNS VOID AS $$ BEGIN LOOP UPDATE Records SET Radiation = rad, Humidity=humid, Temperature=temp, Wind=w, Power=pow WHERE Time = key; IF found THEN RETURN; END IF; BEGIN INSERT INTO Records(Time, Radiation, Humidity, Temperature, Wind, Power) VALUES (key, rad, humid, temp, w, pow); RETURN; EXCEPTION WHEN unique_violation THEN END; END LOOP; END; $$ LANGUAGE plpgsql;")
}

func getPast (id int, duration string) (resp *http.Response, err error) {
	client := new(http.Client)
	request, err:= http.NewRequest("GET", "https://api.pulseenergy.com/pulse/1/points/"+strconv.Itoa(id)+"/data.xml?interval="+duration, nil)
	if err != nil {
		return nil, err
	}
	request.Header.Add("Authorization", apikey)
	resp, err = client.Do(request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

type records struct {
	RecordList []record `xml:"record"`
}

type record struct {
	Date string `xml:"date,attr"`
	Value float64 `xml:"value,attr"`
}

type point struct {
	Records records `xml:"records"`
}

func parseXmlFloat64 (r io.Reader) []record {
	decoder := xml.NewDecoder(r)
	var output point
	err := decoder.Decode(&output)
	if err != nil {
		panic(err)
	}
	return output.Records.RecordList
}
	
func creativeUpdate(data []Record) {
	var db, err = sql.Open(db_provider, db_connection)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	var statement *sql.Stmt
	statement, err = db.Prepare("SELECT merge_db($1, $2, $3, $4, $5, $6)")
	if err != nil {
		panic(err)
	}
	defer statement.Close()
	for i := 0; i < len(data); i++ {
		_, err = statement.Exec(data[i].Time, data[i].Radiation, data[i].Humidity, data[i].Temperature, data[i].Wind, data[i].Power)
		if err != nil {
			panic(err)
		}
	}
}


func getPastUnit (unit string) {
	resp, err := getPast(66094, unit) // Radiation
	if err != nil {
		panic(err)
	}
	RadList :=  parseXmlFloat64(resp.Body)<commit_msg>... annoying nonsense error<commit_after>package data

import (
	"net/http"
	"encoding/xml"
	"io"
	"strconv"
	"time"
	"database/sql"
	"os"
	_ "github.com/jbarham/gopgsqldriver"
)

var db_connection = "user=adminficeuc6 dbname=codejam2013 password=<PASSWORD> host="+os.Getenv("OPENSHIFT_POSTGRESQL_DB_HOST")+" port="+os.Getenv("OPENSHIFT_POSTGRESQL_DB_PORT")
const db_provider = "postgres"

const apikey = "<KEY>"

const day = "day"
const month = "month"

const quarter = (15*time.Minute)

func Monitor () (chan bool) {
	msg := make(chan bool, 5)
	go func () {
		db_init()
		//getPastUnit(month) //Initialize the db with the past month's data (Currently provides broken data...)
		for {
			getPastUnit(day)
			msg <- true //tell Predicate to update
			time.Sleep(quarter) //wait for another 15 mins
		}
	} ()
	return msg
}

func db_init() {
	var db, err = sql.Open(db_provider, db_connection)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	_, err = db.Exec("CREATE TABLE IF NOT EXISTS Records (ID SERIAL PRIMARY KEY UNIQUE,Time TIMESTAMP WITH TIME ZONE UNIQUE NOT NULL, Radiation DOUBLE precision, Humidity DOUBLE precision, Temperature DOUBLE precision, Wind DOUBLE precision, Power DOUBLE precision);")
	_, err = db.Exec("CREATE FUNCTION merge_db(key timestamp with time zone, rad DOUBLE precision, humid DOUBLE precision, temp DOUBLE precision, w DOUBLE precision, pow DOUBLE precision) RETURNS VOID AS $$ BEGIN LOOP UPDATE Records SET Radiation = rad, Humidity=humid, Temperature=temp, Wind=w, Power=pow WHERE Time = key; IF found THEN RETURN; END IF; BEGIN INSERT INTO Records(Time, Radiation, Humidity, Temperature, Wind, Power) VALUES (key, rad, humid, temp, w, pow); RETURN; EXCEPTION WHEN unique_violation THEN END; END LOOP; END; $$ LANGUAGE plpgsql;")
}

func getPast (id int, duration string) (resp *http.Response, err error) {
	client := new(http.Client)
	request, err:= http.NewRequest("GET", "https://api.pulseenergy.com/pulse/1/points/"+strconv.Itoa(id)+"/data.xml?interval="+duration, nil)
	if err != nil {
		return nil, err
	}
	request.Header.Add("Authorization", apikey)
	resp, err = client.Do(request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

type records struct {
	RecordList []record `xml:"record"`
}

type record struct {
	Date string `xml:"date,attr"`
	Value float64 `xml:"value,attr"`
}

type point struct {
	Records records `xml:"records"`
}

func parseXmlFloat64 (r io.Reader) []record {
	decoder := xml.NewDecoder(r)
	var output point
	err := decoder.Decode(&output)
	if err != nil {
		panic(err)
	}
	return output.Records.RecordList
}
	
func creativeUpdate(data []Record) {
	var db, err = sql.Open(db_provider, db_connection)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	var statement *sql.Stmt
	statement, err = db.Prepare("SELECT merge_db($1, $2, $3, $4, $5, $6)")
	if err != nil {
		panic(err)
	}
	err = nil
	defer statement.Close()
	for i := 0; i < len(data); i++ {
		err = nil
		_, err = statement.Exec(data[i].Time, data[i].Radiation, data[i].Humidity, data[i].Temperature, data[i].Wind, data[i].Power)
		if err != nil {
			panic(err)
		}
	}
}


func getPastUnit (unit string) {
	resp, err := getPast(66094, unit) // Radiation
	if err != nil {
		panic(err)
	}
	RadList :=  parseXmlFloat64(resp.Body)
{'fraction_non_alphanumeric': 0.13854214825088612, 'fraction_numerical': 0.008938203112960395, 'mean_word_length': 4.453781512605042, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324001', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2278, 'n_words': 672}