Skip to content

Commit

Permalink
Technical debt (#248)
Browse files Browse the repository at this point in the history
* technical debt
  • Loading branch information
e154 authored Nov 28, 2023
1 parent e49e4ef commit a087f5b
Show file tree
Hide file tree
Showing 131 changed files with 1,314 additions and 475 deletions.
16 changes: 8 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ run:
- tmp/stream
linters:
enable:
- deadcode
- gofmt
- goimports
- varcheck
- errcheck
- gosimple
- ineffassign
# - staticcheck
# - structcheck
# - unused
# - govet
# - deadcode
# - varcheck
# - errcheck
# - staticcheck
# - structcheck
# - unused
# - govet
disable-all: true
go: '1.18'
go: '1.20'
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test:
go test -race $(go list ./... | grep -v /tests/)

install_linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.45.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.55.2

lint-todo:
@echo MARK: make lint todo
Expand Down
2 changes: 1 addition & 1 deletion Makefile.local
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test:
go test -race $(go list ./... | grep -v /tests/)

install_linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.45.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.55.2

lint-todo:
@echo MARK: make lint todo
Expand Down
5 changes: 3 additions & 2 deletions adaptors/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ package adaptors

import (
"context"
"github.com/e154/smart-home/system/orm"
"time"

"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
"github.com/e154/smart-home/system/orm"
)

// IAction ...
Expand Down
4 changes: 2 additions & 2 deletions adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/e154/smart-home/system/orm"
"strings"

"github.com/e154/smart-home/common/apperr"
"github.com/pkg/errors"
"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/common/apperr"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/system/orm"
)

// IEntity ...
Expand Down
4 changes: 3 additions & 1 deletion adaptors/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package adaptors

import (
"context"

"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
)

// IImage ...
Expand Down
9 changes: 5 additions & 4 deletions adaptors/run_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ func GetRunHistoryAdaptor(d *gorm.DB) IRunHistory {
func (n *RunHistory) Add(ctx context.Context, story *m.RunStory) (id int64, err error) {

var dbVer *db.RunStory
dbVer, err = n.toDb(story)
if id, err = n.table.Add(ctx, dbVer); err != nil {
if dbVer, err = n.toDb(story); err != nil {
return
}

id, err = n.table.Add(ctx, dbVer)
return
}

// Update ...
func (n *RunHistory) Update(ctx context.Context, story *m.RunStory) (err error) {

var dbVer *db.RunStory
dbVer, err = n.toDb(story)
if dbVer, err = n.toDb(story); err != nil {
return
}
err = n.table.Update(ctx, dbVer)
return
}
Expand Down
6 changes: 4 additions & 2 deletions adaptors/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package adaptors

import (
"context"
"sort"

"gorm.io/gorm"

"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
"sort"
)

// IScript ...
Expand Down
5 changes: 3 additions & 2 deletions adaptors/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ package adaptors
import (
"context"
"fmt"
"github.com/e154/smart-home/system/orm"

"gorm.io/gorm"

"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
"github.com/e154/smart-home/system/orm"
)

// ITask ...
Expand Down
6 changes: 3 additions & 3 deletions adaptors/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ package adaptors
import (
"context"
"encoding/json"
"github.com/e154/smart-home/system/orm"
"time"

"github.com/e154/smart-home/common"
"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
"github.com/e154/smart-home/system/orm"
)

// ITrigger ...
Expand Down
9 changes: 5 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ func (a *Api) Start() (err error) {

a.echo.HideBanner = true
a.echo.HidePort = true
a.echo.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: -1,
}))
a.echo.Use(middleware.Decompress())

if a.cfg.Gzip {
a.echo.Use(middleware.GzipWithConfig(middleware.DefaultGzipConfig))
a.echo.Use(middleware.Decompress())
}

a.registerHandlers()

Expand Down
1 change: 1 addition & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
Swagger bool
Debug bool
Pprof bool
Gzip bool
}

// String ...
Expand Down
4 changes: 1 addition & 3 deletions api/dto/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func (r Role) GetStubRole(from *m.Role) (to stub.ApiRole) {
for levelName, levels := range from.AccessList {
if len(levels) > 0 {
var items []string
for _, item := range levels {
items = append(items, item)
}
items = append(items, levels...)
if _, ok := to.AccessList.Levels[levelName]; !ok {
to.AccessList.Levels[levelName] = stub.AccessListListOfString{
Items: items,
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/commands/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
logger *logging.Logging,
backup *backup.Backup) {

if err := backup.New(); err != nil {
if err := backup.New(false); err != nil {
log.Error(err.Error())
}
}))
Expand Down
1 change: 1 addition & 0 deletions cmd/server/container/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func NewApiConfig(cfg *models.AppConfig) api.Config {
Swagger: cfg.ApiSwagger,
Pprof: cfg.Pprof,
Debug: cfg.ApiDebug,
Gzip: cfg.ApiGzip,
}
}
1 change: 1 addition & 0 deletions cmd/server/container/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ func MigrationList(adaptors *adaptors.Adaptors,
local_migrations.NewMigrationJavascriptV2(adaptors),
local_migrations.NewMigrationTimezone(adaptors),
local_migrations.NewMigrationSpeedtest(adaptors),
local_migrations.NewMigrationBackup(adaptors),
}
}
13 changes: 13 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ func Dir() string {
dir = strings.Replace(dir, "+/", "", -1)
return dir
}

func IsRunningInDockerContainer() bool {
// docker creates a .dockerenv file at the root
// of the directory tree inside the container.
// if this file exists then the viewer is running
// from inside a container so return true

if _, err := os.Stat("/.dockerenv"); err == nil {
return true
}

return false
}
20 changes: 19 additions & 1 deletion common/events/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

package events

import "github.com/e154/smart-home/common"

type EventCreatedBackup struct {
Name string `json:"name"`
Name string `json:"name"`
Scheduler bool `json:"scheduler"`
}

type EventRemovedBackup struct {
Expand All @@ -33,3 +36,18 @@ type EventUploadedBackup struct {
type EventStartedRestore struct {
Name string `json:"name"`
}

type CommandCreateBackup struct {
Scheduler bool `json:"scheduler"`
}

type CommandClearStorage struct {
Num int64 `json:"num"`
}

type CommandSendFileToTelegram struct {
Filename string `json:"filename"`
EntityId common.EntityId `json:"entity_id"`
Chunks bool `json:"chunks"`
ChunkSize int `json:"chunk_size"`
}
1 change: 1 addition & 0 deletions common/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package location
import (
"encoding/json"
"fmt"

"github.com/golang/geo/s1"
"github.com/golang/geo/s2"

Expand Down
3 changes: 2 additions & 1 deletion common/location/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package location

import (
m "github.com/e154/smart-home/models"
"math"
"testing"

"github.com/stretchr/testify/require"

m "github.com/e154/smart-home/models"
)

func TestGetDistanceBetweenPoints(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion common/web/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (d *DigestHeaders) Auth(username string, password string, uri string) (*Dig
d.Username = username
d.Password = password

req, err = http.NewRequest("GET", uri, nil)
req, _ = http.NewRequest("GET", uri, nil)
d.ApplyAuth(req)
resp, err = client.Do(req)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions conf/config.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"api_http_port": 3011,
"api_swagger": true,
"api_debug": false,
"api_gzip": false,
"pprof": false,
"domain": "localhost",
"https": false
Expand Down
4 changes: 2 additions & 2 deletions db/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ func (n Entities) DeleteScripts(ctx context.Context, id common.EntityId) (err er
}

// PreloadStorage ...
//todo: fix
// temporary solution because Preload("Storage", func(db *gorm.DB) *gorm.DB { - does not work ...
func (n Entities) PreloadStorage(ctx context.Context, list []*Entity) (err error) {

//todo: fix
// temporary solution because Preload("Storage", func(db *gorm.DB) *gorm.DB { - does not work ...
for _, item := range list {
err = n.Db.WithContext(ctx).Model(&EntityStorage{}).
Order("created_at desc").
Expand Down
2 changes: 1 addition & 1 deletion db/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (n *EntityStorages) List(ctx context.Context, limit, offset int, orderBy, s

q := n.Db.WithContext(ctx).Model(&EntityStorage{})

if entityIds != nil && len(entityIds) > 0 {
if len(entityIds) > 0 {
var ids = make([]string, 0, len(entityIds))
for _, id := range entityIds {
ids = append(ids, id.String())
Expand Down
8 changes: 6 additions & 2 deletions db/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/common/apperr"
"github.com/pkg/errors"
)

// Variables ...
Expand Down Expand Up @@ -60,7 +60,11 @@ func (n Variables) Add(ctx context.Context, variable Variable) (err error) {

// CreateOrUpdate ...
func (n *Variables) CreateOrUpdate(ctx context.Context, v Variable) (err error) {
if n.Db.WithContext(ctx).Model(&v).Where("name = ?", v.Name).Updates(&v).RowsAffected == 0 {
params := map[string]interface{}{
"name": v.Name,
"value": v.Value,
}
if n.Db.WithContext(ctx).Model(&v).Where("name = ?", v.Name).Updates(params).RowsAffected == 0 {
err = n.Db.WithContext(ctx).Create(&v).Error
}
return
Expand Down
10 changes: 9 additions & 1 deletion doc/content/en/docs/plugins/notify/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ msg.entity_id = 'telegram.name';
msg.attributes = {
'body': 'some text msg',
'chat_id': 123456,
'keys': ['foo', 'bar']
'keys': ['foo', 'bar'],
'photo_uri': ['foo', 'bar'],
'photo_path': ['foo', 'bar'],
'file_path': ['foo', 'bar'],
'file_uri': ['foo', 'bar']
};

```
Expand All @@ -57,6 +61,10 @@ attributes:
| body | Type: string, message body |
| chat_id | Type: int64, user's id |
| keys | Type: []string, keyboard |
| photo_uri | Type: []string, image |
| photo_path | Type: []string, image |
| file_path | Type: []string, file |
| file_uri | Type: []string, file |

----------------

Expand Down
Loading

0 comments on commit a087f5b

Please sign in to comment.