1-- Accumulates txBytes and rxBytes per package group, includes session data
2function onConnectivityDataWithSession(published_data, state)
3    log("entering onConnectivityDataWithSession");
4    res = {}
5    -- Save session data which unlike normal fields of published_data is not array type and exists
6    -- for every session
7    -- sessionId: integer that starts from 1 and increases for each new session to uniquely
8    -- identify each session. It's reset to 1 upon reboot.
9    res['sessionId'] = published_data['session.sessionId']
10    -- sessionState: either 1 (STATE_EXIT_DRIVING_SESSION) meaning currently outside a session or
11    -- 2 (STATE_ENTER_DRIVING_SESSION) meaning currently in a session (device is on). For
12    -- connectivity this is always 1 because data is calculated at session end.
13    res['sessionState'] = published_data['session.sessionState']
14    -- createdAtSinceBootMillis: milliseconds since boot
15    res['createdAtSinceBootMillis'] = published_data['session.createdAtSinceBootMillis']
16    -- createdAtMillis: current time in milliseconds unix time
17    res['createdAtMillis'] = published_data['session.createdAtMillis']
18    -- bootReason: last boot reason
19    res['bootReason'] = published_data['session.bootReason']
20    res['bootCount'] = published_data['session.bootCount']
21
22    -- If we don't have metrics data then exit with only sessions data right now
23    if published_data['conn.packages'] == nil then
24        -- on_metrics_report(r) sends r as finished result table
25        -- on_metrics_report(r, s) sends r as finished result table while also sending
26        -- s as intermediate result that will be received next time as 'state' param
27        log("packages is nil, only sessions data available.")
28        on_metrics_report(res)
29        do return end
30    end
31
32    -- Accumulate rxBytes and txBytes for each package group
33    rx = {}
34    tx = {}
35    uids = {}
36    -- Go through the arrays (all same length as packages array) and accumulate rx and tx for each
37    -- package name group. In the packages array an entry can be a conglomeration of multiple package
38    -- names (eg. ["com.example.abc", "com.example.cdf,com.vending.xyz"] the 2nd entry has 2
39    -- package names because it's not distinguishable which made the data transfers)
40    for i, ps in ipairs(published_data['conn.packages']) do
41        if rx[ps] == nil then
42            rx[ps] = 0
43            tx[ps] = 0
44        end
45        -- For each package group accumulate the rx and tx separately
46        rx[ps] = rx[ps] + published_data['conn.rxBytes'][i]
47        tx[ps] = tx[ps] + published_data['conn.txBytes'][i]
48        uids[ps] = published_data['conn.uid'][i]
49    end
50    -- For each package group name combine rx and tx into one string for print
51    for p, v in pairs(rx) do
52        res[p] = 'rx: ' .. rx[p] .. ', tx: ' .. tx[p] .. ', uid: ' .. uids[p]
53    end
54    on_metrics_report(res)
55end
56