Skip to content
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

timestamp added #42

Open
wants to merge 1 commit into
base: leveldb
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/implementations/batch/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = class Batch extends Builder.Flow {
if (new.target === Batch) {
throw new Error('Cannot construct Batch instances directly');
}
this._storeOptions = _.pick(options, ["number", "groupBy", "attributes", "timeout", "type", "path"]);
this._storeOptions = _.pick(options, ["number", "groupBy", "attributes", "timeout", "type", "path", "timestamp"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passed time stamp parameter should be treated similar to group by or attribute parameters

this._lambda = lambda;
this._timeout = options.timeout;
this._aggrigator = aggrigator
Expand Down
13 changes: 11 additions & 2 deletions lib/storage/inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class Inmemory {
this._tupels = new Map();
this._events = new Events();
this._buckts = [[]];
this._timestamp = options.timestamp;
this._setTimer();
}

Expand Down Expand Up @@ -104,16 +105,23 @@ module.exports = class Inmemory {
console.error(`can't store store inactive`);
} else {
try {
let time;
if (!!this._timestamp) {
time = obj.data[this._timestamp];
} else {
time = new Date().getTime();
}
const id = this._incriment();
obj['_timestamp'] = time;
this._tupels.set(id, obj);
this._addToBucket(id, obj.data);
this._addToBucket(id, obj.data, time);
} catch (err) {
this._flushBucket(obj.error);
}
}
}

_addToBucket(id, data) {
_addToBucket(id, data, time) {
let viewObj;
if (!!this._attributes) {
viewObj = _.pick(data, this._attributes);
Expand All @@ -122,6 +130,7 @@ module.exports = class Inmemory {
}
viewObj['id'] = id;
viewObj['groups'] = this._groupBy;
viewObj['_timestamp'] = time;
this._currentBucket().push(viewObj);
if (this._currentBucket().length >= this._maxBucketSize) {
this._moveBucket();
Expand Down
13 changes: 11 additions & 2 deletions lib/storage/leveldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = class LevelDb {
this._timerRef;
this._active = true;
this._error = null;
this._timestamp = options.timestamp;
this.db = level(getDbPath(this._path), leveDBoptions);
if (this._maxBucketSize > maxRecordSize / 2) {
throw `Bucket size cannot be larger than ${maxRecordSize / 2}`;
Expand Down Expand Up @@ -136,16 +137,23 @@ module.exports = class LevelDb {
console.error(`can't store store inactive`);
} else {
try {
let time;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

voluntarily capture both options.timesamp and message arrival timestamp and save them in the bucket.

if (!!this._timestamp) {
time = obj.data[this._timestamp];
} else {
time = new Date().getTime();
}
const id = this._incriment();
obj['_timestamp'] = time;
this.db.put(id, obj);
this._addToBucket(id, obj.data); // promise bnana hai kya
this._addToBucket(id, obj.data, time); // promise bnana hai kya
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the comment

} catch (err) {
this._flushBucket(error);
}
}
}

_addToBucket(id, data) {
_addToBucket(id, data, time) {
let viewObj;
let moveBucket;
let bucktRefs;
Expand All @@ -157,6 +165,7 @@ module.exports = class LevelDb {
}
viewObj['id'] = id;
viewObj['groups'] = this._groupBy;
viewObj['_timestamp'] = time;
this._currentBucket()
.then(buckt => {
bucktId = buckt;
Expand Down
58 changes: 41 additions & 17 deletions sample/basic-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,47 @@ const countries = ['japan', 'barmuda'];
// .sink(Kafka.producer({ topic: 'log' }));

// pipeline.source(Stream.consumer({ name: 'process' }))
// .flow((data, err, next) => {
// let num = parseInt(data.num);
// Object.assign(data, { num: num + 1 , from : countries[Math.floor(Math.random() * country.length)]});
// Object.assign(data, { to : countries[Math.floor(Math.random() * country.length)]});
// // throw new Error('Kaka punjabi');
// next(data, err);
// })
// .flow(Batch.reduce({ number: 5, timeout: 30000, groupBy: "from", attributes: ["num", "to"]},
// (aggtr ,data) => {
// .flow((data, err, next) => {
// let num = parseInt(data.num);
// aggtr.number += num;
// return aggtr;
// }, { number:0}))
// .sink((data, err, next) => {
// console.log("\n\n Reduced: \n", JSON.stringify(data, null, 3));
// next(data, err);
// });
// Object.assign(data, { num: num + 1, from: countries[Math.floor(Math.random() * country.length)] });
// Object.assign(data, { to: countries[Math.floor(Math.random() * country.length)] });
// // throw new Error('Kaka punjabi');
// next(data, err);
// })
// .flow(Batch.reduce({ timeout: 5000, groupBy: "from", type: "leveldb", path: "/Users/vaibhav/workspace/xbl/willa", attributes: ["num", "to"] },
// (aggtr, data) => {
// let num = parseInt(data.num);
// aggtr.number += num;
// return aggtr;
// }, { number: 0 }))
// .sink((data, err, next) => {
// console.log("\n\n Reduced: \n", JSON.stringify(data, null, 3));
// next(data, err);
// });


// pipeline.source(Stream.consumer({ name: 'process-mapper' }))
// .flow((data, err, next) => {
// let num = parseInt(data.num);
// Object.assign(data, { num: num + 1, from: country[Math.floor(Math.random() * country.length)] });
// Object.assign(data, { to: country[Math.floor(Math.random() * country.length)] });
// // throw new Error('Kaka punjabi');
// next(data, err);
// })
// .flow(Batch.map({ number: 5, timeout: 30000, groupBy: ["to", "from"], attributes: ["num", "from"], type: "leveldb" },
// (data) => {
// return { "origination": data.from, "volume": data.num };
// })
// )
// .sink(async (data, err, next) => {
// console.log("\n\n Mapped: \n", JSON.stringify(data, null, 3));
// data.data.forEach(async element => {
// for await (const i of element.argdata) {
// console.log(i)
// }
// });
// next(data, err);
// });

pipeline.source(Stream.consumer({ name: 'process-mapper' }))
.flow((data, err, next) => {
Expand All @@ -52,7 +75,7 @@ pipeline.source(Stream.consumer({ name: 'process-mapper' }))
// throw new Error('Kaka punjabi');
next(data, err);
})
.flow(Batch.map({ number: 5, timeout: 30000, groupBy: ["to", "from"], attributes: ["num", "from"], type: "leveldb" },
.flow(Batch.map({ number: 5, timeout: 30000, groupBy: ["to", "from"], attributes: ["num", "from" , "_timestamp"], timestamp : "time" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user will not be aware of _timestamp.
you should programmatically return two parameters in the result (similar to our group by parameters)

  1. the field mention in as timestamp
  2. time of message Arrival

(data) => {
return { "origination": data.from, "volume": data.num };
})
Expand All @@ -67,6 +90,7 @@ pipeline.source(Stream.consumer({ name: 'process-mapper' }))
next(data, err);
});


// pipeline.sourceCommitable(Kafka.consumer({ topic: 'log' }))
// .flow((data, err, next) => {
// next(data, err);
Expand Down