Skip to content

Commit

Permalink
✨ Add daemon task log table (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Jul 26, 2023
1 parent 28b51a6 commit cb802cc
Show file tree
Hide file tree
Showing 32 changed files with 1,821 additions and 19 deletions.
14 changes: 14 additions & 0 deletions pkg/configs/configuration.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package configs

import "github.com/go-sigma/sigma/pkg/types/enums"
Expand Down
4 changes: 2 additions & 2 deletions pkg/consts/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package consts
const (
// TopicSbom is the topic for the sbom
TopicSbom = "sbom"
// TopicVulnerability is the topic for the scan
TopicVulnerability = "scan"
// TopicVulnerability is the topic for the vuln
TopicVulnerability = "vuln"
// TopicGc is the topic for the gc
TopicGc = "gc"
)
1 change: 1 addition & 0 deletions pkg/dal/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func main() {
models.Blob{},
models.BlobUpload{},
models.CasbinRule{},
models.DaemonLog{},
)

g.Execute()
Expand Down
104 changes: 104 additions & 0 deletions pkg/dal/dao/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dao

import (
"context"

"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/types"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)

//go:generate mockgen -destination=mocks/daemon.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao DaemonService
//go:generate mockgen -destination=mocks/daemon_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao DaemonServiceFactory

// DaemonService is the interface that provides methods to operate on daemon model
type DaemonService interface {
// Create creates a new daemon record in the database
Create(ctx context.Context, audit *models.DaemonLog) error
// Delete delete a daemon log record with specific id
Delete(ctx context.Context, id int64) error
// List lists all daemon log
List(ctx context.Context, pagination types.Pagination, sort types.Sortable) ([]*models.DaemonLog, int64, error)
}

type daemonService struct {
tx *query.Query
}

// DaemonServiceFactory is the interface that provides the daemon service factory methods.
type DaemonServiceFactory interface {
New(txs ...*query.Query) DaemonService
}

type daemonServiceFactory struct{}

// NewDaemonServiceFactory creates a new audit service factory.
func NewDaemonServiceFactory() DaemonServiceFactory {
return &daemonServiceFactory{}
}

func (f *daemonServiceFactory) New(txs ...*query.Query) DaemonService {
tx := query.Q
if len(txs) > 0 {
tx = txs[0]
}
return &daemonService{
tx: tx,
}
}

// Create creates a new daemon record in the database
func (s *daemonService) Create(ctx context.Context, daemonLog *models.DaemonLog) error {
return s.tx.DaemonLog.WithContext(ctx).Create(daemonLog)
}

// Delete delete a daemon log record with specific id
func (s *daemonService) Delete(ctx context.Context, id int64) error {
matched, err := s.tx.DaemonLog.WithContext(ctx).Where(s.tx.DaemonLog.ID.Eq(id)).Delete()
if err != nil {
return err
}
if matched.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

// List lists all daemon log
func (s *daemonService) List(ctx context.Context, pagination types.Pagination, sort types.Sortable) ([]*models.DaemonLog, int64, error) {
pagination = utils.NormalizePagination(pagination)
query := s.tx.DaemonLog.WithContext(ctx)
field, ok := s.tx.DaemonLog.GetFieldByName(ptr.To(sort.Sort))
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
query = query.Order(field.Desc())
case enums.SortMethodAsc:
query = query.Order(field)
default:
query = query.Order(s.tx.DaemonLog.UpdatedAt.Desc())
}
} else {
query = query.Order(s.tx.DaemonLog.UpdatedAt.Desc())
}
return query.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}
81 changes: 81 additions & 0 deletions pkg/dal/dao/mocks/daemon.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions pkg/dal/dao/mocks/daemon_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/dal/dao/mocks/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pkg/dal/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ package dao

import (
"context"
"fmt"

"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/types"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)

//go:generate mockgen -destination=mocks/user.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao UserService
Expand All @@ -33,6 +37,8 @@ type UserService interface {
GetByUsername(ctx context.Context, username string) (*models.User, error)
// Create creates a new user.
Create(ctx context.Context, user *models.User) error
// List all users with pagination
List(ctx context.Context, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.User, int64, error)
// UpdateByID updates the namespace with the specified namespace ID.
UpdateByID(ctx context.Context, id int64, updates map[string]interface{}) error
// Count gets the total number of users.
Expand Down Expand Up @@ -88,6 +94,29 @@ func (s *userService) Create(ctx context.Context, user *models.User) error {
return s.tx.User.WithContext(ctx).Create(user)
}

// List all users with pagination
func (s *userService) List(ctx context.Context, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.User, int64, error) {
pagination = utils.NormalizePagination(pagination)
query := s.tx.User.WithContext(ctx)
if name != nil {
query = query.Where(s.tx.User.Username.Like(fmt.Sprintf("%%%s%%", ptr.To(name))))
}
field, ok := s.tx.User.GetFieldByName(ptr.To(sort.Sort))
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
query = query.Order(field.Desc())
case enums.SortMethodAsc:
query = query.Order(field)
default:
query = query.Order(s.tx.User.UpdatedAt.Desc())
}
} else {
query = query.Order(s.tx.User.UpdatedAt.Desc())
}
return query.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

// Count gets the total number of users.
func (s *userService) Count(ctx context.Context) (int64, error) {
return s.tx.User.WithContext(ctx).Count()
Expand Down
13 changes: 13 additions & 0 deletions pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ CREATE TABLE IF NOT EXISTS `artifact_blobs` (
CONSTRAINT `fk_artifact_blobs_blob` FOREIGN KEY (`blob_id`) REFERENCES `blobs` (`id`)
);

CREATE TABLE IF NOT EXISTS `daemon_logs` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`namespace_id` bigint,
`type` ENUM ('Gc', 'Vulnerability', 'Sbom') NOT NULL,
`action` ENUM ('create', 'update', 'delete', 'pull', 'push') NOT NULL,
`resource` varchar(256) NOT NULL,
`status` ENUM ('Success', 'Failed') NOT NULL,
`message` BLOB,
`created_at` timestamp NOT NULL,
`updated_at` timestamp NOT NULL,
`deleted_at` bigint NOT NULL DEFAULT 0
);

CREATE TABLE `casbin_rules` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`ptype` varchar(100),
Expand Down
Loading

0 comments on commit cb802cc

Please sign in to comment.