2015-07-20 07:51:27 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-03-15 18:08:25 -05:00
|
|
|
"time"
|
2017-05-23 03:56:23 -05:00
|
|
|
|
2015-07-20 07:51:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-09-10 12:47:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-07-20 07:51:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-09-11 10:17:10 -05:00
|
|
|
bus.AddHandler("sql", GetOrgQuotaByTarget)
|
|
|
|
bus.AddHandler("sql", GetOrgQuotas)
|
|
|
|
bus.AddHandler("sql", UpdateOrgQuota)
|
|
|
|
bus.AddHandler("sql", GetUserQuotaByTarget)
|
|
|
|
bus.AddHandler("sql", GetUserQuotas)
|
|
|
|
bus.AddHandler("sql", UpdateUserQuota)
|
|
|
|
bus.AddHandler("sql", GetGlobalQuotaByTarget)
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type targetCount struct {
|
|
|
|
Count int64
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetOrgQuotaByTarget(query *models.GetOrgQuotaByTargetQuery) error {
|
|
|
|
quota := models.Quota{
|
2015-07-20 07:51:27 -05:00
|
|
|
Target: query.Target,
|
|
|
|
OrgId: query.OrgId,
|
|
|
|
}
|
2015-09-15 04:10:16 -05:00
|
|
|
has, err := x.Get("a)
|
2015-07-20 07:51:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2015-09-11 10:17:10 -05:00
|
|
|
quota.Limit = query.Default
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//get quota used.
|
2015-09-11 10:17:10 -05:00
|
|
|
rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where org_id=?", dialect.Quote(query.Target))
|
2015-07-20 07:51:27 -05:00
|
|
|
resp := make([]*targetCount, 0)
|
2018-09-16 05:26:05 -05:00
|
|
|
if err := x.SQL(rawSql, query.OrgId).Find(&resp); err != nil {
|
2015-07-20 07:51:27 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = &models.OrgQuotaDTO{
|
2015-07-20 07:51:27 -05:00
|
|
|
Target: query.Target,
|
|
|
|
Limit: quota.Limit,
|
|
|
|
OrgId: query.OrgId,
|
|
|
|
Used: resp[0].Count,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetOrgQuotas(query *models.GetOrgQuotasQuery) error {
|
|
|
|
quotas := make([]*models.Quota, 0)
|
2015-07-20 07:51:27 -05:00
|
|
|
sess := x.Table("quota")
|
2015-09-11 10:17:10 -05:00
|
|
|
if err := sess.Where("org_id=? AND user_id=0", query.OrgId).Find("as); err != nil {
|
2015-07-20 07:51:27 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:18:26 -05:00
|
|
|
defaultQuotas := setting.Quota.Org.ToMap()
|
2015-09-11 10:17:10 -05:00
|
|
|
|
|
|
|
seenTargets := make(map[string]bool)
|
2015-07-20 07:51:27 -05:00
|
|
|
for _, q := range quotas {
|
|
|
|
seenTargets[q.Target] = true
|
|
|
|
}
|
|
|
|
|
2015-09-11 10:17:10 -05:00
|
|
|
for t, v := range defaultQuotas {
|
|
|
|
if _, ok := seenTargets[t]; !ok {
|
2020-02-29 06:35:15 -06:00
|
|
|
quotas = append(quotas, &models.Quota{
|
2015-07-20 07:51:27 -05:00
|
|
|
OrgId: query.OrgId,
|
2015-09-11 10:17:10 -05:00
|
|
|
Target: t,
|
2015-07-20 07:51:27 -05:00
|
|
|
Limit: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 10:17:10 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
result := make([]*models.OrgQuotaDTO, len(quotas))
|
2015-07-20 07:51:27 -05:00
|
|
|
for i, q := range quotas {
|
|
|
|
//get quota used.
|
2015-09-11 10:17:10 -05:00
|
|
|
rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where org_id=?", dialect.Quote(q.Target))
|
2015-07-20 07:51:27 -05:00
|
|
|
resp := make([]*targetCount, 0)
|
2018-09-16 05:26:05 -05:00
|
|
|
if err := x.SQL(rawSql, q.OrgId).Find(&resp); err != nil {
|
2015-07-20 07:51:27 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-02-29 06:35:15 -06:00
|
|
|
result[i] = &models.OrgQuotaDTO{
|
2015-07-20 07:51:27 -05:00
|
|
|
Target: q.Target,
|
|
|
|
Limit: q.Limit,
|
|
|
|
OrgId: q.OrgId,
|
|
|
|
Used: resp[0].Count,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query.Result = result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func UpdateOrgQuota(cmd *models.UpdateOrgQuotaCmd) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-07-21 05:30:31 -05:00
|
|
|
//Check if quota is already defined in the DB
|
2020-02-29 06:35:15 -06:00
|
|
|
quota := models.Quota{
|
2018-12-05 07:19:40 -06:00
|
|
|
Target: cmd.Target,
|
|
|
|
OrgId: cmd.OrgId,
|
2015-07-21 05:30:31 -05:00
|
|
|
}
|
2015-09-15 04:10:16 -05:00
|
|
|
has, err := sess.Get("a)
|
2015-07-21 05:30:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 07:19:40 -06:00
|
|
|
quota.Updated = time.Now()
|
2015-07-21 05:30:31 -05:00
|
|
|
quota.Limit = cmd.Limit
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
if !has {
|
2018-03-15 18:08:25 -05:00
|
|
|
quota.Created = time.Now()
|
2015-07-21 05:30:31 -05:00
|
|
|
//No quota in the DB for this target, so create a new one.
|
|
|
|
if _, err := sess.Insert("a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//update existing quota entry in the DB.
|
2020-07-16 07:39:01 -05:00
|
|
|
_, err := sess.ID(quota.Id).Update("a)
|
|
|
|
if err != nil {
|
2015-07-21 05:30:31 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
2015-09-11 10:17:10 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetUserQuotaByTarget(query *models.GetUserQuotaByTargetQuery) error {
|
|
|
|
quota := models.Quota{
|
2015-09-11 10:17:10 -05:00
|
|
|
Target: query.Target,
|
|
|
|
UserId: query.UserId,
|
|
|
|
}
|
2015-09-15 04:10:16 -05:00
|
|
|
has, err := x.Get("a)
|
2015-09-11 10:17:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2015-09-11 10:17:10 -05:00
|
|
|
quota.Limit = query.Default
|
|
|
|
}
|
|
|
|
|
|
|
|
//get quota used.
|
|
|
|
rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where user_id=?", dialect.Quote(query.Target))
|
|
|
|
resp := make([]*targetCount, 0)
|
2018-09-16 05:26:05 -05:00
|
|
|
if err := x.SQL(rawSql, query.UserId).Find(&resp); err != nil {
|
2015-09-11 10:17:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = &models.UserQuotaDTO{
|
2015-09-11 10:17:10 -05:00
|
|
|
Target: query.Target,
|
|
|
|
Limit: quota.Limit,
|
|
|
|
UserId: query.UserId,
|
|
|
|
Used: resp[0].Count,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetUserQuotas(query *models.GetUserQuotasQuery) error {
|
|
|
|
quotas := make([]*models.Quota, 0)
|
2015-09-11 10:17:10 -05:00
|
|
|
sess := x.Table("quota")
|
|
|
|
if err := sess.Where("user_id=? AND org_id=0", query.UserId).Find("as); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:18:26 -05:00
|
|
|
defaultQuotas := setting.Quota.User.ToMap()
|
2015-09-11 10:17:10 -05:00
|
|
|
|
|
|
|
seenTargets := make(map[string]bool)
|
|
|
|
for _, q := range quotas {
|
|
|
|
seenTargets[q.Target] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for t, v := range defaultQuotas {
|
|
|
|
if _, ok := seenTargets[t]; !ok {
|
2020-02-29 06:35:15 -06:00
|
|
|
quotas = append(quotas, &models.Quota{
|
2015-09-11 10:17:10 -05:00
|
|
|
UserId: query.UserId,
|
|
|
|
Target: t,
|
|
|
|
Limit: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
result := make([]*models.UserQuotaDTO, len(quotas))
|
2015-09-11 10:17:10 -05:00
|
|
|
for i, q := range quotas {
|
|
|
|
//get quota used.
|
|
|
|
rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where user_id=?", dialect.Quote(q.Target))
|
|
|
|
resp := make([]*targetCount, 0)
|
2018-09-16 05:26:05 -05:00
|
|
|
if err := x.SQL(rawSql, q.UserId).Find(&resp); err != nil {
|
2015-09-11 10:17:10 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-02-29 06:35:15 -06:00
|
|
|
result[i] = &models.UserQuotaDTO{
|
2015-09-11 10:17:10 -05:00
|
|
|
Target: q.Target,
|
|
|
|
Limit: q.Limit,
|
|
|
|
UserId: q.UserId,
|
|
|
|
Used: resp[0].Count,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query.Result = result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func UpdateUserQuota(cmd *models.UpdateUserQuotaCmd) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-09-11 10:17:10 -05:00
|
|
|
//Check if quota is already defined in the DB
|
2020-02-29 06:35:15 -06:00
|
|
|
quota := models.Quota{
|
2018-12-05 07:29:07 -06:00
|
|
|
Target: cmd.Target,
|
|
|
|
UserId: cmd.UserId,
|
2015-09-11 10:17:10 -05:00
|
|
|
}
|
2015-09-15 04:10:16 -05:00
|
|
|
has, err := sess.Get("a)
|
2015-09-11 10:17:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-05 07:29:07 -06:00
|
|
|
quota.Updated = time.Now()
|
2015-09-11 10:17:10 -05:00
|
|
|
quota.Limit = cmd.Limit
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
if !has {
|
2018-03-15 18:08:25 -05:00
|
|
|
quota.Created = time.Now()
|
2015-09-11 10:17:10 -05:00
|
|
|
//No quota in the DB for this target, so create a new one.
|
|
|
|
if _, err := sess.Insert("a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//update existing quota entry in the DB.
|
2020-07-16 07:39:01 -05:00
|
|
|
_, err := sess.ID(quota.Id).Update("a)
|
|
|
|
if err != nil {
|
2015-09-11 10:17:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetGlobalQuotaByTarget(query *models.GetGlobalQuotaByTargetQuery) error {
|
2015-09-11 10:17:10 -05:00
|
|
|
//get quota used.
|
|
|
|
rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s", dialect.Quote(query.Target))
|
|
|
|
resp := make([]*targetCount, 0)
|
2018-09-16 05:26:05 -05:00
|
|
|
if err := x.SQL(rawSql).Find(&resp); err != nil {
|
2015-09-11 10:17:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = &models.GlobalQuotaDTO{
|
2015-09-11 10:17:10 -05:00
|
|
|
Target: query.Target,
|
|
|
|
Limit: query.Default,
|
|
|
|
Used: resp[0].Count,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|