timeseries

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 1, 2023 License: MIT Imports: 9 Imported by: 2

README

Database access PostgresSQL

This package provides different functionialities and structures to store timeseries into a sqlite or postgres database.

Install

sudo apt install postgresql

Create and configure tables

Replace usernames and passwords to your needs.

sudo -u postgres psql
CREATE USER "grafanawriteuser";  
ALTER USER "grafanawriteuser" WITH PASSWORD 'GrafanaWritePassw0rd';  
CREATE DATABASE plottydb;  
GRANT ALL PRIVILEGES ON DATABASE "plottydb" TO "grafanawriteuser";  
GRANT CONNECT ON DATABASE plottydb TO "grafanawriteuser";  
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "grafanawriteuser";  

CREATE USER "grafanaread"; 
ALTER USER "grafanaread" WITH PASSWORD 'GrafanaReadPassw0rd';  
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO "grafanaread";  
GRANT CONNECT ON DATABASE plottydb TO "grafanaread";  
GRANT USAGE ON SCHEMA public TO "grafanaread";  
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "grafanaread";  
GRANT SELECT ON ALL TABLES IN SCHEMA public TO "grafanaread";  

ALTER DATABASE plottydb SET statement_timeout = '60s';

Adapt config to be able to connect from different hosts
sudo vim /etc/postgresql/11/main/pg_hba.conf
## IPv4 local connections
#host    all             all             127.0.0.1/32            md5 #comment this  
host  all  all 0.0.0.0/0 md5 ## add this  

sudo vim /etc/postgresql/11/main/postgresql.conf
listen_addresses = '*'
restart service and check
sudo systemctl status postgresql.service
ps -f -u  postgres
sudo lsof -n -u postgres | grep LISTEN
sudo netstat -ltnp | grep postgres
backup db
pg_dump -h 127.0.0.1 --user myuser -d plottydb --format plain --file "2019_09_10-livingroom.dump" -t measurements
restore db
sudo su postgres 
postgres@raspberrypi:/home/pi/data$ psql  plottydb < 2019_10_11-livingroom.pgsql
Correct timeshift
UPDATE measurements SET timestamp = timestamp - INTERVAL '2 hour' WHERE timestamp > '2020-10-24 20:00:00.000';
Others useful commands
postgres-# DROP OWNED BY myuser;
postgres-# DROP USER "myuser";
sudo su postgres
psql -U postgres -d plottydb -c "Select count(timestamp) from measurements;"
psql -U postgres -d plottydb -h 127.0.0.1 -c "Select count(timestamp) from measurements;"
select * from measurements order by timestamp desc limit 20;
select * from measurements where timestamp > '2018-11-04 14:45:28.367' order by timestamp asc limit 10;

Timeseries

Needs postgres 11 or 12

TimescaleDB Docs install To Upgrade: - Dump database https://www.postgresql.org/docs/12/upgrading.html ```terminal pg_dumpall > outputfile

```
- Change port of old postgres to something different (e.g. 5433)(postgresql.conf)
- Install new postgres and change port to 5432
- Configure database as old one
- Deactivate old one [stackoverflow]("https://serverfault.com/questions/542385/how-to-disable-1-version-of-postgresql-server-without-uninstalling-it/542390")
- disable old service -> vim /etc/postgresql/10/main/start.conf
- restart: sudo service postgresql restart

Connect to database (sudo -u postgres psql)

CREATE EXTENSION IF NOT EXISTS timesc;
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

// create timeseries db

CREATE TABLE measurements (
 time       TIMESTAMP(3)        NOT NULL,
 tag        TEXT                NOT NULL,
 value      DOUBLE PRECISION    NULL,
 comment    TEXT                DEFAULT '',
 unique (time, tag)
);


SELECT * FROM create_hypertable('measurements','time');

timescaledb-parallel-copy --verbose --connection="host=localhost --db-name ts_database --table measurements --file old_forcex.csv --workers 4 --copy-options "CSV"

CREATE TABLE new_table (LIKE old_table INCLUDING DEFAULTS INCLUDING 
CONSTRAINTS INCLUDING INDEXES);

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBConfig

type DBConfig struct {
	Name        string `json:"Name"`
	IPOrPath    string `json:"IPOrPath"`
	UsePostgres bool   `json:"UsePostgres"`
	User        string `json:"User"`
	Password    string `json:"Password"`
	Port        int    `json:"Port"`
	TableName   string `json:"TableName"`
}

func GetDefaultDBConfig

func GetDefaultDBConfig() DBConfig

type DbHandler

type DbHandler struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

func New

func New(conf DBConfig) DbHandler

New creates a DbHandler

func NewDefault

func NewDefault() DbHandler

NewDefault creates DbHandler with default a configuration

func (*DbHandler) AddColumnToTable

func (dbh *DbHandler) AddColumnToTable(tableName string, columnName string) error

AddColumnToTable adds a column with type number into table (real default null))

func (*DbHandler) CloseDatabase

func (dbh *DbHandler) CloseDatabase() error

CloseDatabase closes database connection

func (*DbHandler) CreateTimeseriesTable

func (dbh *DbHandler) CreateTimeseriesTable() error

CreateTimeseriesTable creates a table for timeseries values. Consider adding timescaledb features for postgres.

func (*DbHandler) InsertIntoDatabase

func (dbh *DbHandler) InsertIntoDatabase(tableName string, is ImportStruct) error

InsertIntoDatabase stores values into database

func (*DbHandler) InsertRowToTable

func (dbh *DbHandler) InsertRowToTable(tableName string, is ImportRowStruct) error

InsertRowToTable inserts one row into database

func (*DbHandler) InsertRowsToTable

func (dbh *DbHandler) InsertRowsToTable(tableName string, importStructs []ImportRowStruct) ([]ImportRowStruct, error)

InsertRowsToTable imports importStructs into table and returns failed rows

func (*DbHandler) InsertTimeseries

func (dbh *DbHandler) InsertTimeseries(is TimeseriesImportStruct, onClonflictDoNothing bool) error

InsertTimeseries stores values into timeseries table

func (*DbHandler) OpenDatabase added in v1.0.3

func (dbh *DbHandler) OpenDatabase() error

OpenDatabase creates a sqlite or postgres db

func (*DbHandler) ReadAllTPH

func (dbh *DbHandler) ReadAllTPH() ImportStruct

func (*DbHandler) ReadTPH

func (dbh *DbHandler) ReadTPH() ImportStruct

func (*DbHandler) SetFetched

func (dbh *DbHandler) SetFetched(firstTimestamp string, lastTimestamp string)

type ImportRowStruct

type ImportRowStruct struct {
	Names     []string
	Timestamp string
	Values    []string
}

ImportRowStruct contains data for one row

type ImportStruct

type ImportStruct struct {
	Names      []string
	Timestamps []string
	Data       [][]string
}

ImportStruct contains all data which are needed to add to a database. One Timestamp can have multiple values

func CreateImportTable

func CreateImportTable(importRows []ImportRowStruct) ImportStruct

type TimeseriesImportStruct

type TimeseriesImportStruct struct {
	Tag        string
	Timestamps []string
	Values     []string
	Comments   []string
}

TimeseriesImportStruct is to insert into a timeseries table

Jump to

Keyboard shortcuts

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