@cocreate/usage 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # CoCreate-usage
2
+
3
+ A simple usage component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API. Take it for a spin in our [playground!](https://cocreate.app/docs/usage)
4
+
5
+ ![GitHub latest release](https://img.shields.io/github/v/release/CoCreate-app/CoCreate-usage?style=flat-square)
6
+ ![GitHub contributors](https://img.shields.io/github/contributors/CoCreate-app/CoCreate-usage?style=flat-square)
7
+ ![License](https://img.shields.io/static/v1?style=flat-square&label=license&message=SSPL&color=green)
8
+ ![Hiring](https://img.shields.io/static/v1?style=flat-square&label=&message=Hiring&color=blueviolet)
9
+
10
+ ![CoCreate](https://cdn.cocreate.app/logo.png)
11
+
12
+ [CoCreate Docs](https://cocreate.app/docs/usage)
13
+
14
+ We want this library to be community-driven, and CoCreate led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create [issues](https://github.com/CoCreate-app/Realtime_Admin_CRM_and_CMS/issues) and [pull requests](https://github.com/CoCreate-app/Realtime_Admin_CRM_and_CMS/pulls) or merely upvote or comment on existing issues or pull requests.
15
+
16
+ We appreciate your continued support, thank you!
17
+
18
+ # Table of Contents
19
+
20
+ - [Table of Contents](#table-of-contents)
21
+ - [Announcements](#announcements)
22
+ - [Roadmap](#roadmap)
23
+ - [How to Contribute](#how-to-contribute)
24
+ - [About](#about)
25
+ - [License](#license)
26
+
27
+ <a name="announcements"></a>
28
+
29
+ # Announcements
30
+
31
+ All updates to this library are documented in our [CHANGELOG](https://github.com/CoCreate-app/CoCreate-usage/blob/master/CHANGELOG.md) and [releases](https://github.com/CoCreate-app/CoCreate-usage/releases). You may also subscribe to email for releases and breaking changes.
32
+
33
+ <a name="roadmap"></a>
34
+
35
+ # Roadmap
36
+
37
+ If you are interested in the future direction of this project, please take a look at our open [issues](https://github.com/CoCreate-app/CoCreate-usage/issues) and [pull requests](https://github.com/CoCreate-app/CoCreate-usage/pulls). We would love to hear your feedback.
38
+
39
+ <a name="about"></a>
40
+
41
+ # About
42
+
43
+ CoCreate-usage is guided and supported by the CoCreate Developer Experience Team.
44
+
45
+ Please Email the Developer Experience Team [here](mailto:develop@cocreate.app) in case of any queries.
46
+
47
+ CoCreate-usage is maintained and funded by CoCreate. The names and logos for CoCreate are trademarks of CoCreate, LLC.
48
+
49
+ <a name="contribute"></a>
50
+
51
+ # How to Contribute
52
+
53
+ We encourage contribution to our libraries (you might even score some nifty swag), please see our [CONTRIBUTING](https://github.com/CoCreate-app/CoCreate-usage/blob/master/CONTRIBUTING.md) guide for details.
54
+
55
+ <a name="license"></a>
56
+
57
+ # License
58
+
59
+ This software is dual-licensed under the GNU Affero General Public License version 3 (AGPLv3) and a commercial license.
60
+
61
+ - **Open Source Use**: For open-source projects and non-commercial use, this software is available under the AGPLv3. The AGPLv3 allows you to freely use, modify, and distribute this software, provided that all modifications and derivative works are also licensed under the AGPLv3. For the full license text, see the [LICENSE file](https://github.com/CoCreate-app/CoCreate-usage/blob/master/LICENSE).
62
+
63
+ - **Commercial Use**: For-profit companies and individuals intending to use this software for commercial purposes must obtain a commercial license. The commercial license is available when you sign up for an API key on our [website](https://cocreate.app). This license permits proprietary use and modification of the software without the copyleft requirements of the AGPLv3. It is ideal for integrating this software into proprietary commercial products and applications.
64
+
65
+ If you have not purchased a commercial license and intend to use this software for commercial purposes, you are required to sign up for an API key on our website.
package/demo/server.js ADDED
File without changes
@@ -0,0 +1,223 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <title>CoCreate-usage Documentation | CoCreateJS</title>
8
+ <link
9
+ rel="icon"
10
+ type="image/png"
11
+ sizes="32x32"
12
+ href="https://cocreate.app/images/favicon.ico" />
13
+ <meta
14
+ name="description"
15
+ content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
+ <meta
17
+ name="keywords"
18
+ content="helper classes, utility classes, css framework, css library, inline style classes" />
19
+ <meta name="robots" content="index,follow" />
20
+
21
+ <!-- CoCreate CSS -->
22
+ <link
23
+ rel="stylesheet"
24
+ href="https://cdn.cocreate.app/latest/CoCreate.min.css"
25
+ type="text/css" />
26
+ </head>
27
+
28
+ <body>
29
+ <div array="" object="" key="" id="cocreate-usage">
30
+ <div
31
+ class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
32
+ <div class="display:flex align-items:center">
33
+ <h2>CoCreate-usage</h2>
34
+ </div>
35
+ <div
36
+ class="display:flex align-items:center font-size:20px"
37
+ share-height="600"
38
+ share-width="500"
39
+ share-media="https://via.placeholder.com/300/09f/fff.png">
40
+ <a
41
+ href="https://github.com/CoCreate-app/CoCreate-usage"
42
+ target="_blank"
43
+ class="margin-right:15px"
44
+ ><i class="fab fa-github"></i
45
+ ></a>
46
+ <a
47
+ class="margin-right:15px share"
48
+ share-network="twitter"
49
+ title="Share on twitter"
50
+ ><i class="fab fa-twitter"></i
51
+ ></a>
52
+ <a
53
+ class="margin-right:15px share"
54
+ share-network="facebook"
55
+ title="Share on Facebook"
56
+ ><i class="fab fa-facebook"></i
57
+ ></a>
58
+ <a
59
+ class="margin-right:15px share"
60
+ share-network="instagram"
61
+ title="Share on instagram"
62
+ ><i class="fab fa-instagram"></i
63
+ ></a>
64
+ <a
65
+ class="margin-right:15px share"
66
+ share-network="share"
67
+ title="Share on share"
68
+ ><i class="fas fa-share-alt"></i
69
+ ></a>
70
+ </div>
71
+ </div>
72
+ <h1 class="max-width:500px margin:20px_10px">
73
+ A simple HTML5, CSS and pure javascript component. Easy
74
+ configuration using data-attributes and highly styleable.
75
+ </h1>
76
+ <div id="usage-section" class="display:flex flex-wrap:wrap">
77
+ <div
78
+ class="flex-grow:1 min-width:300px width:50% padding:20px_10px">
79
+ <h2
80
+ class="border-bottom:1px_solid_lightgrey padding:5px_0px">
81
+ Install
82
+ </h2>
83
+ <pre
84
+ class="margin-top:15px"><code class="language-javascript">npm install cocreate-usage</code></pre>
85
+ <p class="padding:10px_0px">Or you can use cdn link:</p>
86
+ <pre><code class="language-javascript">https://cdn.cocreate.app/js/CoCreate-usage.min.js</code></pre>
87
+
88
+ <h2
89
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
90
+ Usage
91
+ </h2>
92
+ <p class="padding:10px_0px">usage usage content</p>
93
+ <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;</code></pre>
94
+
95
+ <h2
96
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
97
+ Reference
98
+ </h2>
99
+ <p class="padding:10px_0px">
100
+ This is usage reference content
101
+ </p>
102
+ <pre><code class="language-javascript">&lt;div&gt;&lt;/div&gt;</code></pre>
103
+ <p class="padding:10px_0px">
104
+ This is usage reference content
105
+ </p>
106
+
107
+ <h2
108
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
109
+ Attributes
110
+ </h2>
111
+ <ul class="list-style-type:none">
112
+ <li
113
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
114
+ <h4>
115
+ <span>usage</span>
116
+ <span class="cocreate-badge success"
117
+ >string</span
118
+ >
119
+ <span class="cocreate-badge warning"
120
+ >optional</span
121
+ >
122
+ </h4>
123
+ <p>usage-attribute</p>
124
+ </li>
125
+ <li
126
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
127
+ <h4>
128
+ <span>usage</span>
129
+ <span class="cocreate-badge success"
130
+ >string</span
131
+ >
132
+ <span class="cocreate-badge warning"
133
+ >optional</span
134
+ >
135
+ </h4>
136
+ <p>usage-attribute</p>
137
+ </li>
138
+ </ul>
139
+ </div>
140
+
141
+ <div
142
+ class="flex-grow:1 min-width:300px width:50% padding:0px_10px margin:20px_0px border-bottom:1px_solid_lightgrey">
143
+ <!-- SandBox -->
144
+ <div
145
+ class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px"
146
+ id="playground">
147
+ <div
148
+ id="demo-code"
149
+ resizable
150
+ class="position:relative height:50%">
151
+ <textarea
152
+ type="code"
153
+ lang="html"
154
+ array="demos"
155
+ object=""
156
+ key="demo"
157
+ save="false"
158
+ id="demo"
159
+ class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
160
+ <div
161
+ resize="bottom"
162
+ class="background:lightgrey"></div>
163
+ </div>
164
+
165
+ <div
166
+ id="demo-preview"
167
+ class="position:relative overflow:auto background-color:white">
168
+ <div get-value="#demo" class="padding:20px"></div>
169
+ </div>
170
+
171
+ <div
172
+ class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
173
+ <a
174
+ class="margin-right:10px"
175
+ id="eye"
176
+ show="#eye-slash"
177
+ hide="#eye, #demo-preview"
178
+ toggle="code-height"
179
+ toggle-selector="#demo-code"
180
+ ><i class="far fa-eye"></i
181
+ ></a>
182
+ <a
183
+ class="margin-right:10px"
184
+ hidden
185
+ id="eye-slash"
186
+ show="#eye, #demo-preview"
187
+ hide="#eye-slash"
188
+ toggle="code-height"
189
+ toggle-selector="#demo-code"
190
+ ><i class="fas fa-eye-slash"></i
191
+ ></a>
192
+ <a
193
+ class="margin-right:10px"
194
+ id="code"
195
+ show="#code-slash"
196
+ hide="#code, #demo-code"
197
+ ><i class="fa fa-code"></i
198
+ ></a>
199
+ <a
200
+ class="margin-right:10px"
201
+ hidden
202
+ id="code-slash"
203
+ show="#code, #demo-code"
204
+ hide="#code-slash"
205
+ ><i class="fas fa-code"></i
206
+ ></a>
207
+ <a
208
+ class="margin-right:5px"
209
+ fullscreen
210
+ target="#playground"
211
+ ><i class="fas fa-expand"></i
212
+ ></a>
213
+ </div>
214
+ </div>
215
+ <!-- End SandBox -->
216
+ </div>
217
+ </div>
218
+ </div>
219
+
220
+ <!--CoCreateJS-->
221
+ <script src="https://CoCreate.app/dist/CoCreate.js"></script>
222
+ </body>
223
+ </html>
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@cocreate/usage",
3
+ "version": "1.0.0",
4
+ "description": "CoCreate-usage",
5
+ "keywords": [
6
+ "cocreate-usage",
7
+ "cocreate",
8
+ "low-code-framework",
9
+ "no-code-framework",
10
+ "cocreatejs",
11
+ "cocreatejs-component",
12
+ "cocreate-framework",
13
+ "no-code",
14
+ "low-code",
15
+ "collaborative-framework",
16
+ "realtime",
17
+ "realtime-framework",
18
+ "collaboration",
19
+ "shared-editing",
20
+ "html5-framework",
21
+ "javascript-framework"
22
+ ],
23
+ "main": "./src/index.js",
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/CoCreate-app/CoCreate-usage.git"
30
+ },
31
+ "author": "CoCreate LLC",
32
+ "license": "AGPL-3.0",
33
+ "bugs": {
34
+ "url": "https://github.com/CoCreate-app/CoCreate-usage/issues"
35
+ },
36
+ "homepage": "https://cocreate.app/docs/CoCreate-usage",
37
+ "scripts": {
38
+ "demo": "PORT=5000 node demo/server.js",
39
+ "test": "echo \"Error: no test specified\" && exit 1",
40
+ "postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
41
+ }
42
+ }
@@ -0,0 +1,22 @@
1
+ module.exports = {
2
+ dryRun: false,
3
+ branches: ["master"],
4
+ plugins: [
5
+ "@semantic-release/commit-analyzer",
6
+ "@semantic-release/release-notes-generator",
7
+ [
8
+ "@semantic-release/changelog",
9
+ {
10
+ changelogFile: "CHANGELOG.md",
11
+ },
12
+ ],
13
+ "@semantic-release/npm",
14
+ "@semantic-release/github",
15
+ [
16
+ "@semantic-release/git",
17
+ {
18
+ assets: ["CHANGELOG.md", "package.json"],
19
+ },
20
+ ],
21
+ ],
22
+ };
package/src/index.js ADDED
@@ -0,0 +1,204 @@
1
+ class CoCreateUsage {
2
+ constructor(crud) {
3
+ this.wsManager = crud.wsManager;
4
+ this.crud = crud;
5
+ this.init();
6
+ this.organizations = new Map()
7
+ }
8
+
9
+ init() {
10
+ if (this.wsManager) {
11
+ this.wsManager.on('setBandwidth', (data) => this.setBandwidth(data));
12
+ }
13
+ }
14
+
15
+
16
+ async setBandwidth({ type, data, organization_id }) {
17
+ try {
18
+ let dataTransfered = this.getBytes(data)
19
+
20
+ if (!dataTransfered || !organization_id)
21
+ return
22
+ // TODO: set duration
23
+ let org = this.organizations.get(organization_id)
24
+ if (!org) {
25
+ let self = this
26
+
27
+ org = {}
28
+ org.debounce = setTimeout(() => {
29
+ self.send(org, organization_id); // Call the callback to display the count
30
+ self.organizations.delete(organization_id)
31
+ }, 60000);
32
+
33
+ org.dataTransferedIn = 0;
34
+ org.dataTransferedInCount = 0;
35
+ org.dataTransferedOut = 0;
36
+ org.dataTransferedOutCount = 0;
37
+
38
+ this.organizations.set(organization_id, org)
39
+ }
40
+
41
+ if (type === 'in') {
42
+ org.dataTransferedIn = dataTransfered;
43
+ org.dataTransferedInCount++
44
+ } else if (type === 'out') {
45
+ org.dataTransferedOut = dataTransfered;
46
+ org.dataTransferedOutCount++
47
+ } else {
48
+ console.log('else')
49
+ }
50
+
51
+ } catch (error) {
52
+ console.log('Usage error', error)
53
+ }
54
+ }
55
+
56
+
57
+ async send(org, organization_id) {
58
+ delete org.debounce
59
+
60
+ org.dataTransferedOut += 250
61
+ org.dataTransferedOutCount++
62
+
63
+ const platformOrganization = this.crud.config.organization_id
64
+ let organization = await this.crud.send({
65
+ method: 'object.read',
66
+ array: 'organizations',
67
+ object: { _id: organization_id },
68
+ organization_id: platformOrganization
69
+ })
70
+
71
+ org.dataTransferedIn += this.getBytes(platformOrganization)
72
+ org.dataTransferedInCount++
73
+
74
+ if (organization && organization.object && organization.object[0]) {
75
+ organization = organization.object[0]
76
+ } else return
77
+
78
+ //TODO: if (organization.transactionInterval) // set in global map to use with timeout 1 sec intervals to 3600 sec
79
+ if (this.wsManager.organizations.has(organization_id)) {
80
+ if (organization.balance <= 0) {
81
+ this.wsManager.organizations.get(organization_id).status = false
82
+ this.wsManager.organizations.get(organization_id).organizationBalance = false
83
+ this.wsManager.organizations.get(organization_id).error = 'Your balance has fallen bellow 0'
84
+
85
+ } else {
86
+ this.wsManager.organizations.get(organization_id).status = true
87
+ this.wsManager.organizations.get(organization_id).organizationBalance = true
88
+ this.wsManager.organizations.get(organization_id).error = ''
89
+ }
90
+ }
91
+
92
+ let timeStamp = new Date(new Date().toISOString());
93
+ let isExpired = false
94
+ if (organization.lastDeposit) {
95
+ let lastDeposit = new Date(organization.lastDeposit)
96
+ isExpired = lastDeposit <= timeStamp.setFullYear(timeStamp.getFullYear() - 1)
97
+ }
98
+
99
+ let isResetDataTransfer = false
100
+ if (organization.modified && organization.modified.on) {
101
+ let previousTimeStamp = new Date(organization.modified.on)
102
+ if (previousTimeStamp.getMonth() !== timeStamp.getMonth()) {
103
+ isResetDataTransfer = true
104
+ this.crud.send({
105
+ method: 'object.create',
106
+ array: 'transactions',
107
+ object: {
108
+ organization_id,
109
+ type: "withdrawal", // deposit, credit, withdrawal, debit
110
+ dataTransfered: organization.dataTransfered,
111
+ previousTimeStamp
112
+ },
113
+ organization_id: platformOrganization,
114
+ timeStamp
115
+ });
116
+ organization.dataTransfered = 0
117
+ }
118
+ }
119
+
120
+ let dataTransfered = org.dataTransferedIn + org.dataTransferedOut
121
+ dataTransfered = dataTransfered / 1073741824;
122
+ dataTransfered = dataTransfered.toFixed(32);
123
+ dataTransfered = parseFloat(dataTransfered);
124
+
125
+ let rate = this.getRate(organization.dataTransfered)
126
+ let amount = dataTransfered * rate
127
+ amount = -amount
128
+ amount = amount.toFixed(32)
129
+ amount = parseFloat(amount)
130
+
131
+ let balanceUpdate = {
132
+ method: 'object.update',
133
+ array: 'organizations',
134
+ object: { _id: organization_id },
135
+ organization_id: platformOrganization,
136
+ timeStamp
137
+ }
138
+
139
+ if (isExpired)
140
+ balanceUpdate.object['balance'] = 0
141
+ else
142
+ balanceUpdate.object.$inc = { balance: amount }
143
+
144
+ if (isResetDataTransfer)
145
+ balanceUpdate.object['dataTransfered'] = 0
146
+ else if (!balanceUpdate.object.$inc)
147
+ balanceUpdate.object.$inc = { dataTransfered }
148
+ else
149
+ balanceUpdate.object.$inc.dataTransfered = dataTransfered
150
+
151
+ // console.log('balanceUpdate: ', balanceUpdate)
152
+
153
+ this.crud.send(balanceUpdate)
154
+
155
+ let transaction = {
156
+ method: 'object.create',
157
+ array: 'transactions',
158
+ object: {
159
+ organization_id,
160
+ type: "withdrawal", // deposit, credit, withdrawal, debit
161
+ amount,
162
+ rate,
163
+ dataTransfered,
164
+ ...org
165
+ },
166
+ organization_id,
167
+ timeStamp
168
+ }
169
+
170
+ this.crud.send(transaction);
171
+
172
+ }
173
+
174
+ getBytes(data) {
175
+ if (typeof data === 'number')
176
+ return data;
177
+ else if (data instanceof Buffer)
178
+ return data.byteLength || 0;
179
+ else if (data instanceof String || typeof data === 'string')
180
+ return Buffer.byteLength(data, 'utf8') || 0;
181
+ else if (typeof data === 'object')
182
+ return Buffer.byteLength(JSON.stringify(data), 'utf8') || 0;
183
+ else return 0
184
+ }
185
+
186
+ getRate(totalUsage = 0) {
187
+ const tiers = [
188
+ { limit: 1, rate: 10 },
189
+ { limit: 10, rate: 5 },
190
+ { limit: 100, rate: 2.5 },
191
+ { limit: 1000, rate: 1.125 },
192
+ { limit: 10000, rate: 0.562 },
193
+ { limit: 100000, rate: 0.281 },
194
+ { limit: 1000000, rate: 0.145 }
195
+ ];
196
+
197
+ const matchingTier = tiers.find(tier => totalUsage < tier.limit);
198
+
199
+ return matchingTier ? matchingTier.rate : 0.12;
200
+ }
201
+
202
+ }
203
+
204
+ module.exports = CoCreateUsage;