-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathretention.ts
More file actions
executable file
·133 lines (120 loc) · 3.32 KB
/
retention.ts
File metadata and controls
executable file
·133 lines (120 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as _ from 'lodash';
import utils from '../../index';
import { DataFormatTypes, IDataFormat, formatWarn, getPrefix } from '../common';
import { IDataSourcePlugin } from '../../../data-sources/plugins/DataSourcePlugin';
/**
* Received a result in the form of:
* values: [
* {
* totalUnique: number
* totalUniqueUsersIn24hr: number
* totalUniqueUsersIn7d: number
* totalUniqueUsersIn30d: number
* returning24hr: number
* returning7d: number
* returning30d: number
* }
* ]
*
* And returns the following format:
* {
* total: number
* returning: number
* values: [
* {
* timespan: '24 hours',
* retention: '%',
* returning: number,
* unique:number
* },
* {
* timespan: '7 days',
* retention: '%',
* returning: number,
* unique:number
* }
* {
* timespan: '30 days',
* retention: '%',
* returning: number,
* unique:number
* }
* ]
* }
*
* @param format 'retention' | {
* type: 'retention',
* args: {
* prefix: string - a prefix string for the exported variables (default to id).
* data: string - the state property holding the data (default is 'values').
* }
* }
* @param state Current received state from data source
* @param dependencies Dependencies for the plugin
* should contain "selectedTimespan" equals to 'PT24H', 'P7D' etc...
* @param plugin The entire plugin (for id generation, params etc...)
*/
export function retention (
format: string | IDataFormat,
state: any,
dependencies: IDictionary,
plugin: IDataSourcePlugin,
prevState: any) {
const prefix = getPrefix(format);
const args = typeof format !== 'string' && format.args || {};
let values = state[args.data || 'values'];
if (!values || !values.length) { return null; }
const { selectedTimespan } = dependencies;
let result = {
totalUnique: 0,
totalUniqueUsersIn24hr: 0,
totalUniqueUsersIn7d: 0,
totalUniqueUsersIn30d: 0,
returning24hr: 0,
returning7d: 0,
returning30d: 0,
total: 0,
returning: 0,
values: []
};
_.extend(result, values[0]);
switch (selectedTimespan) {
case 'PT24H':
result.total = result.totalUniqueUsersIn24hr;
result.returning = result.returning24hr;
break;
case 'P7D':
result.total = result.totalUniqueUsersIn7d;
result.returning = result.returning7d;
break;
case 'P30D':
result.total = result.totalUniqueUsersIn30d;
result.returning = result.returning30d;
break;
default:
result.total = 0;
result.returning = 0;
break;
}
result[prefix + 'values'] = [
{
timespan: '24 hours',
retention: Math.round(100 * result.returning24hr / result.totalUniqueUsersIn24hr || 0) + '%',
returning: result.returning24hr,
unique: result.totalUniqueUsersIn24hr
},
{
timespan: '7 days',
retention: Math.round(100 * result.returning7d / result.totalUniqueUsersIn7d || 0) + '%',
returning: result.returning7d,
unique: result.totalUniqueUsersIn7d
},
{
timespan: '30 days',
retention: Math.round(100 * result.returning30d / result.totalUniqueUsersIn30d || 0) + '%',
returning: result.returning30d,
unique: result.totalUniqueUsersIn30d
},
];
return result;
}