1.1 Getting datas - Server-Analytics/stats-page GitHub Wiki
Getting datas
All datas are stored under javascript values, but there are multiple types of datas, each of them having their own structure and specific use:
Data-storage: DATAS_statsDataRange
This data-storage contains every stats Server Analytics have on the server, datas are not sorted. This storage is used by every other data-storers
, so these datas should not be changed or affected. Datas are here "stats groups", also called suivis
. These are datas from a certain period to another. The default stats group
time is 2 hours, which means that every stats group
is a group of all stats within a date and a date + 2 hours.
Note: datas are deleted after 31 days for non-premium servers, so these datas are not global stats!
Example of a stats group Object:
Object {
date: 1598667150752,
readableDate: "Sam. 29 août 2020", // Can change depending on selected language
messages: 15,
voice: 5874
// And more datas...
}
Getting all datas with DATAS_statsDataRange:
const datas = DATAS_statsDataRange;
console.log(datas[0][0]);
// Return the first stats group
console.log(datas[1][0]);
// Return the second stats group
If you want to fetch the number of messages sent in the first stats group, you can use
const datas = DATAS_statsDataRange;
console.log(datas[0][0].messages);
// Output: 15
Note: You should always check if there are enough stats groups before trying to access one.
if(DATAS_statsDataRange.length >= 3) console.log(DATAS_statsDataRange[2]);
Data-storage: DATAS_timeranges
This data-storage contains every DATAS_statsDataRange
stats but sorted by timeranges. This array
contains every time ranges set in the getStatsLayout.php
file.
DATAS_statsDataRange structure:
0: Array(4) [ 86400000, (1) […], "Ces dernières 24 heures", 0 ]
1: Array(4) [ 172800000, (2) […], "Ces dernières 48 heures", 0 ]
2: Array(4) [ 604800000, (4) […], "Ces derniers 7 jours", 0 ]
3: Array(4) [ 2419200000, (4) […], "Ces derniers 28 jours", 0 ]
4: Array(4) [ 2592000000, (4) […], "Ces derniers 31 jours", 0 ]
Position | Name | Example value |
---|---|---|
0 | Timerange in ms | 8000 |
1 | Stats groups | [{ messages: [12, 13, 14] }] |
2 | Time range name | These last 8000ms |
3 | Locked to non-premium | 0 |
The real data-storage is the "stats group" argument in the DATAS_statsDataRange
array. You can access these datas by using DATAS_statsDataRange[timerange.id][1]
.
Timerange Stats Groups structure:
[
// Array containing every stats group with a date corresponding to the timerange
[{
date: 1598667150752,
readableDate: "Sam. 29 août 2020",
messages: 15,
voice: 5874
// And more datas...
}],
[{
date: 1598667850718,
readableDate: "Dim. 30 août 2020",
messages: 18,
voice: 5874
// And more datas...
}],
// etc...
]
Data-storage: DATAS_statsData
STATS_statsData is the easiest way to get and display datas. This data storage contains every data from a selected timerange. Every stat is sorted by key name and is set with an array containing every stats group values corresponding to a certain stat key.
{
"baseTimerange": 2,
"date": [1598667150752, 1598574350752, 1598203150752, 1598388750752],
"readableDate": ["Sam. 29 août 2020", "Ven. 28 août 2020", "Dim. 23 août 2020", "Mar. 25 août 2020"],
"messages": [15, 13, 14, 12],
"voice": [5874, 10547, 387, 1254]
// And other stats...
}
As you can see, it has the same keys that DATAS_statsDataRange
, except for "baseTimerange", and this is where it gets tricky. This data-storage only stores stats for one timerange, which is the one set in the baseTimerange
key. The default baseTimerange is 2
(id of timerange). In order to get datas from another timerange
, you have to use the refreshStatsDatas()
function.
refreshStatsDatas() function
This function refreshes statistics stored in the DATAS_statsDatas
object according to the given timerange:
Argument | Name | Example value | Optional |
---|---|---|---|
0 | Timerange ID | 0 | true |
Usage:
refreshStatsDatas(0);
console.log(DATAS_statsDatas);
// Return stats for the timerange id 0
refreshStatsDatas(3);
console.log(DATAS_statsDatas);
// Return stats for the timerange id 3
refreshStatsDatas();
console.log(DATAS_statsDatas);
// Return stats for the actually selected timerange (id 3 here)
GlobalOptions.selectedTimerange = 1;
refreshStatsDatas();
// Return stats for the timerange id 1
refreshStatsDatas(99);
// Warning » Couldn't refresh datas. Timerange id specified doesn't exist.