| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package sqlstore
- import (
- "fmt"
- "github.com/grafana/grafana/pkg/bus"
- m "github.com/grafana/grafana/pkg/models"
- )
- func init() {
- bus.AddHandler("sql", GetQuotaByTarget)
- bus.AddHandler("sql", GetQuotas)
- bus.AddHandler("sql", UpdateQuota)
- }
- type targetCount struct {
- Count int64
- }
- func GetQuotaByTarget(query *m.GetQuotaByTargetQuery) error {
- quota := m.Quota{
- Target: query.Target,
- OrgId: query.OrgId,
- }
- has, err := x.Get(quota)
- if err != nil {
- return err
- } else if has == false {
- quota.Limit = m.DefaultQuotas[query.Target]
- }
- //get quota used.
- rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where org_id=?", dialect.Quote(string(query.Target)))
- resp := make([]*targetCount, 0)
- if err := x.Sql(rawSql, query.OrgId).Find(&resp); err != nil {
- return err
- }
- query.Result = &m.QuotaDTO{
- Target: query.Target,
- Limit: quota.Limit,
- OrgId: query.OrgId,
- Used: resp[0].Count,
- }
- return nil
- }
- func GetQuotas(query *m.GetQuotasQuery) error {
- quotas := make([]*m.Quota, 0)
- sess := x.Table("quota")
- if err := sess.Where("org_id=?", query.OrgId).Find("as); err != nil {
- return err
- }
- seenTargets := make(map[m.QuotaTarget]bool)
- for _, q := range quotas {
- seenTargets[q.Target] = true
- }
- for t, v := range m.DefaultQuotas {
- if _, ok := seenTargets[t]; !ok {
- quotas = append(quotas, &m.Quota{
- OrgId: query.OrgId,
- Target: t,
- Limit: v,
- })
- }
- }
- result := make([]*m.QuotaDTO, len(quotas))
- for i, q := range quotas {
- //get quota used.
- rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where org_id=?", dialect.Quote(string(q.Target)))
- resp := make([]*targetCount, 0)
- if err := x.Sql(rawSql, q.OrgId).Find(&resp); err != nil {
- return err
- }
- result[i] = &m.QuotaDTO{
- Target: q.Target,
- Limit: q.Limit,
- OrgId: q.OrgId,
- Used: resp[0].Count,
- }
- }
- query.Result = result
- return nil
- }
- func UpdateQuota(cmd *m.UpdateQuotaCmd) error {
- return inTransaction2(func(sess *session) error {
- //Check if quota is already defined in the DB
- quota := m.Quota{
- Target: cmd.Target,
- OrgId: cmd.OrgId,
- }
- has, err := sess.Get(quota)
- if err != nil {
- return err
- }
- quota.Limit = cmd.Limit
- if has == false {
- //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.
- if _, err := sess.Id(quota.Id).Update("a); err != nil {
- return err
- }
- }
- return nil
- })
- }
|