-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for MySQL publisher. #348
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## mbmd and mysql | ||
|
||
Let mbmd write sensor data to mysql | ||
|
||
### Table | ||
|
||
Create this table in your database: | ||
|
||
``` | ||
create table readings ( | ||
device varchar(100), | ||
measurement varchar(60), | ||
value float, | ||
tstamp int, | ||
description varchar(100), | ||
unit varchar(10), | ||
primary key (device, measurement, tstamp), | ||
index idx_tstamp (tstamp) | ||
) | ||
``` | ||
|
||
### MySQL parameters | ||
|
||
Run mbmd with: | ||
|
||
``` | ||
mbmd run -a ... -d ... --mysql-host={your-db-host:3306} --mysql-user={your-mysql-user} --mysql-password={mysql-user-password} --mysql-database={mysql-database-name} | ||
``` | ||
|
||
for example: | ||
|
||
``` | ||
mbmd run -a192.168.1.123:1502 -dSOLAREDGE:1.0 -dSOLAREDGE:1.1 -r2s --mysql-database=solaredge --mysql-host=127.0.0.1:3306 --mysql-user=mbmd --mysql-password=secret | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package server | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
// MySQL publisher | ||
type MySQL struct { | ||
client *sql.DB | ||
} | ||
|
||
// NewMySQLClient creates new publisher for MySQL | ||
func NewMySQLClient( | ||
host string, | ||
user string, | ||
password string, | ||
database string, | ||
) *MySQL { | ||
connString := user + ":" + password + "@tcp(" + host + ")/" + database | ||
db, err := sql.Open("mysql", connString) | ||
if err != nil { | ||
panic(err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: Please no panic, return an error with proper information |
||
} | ||
|
||
err = db.Ping() | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return &MySQL{ | ||
client: db, | ||
} | ||
} | ||
|
||
// Run MySQL publisher | ||
func (m *MySQL) Run(in <-chan QuerySnip) { | ||
|
||
var items []string | ||
var vals []interface{} | ||
var sql string | ||
var mu sync.Mutex | ||
|
||
ticker := time.NewTicker(time.Second) | ||
|
||
for { | ||
select { | ||
case snip, ok := <-in: | ||
if !ok { | ||
return | ||
} | ||
|
||
description, unit := snip.Measurement.DescriptionAndUnit() | ||
|
||
mu.Lock() | ||
items = append(items, "(?, ?, ?, ?, ?, ?)") | ||
vals = append(vals, snip.Device, snip.Measurement.String(), snip.Value, snip.Timestamp.Unix(), description, unit) | ||
mu.Unlock() | ||
|
||
case <-ticker.C: | ||
if len(items) == 0 { | ||
fmt.Println("Nothing to do ...", time.Now().Unix()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: this will pollute the output with no valueable informatin. Remove it. |
||
continue | ||
} | ||
|
||
mu.Lock() | ||
|
||
sql = "INSERT INTO readings (device, measurement, value, tstamp, description, unit) " + | ||
" VALUES " + strings.Join(items, ",") | ||
|
||
stmt, err := m.client.Prepare(sql) | ||
if err != nil { | ||
fmt.Println("Error preparing statement:", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: Please use |
||
mu.Unlock() | ||
continue | ||
} | ||
if _, err := stmt.Exec(vals...); err != nil { | ||
fmt.Println("Error executing statement:", err) | ||
} | ||
|
||
fmt.Println("Added: ", time.Now().Unix(), len(items)) | ||
|
||
items = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: |
||
vals = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: |
||
mu.Unlock() | ||
|
||
stmt.Close() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO: What if I want to connect to pgsql or sqlite or mssql? At least provide an adapter/interface where other developers can integrate their desired DB. Also the CLI args should be then:
db.type=mysql
,db.host
,db.user
and so on