-
Notifications
You must be signed in to change notification settings - Fork 981
Changes to run queries(not changestreams) for data processing #6390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4139573
removed debug info
3b91c29
Changes to allow test procesing data points in batches.
7490a2a
Changes to run queries(not changestreams) for data processing
c6b06ee
Merge pull request #6526 from Countly/newarchitecture
Cookiezaurs b9f9dfc
nullcheck for clickhouse require
7d1ee97
fx
592b320
fx
cbf14ec
fallback to mongodb if no adapter defined.
904ce04
fx for tests
cfc7d01
check
894c614
t2
c63fb2d
Merge pull request #6527 from Countly/newarchitecture
Cookiezaurs 2d1002d
update for tests
e87b1e6
Merge branch 'feature/ingestion' of https://github.com/Countly/countl…
200ffc4
fx
67e3a84
fx
eb0fbbd
fx
2641534
Querying event data from granural data
b08abe2
some changes so tests use mongodb in github
633b495
ff
c8530b2
Merge pull request #6539 from Countly/newarchitecture
Cookiezaurs ff65b91
ffx
10ea633
Merge branch 'feature/ingestion' of https://github.com/Countly/countl…
4d865ef
ffxxx
96266ca
Core events tests
007faf0
fixes
cc93bdd
missing fixes
b1ccfce
lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| /** | ||
| * File contains core data aggregators | ||
| */ | ||
|
|
||
|
|
||
| var common = require('../utils/common.js'); | ||
| const { dataBatchReader } = require('../parts/data/dataBatchReader'); | ||
| const plugins = require('../../plugins/pluginManager.js'); | ||
| var usage = require('./usage.js'); | ||
| const log = require('../utils/log.js')('aggregator-core:api'); | ||
| const {WriteBatcher} = require('../parts/data/batcher.js'); | ||
| const {Cacher} = require('../parts/data/cacher.js'); | ||
|
|
||
| //dataviews = require('./parts/data/dataviews.js'); | ||
|
|
||
| var crypto = require('crypto'); | ||
|
|
||
| (function() { | ||
| function determineType(value) { | ||
| var type = "l"; | ||
| if (Array.isArray(value)) { | ||
| type = "a"; | ||
| } | ||
| else if (common.isNumber(value)) { | ||
| if ((value + "").length < 16) { | ||
| if ((value + "").length === 10 || (value + "").length === 13) { | ||
| type = "d"; //timestamp | ||
| } | ||
| else { | ||
| type = "n"; | ||
| } | ||
| } | ||
| } | ||
| return type; | ||
| } | ||
| //Core events data aggregator | ||
| plugins.register("/aggregator", function() { | ||
| new dataBatchReader(common.drillDb, { | ||
| pipeline: [ | ||
| {"$match": {"e": "[CLY]_custom"}}, | ||
| { | ||
| "$group": { | ||
| "_id": { | ||
| "a": "$a", | ||
| "e": "$n", | ||
| "h": {"$dateToString": {"date": {"$toDate": "$ts"}, "format": "%Y:%m:%d:%H", "timezone": "UTC"}}, | ||
| }, | ||
| "c": {"$sum": "$c"}, | ||
| "s": {"$sum": "$s"}, | ||
| "dur": {"$sum": "$dur"} | ||
| } | ||
| }, | ||
| {"$sort": {"a": 1}}, | ||
| { | ||
| "$project": { | ||
| "_id": 0, | ||
| "a": "$_id.a", | ||
| "e": "$_id.e", | ||
| "h": "$_id.h", | ||
| "c": 1, | ||
| "s": 1, | ||
| "dur": 1 | ||
| } | ||
| } | ||
| ], | ||
| "interval": 10000, //10 seconds | ||
| "name": "event-aggregation", | ||
| "collection": "drill_events", | ||
| }, async function(token, results) { | ||
| if (results && results.length > 0) { | ||
| await usage.processEventTotalsFromStream(token, results, common.manualWriteBatcher); | ||
| //Flush collected changes | ||
| await common.manualWriteBatcher.flush("countly", "events_data", token.cd); | ||
| } | ||
| // process next batch of documents | ||
| }); | ||
| }); | ||
|
|
||
| //processes session data and updates in aggregated data | ||
| plugins.register("/aggregator", function() { | ||
| new dataBatchReader(common.drillDb, { | ||
| pipeline: [ | ||
| {"$match": {"e": "[CLY]_session"}}, | ||
| ], | ||
| "name": "session-ingestion" | ||
| }, async function(token, data) { | ||
| if (data.length > 0) { | ||
| for (var k = 0; k < data.length; k++) { | ||
| var currEvent = data[k]; | ||
| if (currEvent && currEvent.a) { | ||
| //Record in session data | ||
| try { | ||
| var app = await common.readBatcher.getOne("apps", common.db.ObjectID(currEvent.a)); | ||
| //record event totals in aggregated data | ||
| if (app) { | ||
| await usage.processSessionFromStream(token, currEvent, {"app_id": currEvent.a, "app": app, "time": common.initTimeObj(app.timezone, currEvent.ts), "appTimezone": (app.timezone || "UTC")}); | ||
| } | ||
| } | ||
| catch (ex) { | ||
| log.e("Error processing session event", ex); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| await common.manualWriteBatcher.flush("countly", "users", token.cd); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| plugins.register("/aggregator", function() { | ||
| var writeBatcher = new WriteBatcher(common.db, true); | ||
| new dataBatchReader(common.drillDb, { | ||
| pipeline: [{"$match": {"e": {"$in": ["[CLY]_session_update"]}}}], | ||
| "name": "session-updates", | ||
| "collection": "drill_events", | ||
| }, async function(token, docs) { | ||
| console.log("Processing session updates " + docs.length); | ||
| if (docs.length > 0) { | ||
| for (var z = 0; z < docs.length; z++) { | ||
| var next = docs[z]; | ||
| if (next && next.a && next.e && next.e === "[CLY]_session_update" && next.ts) { | ||
| try { | ||
| var app = await common.readBatcher.getOne("apps", common.db.ObjectID(next.a)); | ||
| if (app) { | ||
| var dur = 0; | ||
| dur = next.dur || 0; | ||
|
|
||
| await usage.processSessionDurationRange(writeBatcher, token, dur, next.did, {"app_id": next.a, "app": app, "time": common.initTimeObj(app.timezone, next.ts), "appTimezone": (app.timezone || "UTC")}); | ||
| //} | ||
| } | ||
| } | ||
| catch (e) { | ||
| log.e(e); | ||
| return; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| await writeBatcher.flush("countly", "users", token.cd); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| //Drill meta aggregator | ||
| plugins.register("/aggregator", function() { | ||
| var drillMetaCache = new Cacher(common.drillDb); //Used for Apps info | ||
| new dataBatchReader(common.drillDb, { | ||
| pipeline: [ | ||
| { | ||
| "$project": { | ||
| "a": "$a", | ||
| "e": "$e", | ||
| "n": "$n", | ||
| "sg": {"$ifNull": [{"$objectToArray": "$sg"}, [{"k": null, "v": null}]]} | ||
| } | ||
| }, | ||
| {"$unwind": "$sg"}, | ||
| {"$group": {"_id": {"a": "$a", "e": "$e", "n": "$n", "sgk": "$sg.k"}, "sgv": {"$first": "$sg.v"}}}], | ||
| "interval": 10000, ///default update interval | ||
| "name": "drill-meta", | ||
| "collection": "drill_events", | ||
| "onClose": async function(callback) { | ||
| if (callback) { | ||
| callback(); | ||
| } | ||
| }, | ||
| }, async function(token, results) { | ||
| var updates = {}; | ||
| for (var z = 0; z < results.length; z++) { | ||
| if (results[z]._id && results[z]._id.a && results[z]._id.e) { | ||
| if (results[z]._id.e === "[CLY]_custom") { | ||
| results[z]._id.e = results[z]._id.n; | ||
| } | ||
| let event_hash = crypto.createHash("sha1").update(results[z]._id.e + results[z]._id.a).digest("hex"); | ||
| var meta = await drillMetaCache.getOne("drill_meta", {_id: results[z]._id.a + "_meta_" + event_hash}); | ||
| var app_id = results[z]._id.a; | ||
| if ((!meta || !meta._id) && !updates[app_id + "_meta_" + event_hash]) { | ||
| updates[app_id + "_meta_" + event_hash] = { | ||
| _id: app_id + "_meta_" + event_hash, | ||
| app_id: results[z]._id.a, | ||
| e: results[z]._id.e, | ||
| type: "e" | ||
| }; | ||
| meta = { | ||
| _id: app_id + "_meta_" + event_hash, | ||
| app_id: results[z]._id.a, | ||
| e: results[z]._id.e, | ||
| type: "e" | ||
| }; | ||
| } | ||
| if (results[z]._id.sgk) { | ||
| if (!meta.sg || !meta.sg[results[z]._id.sgk]) { | ||
| meta.sg = meta.sg || {}; | ||
| var type = determineType(results[z].sgv); | ||
| meta.sg[results[z]._id.sgk] = { | ||
| type: type | ||
| }; | ||
| updates[app_id + "_meta_" + event_hash] = updates[app_id + "_meta_" + event_hash] || {}; | ||
| updates[app_id + "_meta_" + event_hash]["sg." + results[z]._id.sgk + ".type"] = type; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| //trigger all updates. | ||
| if (Object.keys(updates).length > 0) { | ||
| //bulk operation | ||
| const bulkOps = Object.keys(updates).map(u => { | ||
| return { | ||
| updateOne: { | ||
| filter: {"_id": u}, | ||
| update: {$set: updates[u]}, | ||
| upsert: true | ||
| } | ||
| }; | ||
| }); | ||
| await common.drillDb.collection("drill_meta").bulkWrite(bulkOps); | ||
| } | ||
| // process next document | ||
| }); | ||
| }); | ||
| }()); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.