baselog.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package db
  15. import (
  16. "context"
  17. "fmt"
  18. "strconv"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/sqlchemy"
  24. "yunion.io/x/sqlchemy/backends/clickhouse"
  25. "yunion.io/x/onecloud/pkg/apis"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. )
  30. type SLogBaseManager struct {
  31. SModelBaseManager
  32. }
  33. type SLogBase struct {
  34. SModelBase
  35. Id int64 `primary:"true" auto_increment:"true" list:"user" clickhouse_partition_by:"toInt64(id/100000000000)"`
  36. }
  37. func NewLogBaseManager(model interface{}, table string, keyword, keywordPlural string, timeCol string, useClickHouse bool) SLogBaseManager {
  38. if useClickHouse {
  39. man := SLogBaseManager{NewModelBaseManagerWithDBName(
  40. model,
  41. table,
  42. keyword,
  43. keywordPlural,
  44. ClickhouseDB,
  45. )}
  46. col := man.TableSpec().ColumnSpec("timeCol")
  47. if clickCol, ok := col.(clickhouse.IClickhouseColumnSpec); ok {
  48. clickCol.SetTTL(consts.SplitableMaxKeepMonths(), "MONTH")
  49. }
  50. return man
  51. } else {
  52. return SLogBaseManager{NewModelBaseManagerWithSplitable(
  53. model,
  54. table,
  55. keyword,
  56. keywordPlural,
  57. "id",
  58. timeCol,
  59. consts.SplitableMaxDuration(),
  60. consts.SplitableMaxKeepMonths(),
  61. )}
  62. }
  63. }
  64. func (manager *SLogBaseManager) CreateByInsertOrUpdate() bool {
  65. return false
  66. }
  67. func CurrentTimestamp(t time.Time) int64 {
  68. ret := int64(0)
  69. const (
  70. yOffset = 10000000000000
  71. mOffset = 100000000000
  72. dOffset = 1000000000
  73. hOffset = 10000000
  74. iOffset = 100000
  75. sOffset = 1000
  76. )
  77. ret += int64(t.Year()) * yOffset
  78. ret += int64(t.Month()) * mOffset
  79. ret += int64(t.Day()) * dOffset
  80. ret += int64(t.Hour()) * hOffset
  81. ret += int64(t.Minute()) * iOffset
  82. ret += int64(t.Second()) * sOffset
  83. ret += int64(t.Nanosecond()) / 1000000
  84. return ret
  85. }
  86. func (opslog *SLogBase) BeforeInsert() {
  87. t := time.Now().UTC()
  88. opslog.Id = CurrentTimestamp(t)
  89. }
  90. func (opslog *SLogBase) GetId() string {
  91. return fmt.Sprintf("%d", opslog.Id)
  92. }
  93. func (self *SLogBase) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
  94. return httperrors.NewForbiddenError("not allow to delete log")
  95. }
  96. func (self *SLogBaseManager) FilterById(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
  97. id, _ := strconv.Atoi(idStr)
  98. return q.Equals("id", id)
  99. }
  100. func (self *SLogBaseManager) FilterByNotId(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
  101. id, _ := strconv.Atoi(idStr)
  102. return q.NotEquals("id", id)
  103. }
  104. func (self *SLogBaseManager) FilterByName(q *sqlchemy.SQuery, name string) *sqlchemy.SQuery {
  105. return q
  106. }
  107. func (manager *SLogBaseManager) GetPagingConfig() *SPagingConfig {
  108. return &SPagingConfig{
  109. Order: sqlchemy.SQL_ORDER_DESC,
  110. MarkerFields: []string{"id"},
  111. DefaultLimit: 20,
  112. }
  113. }
  114. func (lb *SLogBase) GetRecordTime() time.Time {
  115. log.Fatalf("not implemented yet!")
  116. return time.Time{}
  117. }
  118. func (manager *SLogBaseManager) FetchById(idStr string) (IModel, error) {
  119. return FetchById(manager.GetIModelManager(), idStr)
  120. }
  121. func (l *SLogBase) ValidateUpdateData(
  122. ctx context.Context,
  123. userCred mcclient.TokenCredential,
  124. query jsonutils.JSONObject,
  125. data jsonutils.JSONObject,
  126. ) (jsonutils.JSONObject, error) {
  127. return nil, errors.Wrap(httperrors.ErrForbidden, "not allow")
  128. }
  129. // 操作日志列表
  130. func (manager *SLogBaseManager) ListItemFilter(
  131. ctx context.Context,
  132. q *sqlchemy.SQuery,
  133. userCred mcclient.TokenCredential,
  134. input apis.LogBaseListInput,
  135. ) (*sqlchemy.SQuery, error) {
  136. var err error
  137. q, err = manager.SModelBaseManager.ListItemFilter(ctx, q, userCred, input.ModelBaseListInput)
  138. if err != nil {
  139. return nil, errors.Wrap(err, "SModelBaseManager.ListItemFilter")
  140. }
  141. return q, nil
  142. }