77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
|
|
package postgres
|
||
|
|
|
||
|
|
import "context"
|
||
|
|
|
||
|
|
const (
|
||
|
|
tbmeasurement string = "public.measurement"
|
||
|
|
)
|
||
|
|
|
||
|
|
const ()
|
||
|
|
|
||
|
|
type addrSSU struct {
|
||
|
|
Station string `json:"station"`
|
||
|
|
Device string `json:"device"`
|
||
|
|
Channel string `json:"channel"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type dataSource struct {
|
||
|
|
Type int `json:"type"`
|
||
|
|
Addr any `json:"io_address"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type measurement struct {
|
||
|
|
ID int64 `gorm:"colunmn:id"`
|
||
|
|
Tag string `gorm:"column:tag"`
|
||
|
|
Size int `gorm:"column:size"`
|
||
|
|
DataSource *dataSource `gorm:"type:jsonb;column:data_source"`
|
||
|
|
// mapping TODO
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetMeasurements(ctx context.Context, batchSize int) ([]*measurement, error) {
|
||
|
|
var totalRecords []*measurement
|
||
|
|
|
||
|
|
id := 0
|
||
|
|
for {
|
||
|
|
var records []*measurement
|
||
|
|
result := client.WithContext(ctx).Table(tbmeasurement).Where("id > ?", id).
|
||
|
|
Order("id ASC").Limit(batchSize).Find(&records)
|
||
|
|
if result.Error != nil {
|
||
|
|
return totalRecords, result.Error
|
||
|
|
}
|
||
|
|
|
||
|
|
length := len(records)
|
||
|
|
if length <= 0 {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
id += length
|
||
|
|
|
||
|
|
totalRecords = append(totalRecords, records...)
|
||
|
|
}
|
||
|
|
|
||
|
|
return totalRecords, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetStationMeasurements(ctx context.Context, batchSize int, station string) ([]*measurement, error) {
|
||
|
|
var totalRecords []*measurement
|
||
|
|
|
||
|
|
id := 0
|
||
|
|
for {
|
||
|
|
var records []*measurement
|
||
|
|
result := client.WithContext(ctx).Table(tbmeasurement).Where("station = ?", station).
|
||
|
|
Where("id > ?", id).Order("id ASC").Limit(batchSize).Find(&records)
|
||
|
|
if result.Error != nil {
|
||
|
|
return totalRecords, result.Error
|
||
|
|
}
|
||
|
|
|
||
|
|
length := len(records)
|
||
|
|
if length <= 0 {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
id += length
|
||
|
|
|
||
|
|
totalRecords = append(totalRecords, records...)
|
||
|
|
}
|
||
|
|
|
||
|
|
return totalRecords, nil
|
||
|
|
}
|