Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / quota.js @ b4329a33

History | View | Annotate | Download (5.2 kB)

1
// Copyright 2013 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
;(function(root){
36
    
37
    // Astakos quotas lib
38
    // Requires jquery and jquery.cookie javascript libs
39
    //
40
    // Usage
41
    // -----
42
    // <script src="jquery.js"></script>
43
    // <script src="backbone.js"></script>
44
    // <script src="snf/quota.js"></script>
45
    // 
46
    // var quotas = new snf.quota.Quota();
47
    // var quotas = new snf.quota.Quota();
48
    // $.ajax({
49
    //        url: '/userquota',
50
    //        async: false,
51
    //        method: 'POST',
52
    //        success: function(data) {
53
    //           quotas.load(data);
54
    //        }
55
    //    })
56
    //
57
    // var vms_limit = quotas.get_limit("cyclades.vm");
58
    // var networks_usage = quotas.get_usage("cyclades.network.private");
59
    //
60

    
61
    var root = root;
62
    var snf = root.synnefo = root.synnefo || {};
63
    var bb = root.Backbone;
64
    var _ = root._;
65
    
66
    // init quota namespace
67
    snf.quota = {};
68
    
69
    snf.quota.Quota = function(defaultns) {
70
        if (defaultns == undefined) { defaultns = "" }
71
        this.ns = defaultns;
72
        this.data = {};
73
    }
74

    
75
    _.extend(snf.quota.Quota.prototype, bb.Events, {
76

    
77
      load: function(resp) {
78
        this.data = {};
79
        _.each(resp, function(q) {
80
          if (this.data[q.name]) {
81
            _.extend(this.data[q.name], q)
82
          } else {
83
            this.data[q.name] = q;
84
          }
85

    
86
          q.maxValue = parseInt(q.maxValue);
87
          q.currValue = parseInt(q.currValue);
88
          this.update_exceeded(q.name, true);
89
        }, this);
90
      },
91
    
92
      get_key: function(key) {
93
        if (key.indexOf(".") == -1) {
94
          return this.ns + "." + key;
95
        }
96
        return key;
97
      },
98
      
99
      get: function(key) {
100
        if (this.get_key(key) in this.data) {
101
          return this.data[this.get_key(key)]
102
        }
103
        return {}
104
      },
105

    
106
      update_exceeded: function(key, silent) {
107
        if (silent === undefined) { silent = false; }
108
        
109
        var q = this.get(key);
110
        var oldexceeded = q.exceeded;
111
        q.exceeded = this.exceeded(key);
112
        if (q.exceeded != oldexceeded) {
113
          key = this.get_key(key);
114
          this.trigger("quota.changed", key, this);
115
          this.trigger(key + ".quota.changed", this);
116
          if (q.exceeded) { this.trigger("quota.reached", this)}
117
          if (!q.exceeded) { this.trigger("quota.free", this)}
118
          if (q.exceeded) { this.trigger(key + ".quota.reached", this)}
119
          if (!q.exceeded) { this.trigger(key + ".quota.free", this)}
120
        }
121
      },
122

    
123
      update_usage: function(key, value) {
124
        this.get(key).currValue = parseInt(value);
125
        this.update_exceeded(key);
126
      },
127

    
128
      update_limit: function(key, value) {
129
        this.get(key).maxValue = parseInt(value);
130
        this.update_exceeded(key);
131
      },
132

    
133
      get_usage: function(key) {
134
        return parseInt(this.get(key).currValue);
135
      },
136

    
137
      get_limit: function(key) {
138
        return parseInt(this.get(key).maxValue);
139
      },
140

    
141
      is_bytes: function(key) {
142
        return this.get(key).unit == "bytes";
143
      },
144

    
145
      get_available: function(key) {
146
        return this.get_limit(key) - this.get_usage(key)
147
      },
148
      
149
      exceeded: function(key) {
150
        return this.get_usage(key) >= this.get_limit(key);
151
      },
152

    
153
      can_consume: function(key, value) {
154
        return (this.get_available(key) - parseInt(value)) >= 0
155
      },
156

    
157
      get_available_readable: function(key) {
158
        var value;
159
        if (this.is_bytes(key)) {
160
          var value = this.get_available(key);
161
          if (!value) { return 0 }
162
          return snf.util.readablizeBytes(value);
163
        } else {
164
          return this.get_available(key);
165
        }
166
      }
167

    
168
    });
169

    
170
})(this);
171