countlyCommon
- Description:
Object with common functions to be used for multiple purposes
- Source:
Members
(static) ACTIVE_APP_ID :string|number
- Description:
App ID of currently selected app or 0 when not initialized
- Source:
App ID of currently selected app or 0 when not initialized
Type:
- string | number
(static) ACTIVE_APP_KEY :string|number
- Description:
App Key of currently selected app or 0 when not initialized
- Source:
App Key of currently selected app or 0 when not initialized
Type:
- string | number
(static) BROWSER_LANG :string
- Description:
Current user's selected language in form en-EN, by default will use browser's language
- Source:
Current user's selected language in form en-EN, by default will use browser's language
Type:
- string
(static) BROWSER_LANG_SHORT :string
- Description:
Current user's browser language in short form as "en", by default will use browser's language
- Source:
Current user's browser language in short form as "en", by default will use browser's language
Type:
- string
(static) Levenshtein
- Description:
Based on the algorithm at http://en.wikipedia.org/wiki/Levenshtein_distance.
- Source:
Based on the algorithm at http://en.wikipedia.org/wiki/Levenshtein_distance.
(static) periodObj
- Description:
Currently selected period
- Source:
Properties:
| Name | Type | Attributes | Description |
|---|---|---|---|
currentPeriodArr |
array |
<optional> |
array with ticks for current period (available only for special periods), example ["2016.12.22","2016.12.23","2016.12.24", ...] |
previousPeriodArr |
array |
<optional> |
array with ticks for previous period (available only for special periods), example ["2016.12.22","2016.12.23","2016.12.24", ...] |
dateString |
string | date format to use when outputting date in graphs, example D MMM, YYYY |
|
isSpecialPeriod |
boolean | true if current period is special period, false if it is not |
|
daysInPeriod |
number | amount of full days in selected period, example 30 |
|
numberOfDays |
number | number of days selected period consists of, example hour period has 1 day |
|
periodContainsToday |
boolean | true if period contains today, false if not |
|
uniquePeriodArr |
array | array with ticks for current period which contains data for unique values, like unique users, example ["2016.12.22","2016.w52","2016.12.30", ...] |
|
uniquePeriodCheckArr |
array | array with ticks for higher buckets to current period unique value estimation, example ["2016.w51","2016.w52","2016.w53","2017.1",...] |
|
previousUniquePeriodArr |
array | array with ticks for previous period which contains data for unique values, like unique users, example ["2016.12.22","2016.w52","2016.12.30"] |
|
previousUniquePeriodCheckArr |
array | array with ticks for higher buckets to previous period unique value estimation, example ["2016.w47","2016.w48","2016.12"] |
|
activePeriod |
string | period name formatted in dateString |
|
previousPeriod |
string | previous period name formatted in dateString |
|
periodMax |
number | max value of current period tick |
|
periodMin |
number | min value of current period tick |
Currently selected period
Examples
Special period object (7days)
{
"currentPeriodArr":["2017.1.14","2017.1.15","2017.1.16","2017.1.17","2017.1.18","2017.1.19","2017.1.20"],
"previousPeriodArr":["2017.1.7","2017.1.8","2017.1.9","2017.1.10","2017.1.11","2017.1.12","2017.1.13"],
"isSpecialPeriod":true,
"dateString":"D MMM",
"daysInPeriod":7,
"numberOfDays":7,
"uniquePeriodArr":["2017.1.14","2017.w3"],
"uniquePeriodCheckArr":["2017.w2","2017.w3"],
"previousUniquePeriodArr":["2017.1.7","2017.1.8","2017.1.9","2017.1.10","2017.1.11","2017.1.12","2017.1.13"],
"previousUniquePeriodCheckArr":["2017.w1","2017.w2"],
"periodContainsToday":true
}
Simple period object (today period - hour)
{
"activePeriod":"2017.1.20",
"periodMax":23,
"periodMin":0,
"previousPeriod":"2017.1.19",
"isSpecialPeriod":false,
"dateString":"HH:mm",
"daysInPeriod":0,
"numberOfDays":1,
"uniquePeriodArr":[],
"uniquePeriodCheckArr":[],
"previousUniquePeriodArr":[],
"previousUniquePeriodCheckArr":[],
"periodContainsToday":true
}
Methods
(static) arrayAddUniq(arr, item)
- Description:
Add item or array to existing array only if values are not already in original array. given array is modified.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
array | original array where to add unique elements |
item |
string | number | array | item to add or array to merge |
(static) browserLang() → {string}
- Description:
Get Browser language
- Source:
Example
//outputs en-US
countlyCommon.browserLang()
Returns:
browser locale in iso format en-US
- Type
- string
(static) calcSpecificPeriodObj(period, currentTimeStamp) → {object}
- Description:
Getter for period object by providing period string value
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
period |
object | given period |
currentTimeStamp |
number | timestamp |
Returns:
returns countlyCommon.periodObj
- Type
- object
(static) calculateBarData(rangeData) → {array}
- Description:
Extracts top three items (from rangeArray) that have the biggest total session counts from the chartData.
- Source:
Example
Return data
[
{"name":"iOS","percent":35},
{"name":"Android","percent":33},
{"name":"Windows Phone","percent":32}
]
Parameters:
| Name | Type | Description |
|---|---|---|
rangeData |
object | chartData retrieved from |
Returns:
array with top 3 values
- Type
- array
(static) calculateBarDataWPercentageOfTotal(rangeData, metric, fixBarSegmentData) → {array}
- Description:
Extracts top three items (from rangeArray) that have the biggest total session counts from the chartData with their percentage of total
- Source:
Example
Return data
[
{"name":"iOS","percent":44},
{"name":"Android","percent":22},
{"name":"Windows Phone","percent":14}
]
Parameters:
| Name | Type | Description |
|---|---|---|
rangeData |
object | chartData retrieved from |
metric |
String | name of the metric to use ordering and returning |
fixBarSegmentData |
function | Function to fix bar data segment data |
Returns:
array with top 3 values
- Type
- array
(static) checkGraphType(type, settings) → {boolean}
- Description:
Checks if current graph type matches the one being drawn
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
type |
string | graph type |
settings |
object | graph settings |
Returns:
Return true if type is the same
- Type
- boolean
(static) compareVersions(a,, b,) → {Number}
- Description:
Compare two versions
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
a, |
String | First version |
b, |
String | Second version |
Returns:
returns -1, 0 or 1 by result of comparing
- Type
- Number
(static) decode(str) → {string}
- Description:
Decode value from db, decoding first $ to $ and all . to . (dots). Decodes also url encoded values as $.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
str |
string | value to decode |
Returns:
decoded string
- Type
- string
(static) decodeHtml(html) → {string}
- Description:
Decode escaped HTML from db
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
html |
string | value to decode |
Returns:
decoded string
- Type
- string
(static) deepObjectExtend(target, source) → {Object}
- Description:
Recursively merges an object into another
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
target |
Object | object to be merged into |
source |
Object | object to merge into the target |
Returns:
target after the merge
- Type
- Object
(static) divide(val1, val2) → {number}
- Description:
Safe division between numbers providing 0 as result in cases when dividing by 0
- Source:
Example
//outputs 0
countlyCommon.divide(100, 0);
Parameters:
| Name | Type | Description |
|---|---|---|
val1 |
number | number which to divide |
val2 |
number | number by which to divide |
Returns:
result of division
- Type
- number
(static) dot(obj, is, value) → {varies}
- Description:
Getter/setter for dot notatons:
- Source:
Example
common.dot({a: {b: {c: 'string'}}}, 'a.b.c') === 'string'
common.dot({a: {b: {c: 'string'}}}, ['a', 'b', 'c']) === 'string'
common.dot({a: {b: {c: 'string'}}}, 'a.b.c', 5) === 5
common.dot({a: {b: {c: 'string'}}}, 'a.b.c') === 5
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
object | object to use |
is |
string | path of properties to get |
value |
varies | value to set |
Returns:
value at provided path
- Type
- varies
(static) drawGauge(targetEl, value, maxValue, gaugeColor, textField)
- Description:
Draws a gauge with provided value on procided container.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
targetEl |
string | object | selector for container or container object itself where to create graph |
value |
number | value to display on gauge |
maxValue |
number | maximal value of the gauge |
gaugeColor |
string | color of the gauge in hexadecimal string as #ffffff |
textField |
string | object | selector for container or container object itself where to output textual value |
(static) drawGraph(dataPoints, container, graphType, inGraphProperties) → {boolean}
- Description:
Draws a graph with the given dataPoints to container. Used for drawing bar and pie charts.
- Source:
Examples
Drawing Pie chart
countlyCommon.drawGraph({"dp":[
{"data":[[0,20]],"label":"Test1","color":"#52A3EF"},
{"data":[[0,30]],"label":"Test2","color":"#FF8700"},
{"data":[[0,50]],"label":"Test3","color":"#0EC1B9"}
]}, "#dashboard-graph", "pie");
Drawing bar chart, to comapre values with different color bars
//[-1,null] and [3,null] are used for offsets from left and right
countlyCommon.drawGraph({"dp":[
{"data":[[-1,null],[0,20],[1,30],[2,50],[3,null]],"color":"#52A3EF"}, //first bar set
{"data":[[-1,null],[0,50],[1,30],[2,20],[3,null]],"color":"#0EC1B9"} //second bar set
],
"ticks":[[-1,""],[0,"Test1"],[1,"Test2"],[2,"Test3"],[3,""]]
}, "#dashboard-graph", "separate-bar", {"series":{"stack":null}});
Drawing Separate bars chart, to comapre values with different color bars
//[-1,null] and [3,null] are used for offsets from left and right
countlyCommon.drawGraph({"dp":[
{"data":[[-1,null],[0,20],[1,null],[2,null],[3,null]],"label":"Test1","color":"#52A3EF"},
{"data":[[-1,null],[0,null],[1,30],[2,null],[3,null]],"label":"Test2","color":"#FF8700"},
{"data":[[-1,null],[0,null],[1,null],[2,50],[3,null]],"label":"Test3","color":"#0EC1B9"}
],
"ticks":[[-1,""],[0,"Test1"],[1,"Test2"],[2,"Test3"],[3,""]
]}, "#dashboard-graph", "separate-bar");
Parameters:
| Name | Type | Description |
|---|---|---|
dataPoints |
object | data poitns to draw on graph |
container |
string | object | selector for container or container object itself where to create graph |
graphType |
string | type of the graph, accepted values are bar, line, pie, separate-bar |
inGraphProperties |
object | object with properties to extend and use on graph library directly |
Returns:
false if container element not found, otherwise true
- Type
- boolean
(static) drawHorizontalStackedBars(data, intoElement, colorIndex)
- Description:
Draws horizibtally stacked bars like in platforms and density analytic sections.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
data |
array | data to draw in form of [{"data":[[0,85]],"label":"Test1"},{"data":[[0,79]],"label":"Test2"},{"data":[[0,78]],"label":"Test3"}] |
intoElement |
object | string | selector for container or container object itself where to create graph |
colorIndex |
number | index of color from |
(static) drawTimeGraph(dataPoints, container, bucketopt, overrideBucketopt, smallopt, appIdsForNotesopt, optionsopt)
- Description:
Draws a time line graph with the given dataPoints to container.
- Source:
Example
countlyCommon.drawTimeGraph([{
"data":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,12],[8,9],[9,10],[10,5],[11,8],[12,7],[13,9],[14,4],[15,6]],
"label":"Total Sessions",
"color":"#DDDDDD",
"mode":"ghost"
},{
"data":[[1,74],[2,69],[3,60],[4,17],[5,6],[6,3],[7,13],[8,25],[9,62],[10,34],[11,34],[12,33],[13,34],[14,30],[15,1]],
"label":"Total Sessions",
"color":"#333933"
}], "#dashboard-graph");
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
dataPoints |
object | data points to draw on graph |
|
container |
string | object | selector for container or container object itself where to create graph |
|
bucket |
string |
<optional> |
time bucket to display on graph. See |
overrideBucket |
string |
<optional> |
time bucket to display on graph. See |
small |
boolean |
<optional> |
if graph won't be full width graph |
appIdsForNotes |
array |
<optional> |
display notes from provided apps ids on graph, will not show notes when empty |
options |
object |
<optional> |
extra graph options, see flot documentation |
(static) encode(str) → {string}
- Description:
Encode value to be passed to db as key, encoding $ symbol to $ if it is first and all . (dot) symbols to . in the string
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
str |
string | value to encode |
Returns:
encoded string
- Type
- string
(static) encodeHtml(html) → {string}
- Description:
Encode html
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
html |
string | value to encode |
Returns:
encode string
- Type
- string
(static) encodeSomeHtml(html, options) → {string}
- Description:
Encode some tags, leaving those set in whitelist as they are.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
html |
string | value to encode |
options |
object | for encoding. Optional. If not passed, using default in common. |
Returns:
encode string
- Type
- string
(static) extendDbObj(dbObj, updateObj)
- Description:
Merge standard countly metric data object, by mergin updateObj retrieved from action=refresh api requests object into dbObj. Used for merging the received data for today to the existing data while updating the dashboard.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
dbObj |
object | standard metric data object |
updateObj |
object | standard metric data object retrieved from action=refresh request to last time bucket data only |
(static) extractBarData(db, rangeArray, clearFunction, fetchFunction) → {array}
- Description:
Extracts top three items (from rangeArray) that have the biggest total session counts from the db object.
- Source:
Example
Return data
[
{"name":"iOS","percent":35},
{"name":"Android","percent":33},
{"name":"Windows Phone","percent":32}
]
Parameters:
| Name | Type | Description |
|---|---|---|
db |
object | countly standard metric data object |
rangeArray |
object | array of all metrics/segments to extract (usually what is contained in meta) |
clearFunction |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
fetchFunction |
function | function to fetch property, default used is function (rangeArr, dataObj) {return rangeArr;} |
Returns:
array with top 3 values
- Type
- array
(static) extractBarDataWPercentageOfTotal(db, rangeArray, clearFunction, fetchFunction, metric, estOverrideMetric, fixBarSegmentData) → {array}
- Description:
Extracts top three items (from rangeArray) that have the biggest total session counts from the db object.
- Source:
Example
Return data
[
{"name":"iOS","percent":35},
{"name":"Android","percent":33},
{"name":"Windows Phone","percent":32}
]
Parameters:
| Name | Type | Description |
|---|---|---|
db |
object | countly standard metric data object |
rangeArray |
object | array of all metrics/segments to extract (usually what is contained in meta) |
clearFunction |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
fetchFunction |
function | function to fetch property, default used is function (rangeArr, dataObj) {return rangeArr;} |
metric |
String | name of the metric to use ordering and returning |
estOverrideMetric |
string | name of the total users estimation override, by default will use default _estOverrideMetric provided on initialization |
fixBarSegmentData |
function | function to make any adjustments to the extracted data based on segment |
Returns:
array with top 3 values
- Type
- array
(static) extractChartData(db, clearFunction, chartData, dataProperties, metric, disableHours) → {object}
- Description:
Extract single level data without metrics/segments, like total user data from users collection
- Source:
Examples
Extracting total users data from users collection
countlyCommon.extractChartData(_sessionDb, countlySession.clearObject, [
{ data:[], label:"Total Users" }
], [
{
name:"t",
func:function (dataObj) {
return dataObj["u"]
}
}
]);
Returned data
{"chartDP":[
{
"data":[[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0],[13,0],[14,0],[15,12]],
"label":"Total Sessions",
"color":"#DDDDDD",
"mode":"ghost"
},
{
"data":[[0,6],[1,14],[2,11],[3,18],[4,10],[5,32],[6,53],[7,55],[8,71],[9,82],[10,74],[11,69],[12,60],[13,17],[14,6],[15,3]],
"label":"Total Sessions",
"color":"#333933"
}
],
"chartData":[
{"date":"22 Dec, 2016","pt":0,"t":6},
{"date":"23 Dec, 2016","pt":0,"t":14},
{"date":"24 Dec, 2016","pt":0,"t":11},
{"date":"25 Dec, 2016","pt":0,"t":18},
{"date":"26 Dec, 2016","pt":0,"t":10},
{"date":"27 Dec, 2016","pt":0,"t":32},
{"date":"28 Dec, 2016","pt":0,"t":53},
{"date":"29 Dec, 2016","pt":0,"t":55},
{"date":"30 Dec, 2016","pt":0,"t":71},
{"date":"31 Dec, 2016","pt":0,"t":82},
{"date":"1 Jan, 2017","pt":0,"t":74},
{"date":"2 Jan, 2017","pt":0,"t":69},
{"date":"3 Jan, 2017","pt":0,"t":60},
{"date":"4 Jan, 2017","pt":0,"t":17},
{"date":"5 Jan, 2017","pt":0,"t":6},
{"date":"6 Jan, 2017","pt":12,"t":3}
],
"keyEvents":[{"min":0,"max":12},{"min":0,"max":82}]
}
Parameters:
| Name | Type | Description |
|---|---|---|
db |
object | countly standard metric data object |
clearFunction |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
chartData |
object | prefill chart data with labels, colors, etc |
dataProperties |
object | describing which properties and how to extract |
metric |
string | metric to select |
disableHours |
boolean | disable hourly data for graphs |
Returns:
object to use in timeline graph with {"chartDP":chartData, "chartData":_.compact(tableData), "keyEvents":keyEvents}
- Type
- object
(static) extractRangeData(db, propertyName, rangeArray, explainRange, myorder) → {array}
- Description:
Extract range data from standard countly metric data model
- Source:
Example
Extracting session frequency from users collection
//outputs [{"f":"First session","t":352,"percent":"88.4"},{"f":"2 days","t":46,"percent":"11.6"}]
countlyCommon.extractRangeData(_userDb, "f", _frequencies, countlySession.explainFrequencyRange);
Parameters:
| Name | Type | Description |
|---|---|---|
db |
object | countly standard metric data object |
propertyName |
string | name of the property to extract |
rangeArray |
object | array of all metrics/segments to extract (usually what is contained in meta) |
explainRange |
function | function to convert range/bucket index to meaningful label |
myorder |
array | arrays of preferred order for give keys. Optional. If not passed - sorted by values |
Returns:
array containing extracted ranged data as [{"f":"First session","t":352,"percent":"88.4"},{"f":"2 days","t":46,"percent":"11.6"}]
- Type
- array
(static) extractTwoLevelData(db, rangeArray, clearFunction, dataProperties, estOverrideMetricopt) → {object}
- Description:
Extract two level data with metrics/segments, like total user data from carriers collection
- Source:
Examples
Extracting carriers data from carriers collection
var chartData = countlyCommon.extractTwoLevelData(_carrierDb, ["At&t", "Verizon"], countlyCarrier.clearObject, [
{
name:"carrier",
func:function (rangeArr, dataObj) {
return rangeArr;
}
},
{ "name":"t" },
{ "name":"u" },
{ "name":"n" }
]);
Return data
{"chartData":['
{"carrier":"At&t","t":71,"u":62,"n":36},
{"carrier":"Verizon","t":66,"u":60,"n":30}
]}
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
db |
object | countly standard metric data object |
|
rangeArray |
object | array of all metrics/segments to extract (usually what is contained in meta) |
|
clearFunction |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
|
dataProperties |
object | describing which properties and how to extract |
|
estOverrideMetric |
object |
<optional> |
data from total users api request to correct unique user values |
Returns:
object to use in bar and pie charts with {"chartData":_.compact(tableData)}
- Type
- object
(static) formatDate(date, format) → {string}
- Description:
Format date based on some locale settings
- Source:
Example
//outputs Jan 20
countlyCommon.formatDate(moment(), "MMM D");
Parameters:
| Name | Type | Description |
|---|---|---|
date |
moment | moment js object |
format |
string | format string to use |
Returns:
date in formatted string
- Type
- string
(static) formatNumber(x) → {string}
- Description:
Formats the number by separating each 3 digits with
- Source:
Example
//outputs 1,234,567
countlyCommon.formatNumber(1234567);
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | number to format |
Returns:
formatted number
- Type
- string
(static) formatNumberSafe(x, fallback) → {string}
- Description:
Formats the number by separating each 3 digits with, falls back to a default value in case of NaN
- Source:
Example
//outputs 1,234,567
countlyCommon.formatNumberSafe(1234567);
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | number to format |
fallback |
string | fallback value for unparsable numbers |
Returns:
formatted number or fallback
- Type
- string
(static) formatSecond(second, trimToopt) → {string}
- Description:
Parse second to standard time format
- Source:
Example
trimTo = 2, "Xh Xm Xs" result will be trimmed to "Xh Xm"
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
second |
number | number |
||
trimTo |
number |
<optional> |
5
|
number [1,5] |
Returns:
return format "Xh Xm Xs", if trimTo is specified the length of the result is trimmed
- Type
- string
(static) formatSecondForDP(chartDPs, labelName) → {array}
- Description:
add one more column in chartDP[index].data to show string in dp
- Source:
Example
for example:
chartDPs = [
{color:"#88BBC8", label:"duration", data:[[0, 23], [1, 22]}],
{color:"#88BBC8", label:"count", data:[[0, 3], [1, 3]}],
}
lable = 'duration',
will return
chartDPs = [
{color:"#88BBC8", label:"duration", data:[[0, 23, "00:00:23"], [1, 22, "00:00:22"]}],
{color:"#88BBC8", label:"count", data:[[0, 3], [1, 3]}],
}
Parameters:
| Name | Type | Description |
|---|---|---|
chartDPs |
array | chart data points |
labelName |
string | label name |
Returns:
chartDPs
- Type
- array
(static) formatTime(timestamp) → {string}
- Description:
Format duration to units of how much time have passed
- Source:
Example
//outputs 47 year(s) 28 day(s) 11:54:26
countlyCommon.formatTime(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number | amount in seconds passed since some reference point |
Returns:
formated time with how much units passed
- Type
- string
(static) formatTimeAgo(timestamp) → {string}
- Description:
Format timestamp to twitter like time ago format with real date as tooltip and hidden data for exporting
- Source:
Example
//outputs <span title="Tue, 17 Jan 2017 13:54:26">3 days ago<a style="display: none;">|Tue, 17 Jan 2017 13:54:26</a></span>
countlyCommon.formatTimeAgo(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number | timestamp in seconds or miliseconds |
Returns:
formated time ago
- Type
- string
(static) formatTimeAgoText(timestamp) → {string}
- Description:
Format timestamp to twitter like time ago format with real date as tooltip and hidden data for exporting
- Source:
Example
//outputs ago time without html tags
countlyCommon.formatTimeAgo(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number | timestamp in seconds or miliseconds |
Returns:
formated time ago
- Type
- string
(static) formatTimeAndDateShort(timestamp) → {string}
- Description:
Format timestamp to D MMM YYYY, HH:mm
- Source:
Example
//outputs 16 Dec 2022, 12:16
countlyCommon.formatTimeAndDateShort(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number | timestamp in seconds or miliseconds |
Returns:
formated time and date
- Type
- string
(static) getDashboardData(data, properties, unique, estOverrideMetric, clearObject, segmentopt) → {object}
- Description:
Get calculated totals for each property, usualy used as main dashboard data timeline data without metric segments
- Source:
Example
countlyCommon.getDashboardData(countlySession.getDb(), ["t", "n", "u", "d", "e", "p", "m"], ["u", "p", "m"], {u:"users"}, countlySession.clearObject);
//outputs
{
"t":{"total":980,"prev-total":332,"change":"195.2%","trend":"u"},
"n":{"total":402,"prev-total":255,"change":"57.6%","trend":"u"},
"u":{"total":423,"prev-total":255,"change":"75.7%","trend":"u","isEstimate":false},
"d":{"total":0,"prev-total":0,"change":"NA","trend":"u"},
"e":{"total":980,"prev-total":332,"change":"195.2%","trend":"u"},
"p":{"total":103,"prev-total":29,"change":"255.2%","trend":"u","isEstimate":true},
"m":{"total":86,"prev-total":0,"change":"NA","trend":"u","isEstimate":true}
}
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
data |
object | countly metric model data |
|
properties |
array | array of all properties to extract |
|
unique |
array | array of all properties that are unique from properties array. We need to apply estimation to them |
|
estOverrideMetric |
object | using unique property as key and total_users estimation property as value for all unique metrics that we want to have total user estimation overridden |
|
clearObject |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
|
segment |
string |
<optional> |
segment value for which to fetch metric data |
Returns:
dashboard data object
- Type
- object
(static) getDate(timestamp) → {string}
- Description:
Get date from seconds timestamp
- Source:
Example
//outputs 17.01.2017
countlyCommon.getDate(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number | timestamp in seconds or miliseconds |
Returns:
formated date
- Type
- string
(static) getDateRange() → {string}
- Description:
Getting the date range shown on the dashboard like 1 Aug - 30 Aug, using
dateString property which holds the date format.
- Source:
Returns:
string with formatted date range as 1 Aug - 30 Aug
- Type
- string
(static) getDescendantProp(obj, my_passed_path, def) → {object
- Description:
Fetches nested property values from an obj.
- Source:
Examples
Path found
//outputs {"u":20,"t":20,"n":5}
countlyCommon.getDescendantProp({"2017":{"1":{"2":{"u":20,"t":20,"n":5}}}}, "2017.1.2", {"u":0,"t":0,"n":0});
Path not found
//outputs {"u":0,"t":0,"n":0}
countlyCommon.getDescendantProp({"2016":{"1":{"2":{"u":20,"t":20,"n":5}}}}, "2017.1.2", {"u":0,"t":0,"n":0});
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
object | standard countly metric object |
my_passed_path |
string | dot separate path to fetch from object |
def |
object | stub object to return if nothing is found on provided path |
Returns:
fetched object from provided path
- Type
- object
(static) getMonths(reset) → {array}
- Description:
Get array of localized short month names from moment js
- Source:
Example
//outputs ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
countlyCommon.getMonths();
Parameters:
| Name | Type | Description |
|---|---|---|
reset |
boolean | used to reset months cache when changing locale |
Returns:
array of short localized month names used in moment js MMM formatting
- Type
- array
(static) getOffsetCorrectionForTimestamp(inTS) → {number}
- Description:
Correct timezone offset on the timestamp for current browser's timezone
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
inTS |
number | second or milisecond timestamp |
Returns:
corrected timestamp applying user's timezone offset
- Type
- number
(static) getPercentChange(previous, current) → {object}
- Description:
Calculates the percent change between previous and current values.
- Source:
Example
//outputs {"percent":"100%","trend":"u"}
countlyCommon.getPercentChange(100, 200);
Parameters:
| Name | Type | Description |
|---|---|---|
previous |
number | data for previous period |
current |
number | data for current period |
Returns:
in the following format {"percent": "20%", "trend": "u"}
- Type
- object
(static) getPeriod() → {string|array}
- Description:
Get currently selected period
- Source:
Returns:
supported values are (month, 60days, 30days, 7days, yesterday, hour or [startMiliseconds, endMiliseconds] as [1417730400000,1420149600000])
- Type
- string | array
(static) getPeriodObj() → {object}
- Description:
Getter for period object
- Source:
Returns:
returns countlyCommon.periodObj
- Type
- object
(static) getPeriodRange(period, baseTimeStamp) → {array}
- Description:
Get timestamp range in format as [startTime, endTime] with period and base time
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
period |
object | period has two format: array or string |
baseTimeStamp |
number | base timestamp to calc the period range |
Returns:
period range
- Type
- array
(static) getPersistentSettings() → {object}
- Description:
Get user persistent settings
- Source:
Returns:
settings
- Type
- object
(static) getShortNumber(number) → {string}
- Description:
Shortens the given number by adding K (thousand) or M (million) postfix. K is added only if the number is bigger than 10000, etc.
- Source:
Example
//outputs 10K
countlyCommon.getShortNumber(10000);
Parameters:
| Name | Type | Description |
|---|---|---|
number |
number | number to shorten |
Returns:
shorter representation of number
- Type
- string
(static) getSparklineData(data, props, clearObject) → {object}
- Description:
Get total data for period's each time bucket as comma separated string to generate sparkle/small bar lines
- Source:
Example
var sparkLines = countlyCommon.getSparklineData(countlySession.getDb(), {
"total-sessions": "t",
"new-users": "n",
"total-users": "u",
"total-duration": "d",
"events": "e",
"returning-users": function(tmp_x){return Math.max(tmp_x["u"] - tmp_x["n"], 0);},
"avg-duration-per-session": function(tmp_x){return (tmp_x["t"] == 0) ? 0 : (tmp_x["d"] / tmp_x["t"]);},
"avg-events": function(tmp_x){return (tmp_x["u"] == 0) ? 0 : (tmp_x["e"] / tmp_x["u"]);}
}, countlySession.clearObject);
//outputs
{
"total-sessions":"73,84,80,72,61,18,11,7,17,27,66,39,41,36,39,36,6,11,6,16,22,30,33,34,32,41,29,9,2,2",
"new-users":"24,30,25,20,16,18,11,7,17,18,20,18,17,11,15,15,6,11,6,16,13,14,12,10,7,4,8,9,2,2",
"total-users":"45,54,50,44,37,18,11,7,17,27,36,39,41,36,39,36,6,11,6,16,22,30,33,34,32,29,29,9,2,2",
"total-duration":"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"events":"73,84,80,72,61,18,11,7,17,27,66,39,41,36,39,36,6,11,6,16,22,30,33,34,32,41,29,9,2,2",
"returning-users":"21,24,25,24,21,0,0,0,0,9,16,21,24,25,24,21,0,0,0,0,9,16,21,24,25,25,21,0,0,0",
"avg-duration-per-session":"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"avg-events":"1.6222222222222222,1.5555555555555556,1.6,1.6363636363636365,1.6486486486486487,1,1,1,1,1,1.8333333333333333,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1.4137931034482758,1,1,1,1"
}
Parameters:
| Name | Type | Description |
|---|---|---|
data |
object | countly metric model data |
props |
object | object where key is output property name and value could be string as key from data object or function to create new value based on existing ones |
clearObject |
function | function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs |
Returns:
object with sparkleline data for each property
- Type
- object
(static) getTickObj(bucket, overrideBucket, newChart) → {object}
- Description:
Get Date graph ticks
- Source:
Example
Example output
{
"min":0,
"max":29,
"tickTexts":["22 Dec, Thursday","23 Dec, Friday","24 Dec, Saturday","25 Dec, Sunday","26 Dec, Monday","27 Dec, Tuesday","28 Dec, Wednesday",
"29 Dec, Thursday","30 Dec, Friday","31 Dec, Saturday","1 Jan, Sunday","2 Jan, Monday","3 Jan, Tuesday","4 Jan, Wednesday","5 Jan, Thursday",
"6 Jan, Friday","7 Jan, Saturday","8 Jan, Sunday","9 Jan, Monday","10 Jan, Tuesday","11 Jan, Wednesday","12 Jan, Thursday","13 Jan, Friday",
"14 Jan, Saturday","15 Jan, Sunday","16 Jan, Monday","17 Jan, Tuesday","18 Jan, Wednesday","19 Jan, Thursday","20 Jan, Friday"],
"ticks":[[1,"23 Dec"],[4,"26 Dec"],[7,"29 Dec"],[10,"1 Jan"],[13,"4 Jan"],[16,"7 Jan"],[19,"10 Jan"],[22,"13 Jan"],[25,"16 Jan"],[28,"19 Jan"]]
}
Parameters:
| Name | Type | Description |
|---|---|---|
bucket |
string | time bucket, accepted values, hourly, weekly, monthly |
overrideBucket |
boolean | override existing bucket logic and simply use current date for generating ticks |
newChart |
boolean | new chart implementation |
Returns:
object containing tick texts and ticks to use on time graphs
- Type
- object
(static) getTime(timestamp, showSecondsopt) → {string}
- Description:
Get time from seconds timestamp
- Source:
Example
//outputs 13:54
countlyCommon.getTime(1484654066);
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
timestamp |
number | timestamp in seconds or miliseconds |
||
showSeconds |
boolean |
<optional> |
false
|
used to return seconds |
Returns:
formated time
- Type
- string
(static) localeStringToNumber(localeString) → {number}
- Description:
Formats and returns local string to number
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
localeString |
string | expected value to be formatted |
Returns:
formatted value
- Type
- number
(static) mergeMetricsByName(chartData, metric) → {object}
- Description:
Merge metric data in chartData returned by @{link countlyCommon.extractChartData} or @{link countlyCommon.extractTwoLevelData }, just in case if after data transformation of countly standard metric data model, resulting chartData contains duplicated values, as for example converting null, undefined and unknown values to unknown
- Source:
Examples
Sample input
{"chartData":[
{"metric":"Test","t":71,"u":62,"n":36},
{"metric":"Test1","t":66,"u":60,"n":30},
{"metric":"Test","t":2,"u":3,"n":4}
]}
Sample output
{"chartData":[
{"metric":"Test","t":73,"u":65,"n":40},
{"metric":"Test1","t":66,"u":60,"n":30}
]}
Parameters:
| Name | Type | Description |
|---|---|---|
chartData |
object | chartData returned by @{link countlyCommon.extractChartData} or @{link countlyCommon.extractTwoLevelData } |
metric |
string | metric name to merge |
Returns:
chartData object with same metrics summed up
- Type
- object
(static) numberToLocaleString(value, currencyVal) → {string}
- Description:
Returns a string with a language-sensitive representation of this number.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
value |
string | expected value to be formatted |
currencyVal |
number | expected currency to be formatted |
Returns:
formatted value
- Type
- string
(static) pad(n, width, z) → {string}
- Description:
Pad number with specified character from left to specified length
- Source:
Example
//outputs 0012
countlyCommon.pad(12, 4, "0");
Parameters:
| Name | Type | Description |
|---|---|---|
n |
number | number to pad |
width |
number | pad to what length in symboles |
z |
string | character to pad with, default 0 |
Returns:
padded number
- Type
- string
(static) round(num, digits) → {number}
- Description:
Round to provided number of digits
- Source:
Example
//outputs 1.235
countlyCommon.round(1.2345, 3);
Parameters:
| Name | Type | Description |
|---|---|---|
num |
number | number to round |
digits |
number | amount of digits to round to |
Returns:
rounded number
- Type
- number
(static) safeDivision(dividend, divisor) → {number}
- Description:
Save division, handling division by 0 and rounding up to 2 decimals
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
dividend |
number | object to use |
divisor |
number | path of properties to get |
Returns:
division
- Type
- number
(static) setActiveApp(appId)
- Description:
Change currently selected app by app ID
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
appId |
string | new app ID from @{countlyGlobal.apps} object |
(static) setPeriod(period, timeStamp, noSet)
- Description:
Change currently selected period
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
period |
string | array | new period, supported values are (month, 60days, 30days, 7days, yesterday, hour or [startMiliseconds, endMiliseconds] as [1417730400000,1420149600000]) |
timeStamp |
int | timeStamp for the period based |
noSet |
boolean | if false - updates countly_date |
(static) setPersistentSettings(data)
- Description:
Set user persistent settings to store local storage
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
data |
object | Object param for set new data |
(static) timeString(timespent) → {string}
- Description:
Format duration into highest unit of how much time have passed. Used in big numbers
- Source:
Example
//outputs 2824.7 yrs
countlyCommon.timeString(1484654066);
Parameters:
| Name | Type | Description |
|---|---|---|
timespent |
number | amount in seconds passed since some reference point |
Returns:
formated time with how much highest units passed
- Type
- string
(static) toFirstUpper(str) → {string}
- Description:
Convert string to first letter uppercase and all other letters - lowercase for each word
- Source:
Example
//outputs Hello World
countlyCommon.toFirstUpper("hello world");
Parameters:
| Name | Type | Description |
|---|---|---|
str |
string | string to convert |
Returns:
converted string
- Type
- string
(static) union(x, y) → {array}
- Description:
Joined 2 arrays into one removing all duplicated values
- Source:
Example
//outputs [1,2,3]
countlyCommon.union([1,2],[2,3]);
Parameters:
| Name | Type | Description |
|---|---|---|
x |
array | first array |
y |
array | second array |
Returns:
new array with only unique values from x and y
- Type
- array