2

I'm using NodeJs.

I received constantly request from server. I'm added some variable like createdTime to it and saved to the database. when I sorted data by createdTime in some case It is not reliable, It is Repeated

How can I make differentiate between them ?

  • I do not want to count request.
  • I do not like to change timestamp's format.

var createdTime = new Date().getTime();

Kenan
  • 2,086
  • 1
  • 16
  • 20
Alyreza
  • 91
  • 13
  • 1
    Why not save it as NOW() in the sql query instead of passing it as a parameter. – Niki van Stein Sep 20 '15 at 08:25
  • @BasvanStein Browser client's time , is a local time. he might want to save _that_ time. ( although I think that UTC should be saved) but he might want to use that specific time. – Royi Namir Sep 20 '15 at 08:27
  • Without changing the type to string or other format , you won't be able to do that. Looks like a design flow here. – Royi Namir Sep 20 '15 at 08:29
  • 1
    Really, a simple server-side global counter is a very simple solution here. You can either use the counter by itself or you can combine it with the time. – jfriend00 Sep 20 '15 at 08:29
  • I'm not using SQL like . I'm using ElasticSearch :) – Alyreza Sep 20 '15 at 08:29
  • You can add _another_ column without touching your createdTime column. That new column would have the sequential number value. – Royi Namir Sep 20 '15 at 08:31
  • 3000 request per min increase length of variable very fast. @jfriend00 – Alyreza Sep 20 '15 at 08:33
  • Uhhh, you're complaining because the time doesn't increase fast enough and now you're complaining about a variable that increases by only 1 for each request. Get real here. That's not a valid complaint at all. When you pass a billion transactions in the same ms on the same server, please let me know. A server-side counter is the simplest way to make sure each request has a monotomically increasing id assigned to it. This is a standard server-side technique. Very simple to code. Completely reliable. – jfriend00 Sep 20 '15 at 08:37
  • @jfriend00 **thanks** for clarification. ;) – Alyreza Sep 20 '15 at 08:55
  • w/ decimal datestamp via process.hrtime(); it's unique every time, even in the barest loop. you can parse dates with decimals in node, `new Date(1442740743401.12345)` , so the result works as both a UID and a timestamp in one stored column. – dandavis Sep 20 '15 at 09:19
  • Did any of these answers solve your issue? If so, please mark the best answer as the accepted answer by checking the green checkmark to the left of that answer to indicate to the community that your question has been answered and then both you and the person who provided the answer will earn some reputation points that can lead to more privileges here on StackOverflow. – jfriend00 Sep 27 '15 at 17:49

2 Answers2

3

Here's a method of combining a counter with the current time to allow you to have as many as 1000 separate transactions within the same ms that are all uniquely numbered, but still a time-based value.

And, here's a working snippet to illustrate:

// this guarantees a unique time-based id
// as long as you don't have more than 1000
// requests in the same ms
var getTransactionID = (function() {
  var lastTime, counter = 0;
  return function() {
    var now = Date.now();
    if (now !== lastTime) {
      lastTime = now;
      counter = 0;
    } else {
      ++counter;
    }
    return (now * 1000) + counter;
  }
})();

for (var i = 0; i < 100; i++) {
    document.write(getTransactionID() + "<br>");
}

If you want something that is likely to work across clusters, you can use process.hrtime() to use the high resolution timer instead of the counter and then make the id be a string that could be parsed into a relative time if needed. Since this requires node.js, I can't make a working snippet here in the browser, but here's the idea:

// this makes a unique time-based id
function getTransactionID () {
    var now = Date.now();
    var hrtime = process.hrtime();
    return now + "." + ((hrtime[0] * 1e9) + hrtime[1]);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • It is Repeated @jfriend00 – Alyreza Sep 20 '15 at 09:15
  • one thing to consider in sticking within a ms precision is that a cluster can easily get hit twice in a ms, resulting in a duplicate, whereas adding hrtime makes a repeat much less likely under moderate workloads. – dandavis Sep 20 '15 at 09:28
  • @Alyreza - show me how it could possibly be repeated? Are you seriously getting more than 1000 request per millisecond? Show me a server that can actually do that. If you want a higher number than 1000, then increase the multiple in the code - simple as that, but I rather doubt that's a real world concern. – jfriend00 Sep 20 '15 at 09:35
  • @dandavis - I added a solution that use `process.hrtime()`. – jfriend00 Sep 20 '15 at 09:50
  • @Alyreza - I added another version. Still don't know why the first one wouldn't work for you, but the second one uses a different technique. – jfriend00 Sep 20 '15 at 09:51
  • this is how you can see , it's repeated; created time : [img](http://uploads.im/XV86x.jpg) – Alyreza Sep 20 '15 at 09:55
  • I'm getting Travel Agent Asterisk log in Local. – Alyreza Sep 20 '15 at 09:59
  • 1
    @Alyreza - you''ll have to show the actual code that makes that log in the img. The format you have in that img is not what either of my code snippets produces so I don't know where you get that from. The second form in my answer is not directly sortable without parsing it first, but if you parse it (separate out before and after decimal), then it is sortable or comparable. The issue is that there's too much range for a pure number so you have to go with multiple pieces of a number in a string which just requires a little more code to compare or sort, but is doable. – jfriend00 Sep 20 '15 at 10:18
  • 1
    @Alyreza - FYI, the first code snippet in my answer is a number and is directly comparable or sortable. – jfriend00 Sep 20 '15 at 10:27
1

Due to my low rep I can't add a comment but it looks like you are needing to go beyond milliseconds.Maybe this stackoverflow question can help you

How to get a microtime in Node.js?

Community
  • 1
  • 1
pwilcox83
  • 51
  • 7