1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 | var Stat = (function () {
|
6 | function Stat(appId, stat, value) {
|
7 | if (value === void 0) { value = 1; }
|
8 | this.appId = appId;
|
9 | this.stat = stat;
|
10 | this.value = value;
|
11 | this.appId = appId;
|
12 | this.stat = stat;
|
13 | this.value = value;
|
14 | this.created = new Date();
|
15 | }
|
16 | Stat.prototype.toJSON = function () {
|
17 | return {
|
18 | app_id: this.appId,
|
19 | stat: this.stat,
|
20 | value: this.value,
|
21 | created: this.created.toISOString(),
|
22 | };
|
23 | };
|
24 | return Stat;
|
25 | }());
|
26 | exports.Stat = Stat;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var Insights = (function () {
|
34 | function Insights(deps, options) {
|
35 | var _this = this;
|
36 | if (options === void 0) { options = {
|
37 | 'intervalSubmit': 60 * 1000,
|
38 | 'intervalActiveCheck': 1000,
|
39 | 'submitCount': 100
|
40 | }; }
|
41 | this.options = options;
|
42 | this.app = deps.appStatus;
|
43 | this.storage = deps.storage;
|
44 | this.config = deps.config;
|
45 | this.client = deps.client;
|
46 | this.logger = deps.logger;
|
47 | this.batch = [];
|
48 | setInterval(function () {
|
49 | _this.submit();
|
50 | }, this.options.intervalSubmit);
|
51 | setInterval(function () {
|
52 | if (!_this.app.closed) {
|
53 | _this.checkActivity();
|
54 | }
|
55 | }, this.options.intervalActiveCheck);
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | Insights.prototype.track = function (stat, value) {
|
64 | if (value === void 0) { value = 1; }
|
65 | this.trackStat(new Stat(this.config.get('app_id'), stat, value));
|
66 | };
|
67 | Insights.prototype.checkActivity = function () {
|
68 | var session = this.storage.get('insights_session');
|
69 | if (!session) {
|
70 | this.markActive();
|
71 | }
|
72 | else {
|
73 | var d = new Date(session);
|
74 | var hour = 60 * 60 * 1000;
|
75 | if (d.getTime() + hour < new Date().getTime()) {
|
76 | this.markActive();
|
77 | }
|
78 | }
|
79 | };
|
80 | Insights.prototype.markActive = function () {
|
81 | this.storage.set('insights_session', new Date().toISOString());
|
82 | this.track('mobileapp.active');
|
83 | };
|
84 | Insights.prototype.trackStat = function (stat) {
|
85 | this.batch.push(stat);
|
86 | if (this.shouldSubmit()) {
|
87 | this.submit();
|
88 | }
|
89 | };
|
90 | Insights.prototype.shouldSubmit = function () {
|
91 | return this.batch.length >= this.options.submitCount;
|
92 | };
|
93 | Insights.prototype.submit = function () {
|
94 | var _this = this;
|
95 | if (this.batch.length === 0) {
|
96 | return;
|
97 | }
|
98 | var insights = [];
|
99 | for (var _i = 0, _a = this.batch; _i < _a.length; _i++) {
|
100 | var stat = _a[_i];
|
101 | insights.push(stat.toJSON());
|
102 | }
|
103 | this.client.post('/insights')
|
104 | .send({ 'insights': insights })
|
105 | .end(function (err, res) {
|
106 | if (err) {
|
107 | _this.logger.error('Ionic Insights: Could not send insights.', err);
|
108 | }
|
109 | });
|
110 | this.batch = [];
|
111 | };
|
112 | return Insights;
|
113 | }());
|
114 | exports.Insights = Insights;
|