2

I'm developing a chat application with appjs that uses node.js as the platform. I'm stucked with detecting when computer is idle (when user is away from it or not using it).

There is os module in node.js and its os.cpus() provides such information for each core:

[ { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 252020,
       nice: 0,
       sys: 30340,
       idle: 1070356870,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 306960,
       nice: 0,
       sys: 26980,
       idle: 1071569080,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 248450,
       nice: 0,
       sys: 21750,
       idle: 1070919370,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 256880,
       nice: 0,
       sys: 19430,
       idle: 1070905480,
       irq: 20 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 511580,
       nice: 20,
       sys: 40900,
       idle: 1070842510,
       irq: 0 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 291660,
       nice: 0,
       sys: 34360,
       idle: 1070888000,
       irq: 10 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 308260,
       nice: 0,
       sys: 55410,
       idle: 1071129970,
       irq: 880 } },
  { model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times:
     { user: 266450,
       nice: 1480,
       sys: 34920,
       idle: 1072572010,
       irq: 30 } } 
]

So does it fit for detecting user idle? As I understand there are two values which I can use: user and idle. The idle value iterates very quickly but the user one iterates in chaotic way. What I'm looking for is to know when user is not moving mouse or typing in ANY application (not only in my app) and after some threshold timeout (for example 60 seconds) of inactivity I need to change his status to 'away' and when he's back change it back 'to online'. Could you please point me to some alghorithm how to do that or even drop some code example for me? Thanks in advance.

Edit. As I know each OS has API to detect user is idle and for example Adobe Air as platform has ability to do that easily and I know I can use node-ffi or even write a module. Furthermore as I know Chromium also has this ability out of the box.

moogeek
  • 397
  • 4
  • 14
  • 34
  • 1
    to help us help you, why would a nodejs app care if the entire computer is idle? What's the endgame? There may be a better way to do what you're trying that you're ignoring because you've rabbit-holed on this particular approach. – jcolebrand Apr 08 '13 at 18:33
  • I would like to simply determine when chat application user is away from computer and not even surfing the web or typing something in MS Word so that I can set his status 'away' and when he's back set his status back to 'online'... – moogeek Apr 09 '13 at 20:19
  • 1
    As a web-app, I would like it to set my status to away if I'm just not using the app on a regular basis. Like how StackExchange chat works, so that when I've been gone from chat for like 15 minutes I show as "away" in the tab itself. – jcolebrand Apr 09 '13 at 22:02
  • Yes, it has the right to be as an additional feature but take a look at any IM client and how it works - it usually sets you away after certain number of minutes automatically and who say I am developing web based app when I take appjs as platform I Try to get the maximum of it. – moogeek Apr 09 '13 at 23:48
  • 1
    So you're going to target a highly select group of individuals to run nodejs locally, or does appjs package and run various versions of node independently? Granted, I've not looked into appjs as I don't see this as a highly functional desktop runtime, but then again, neither Java ;-) Seriously tho, I mean, if you're going to run this as a desktop client then yes, monitor the CPU for low usage, just like `top` does. But you'll have to keep a threshhold for that value, like 4% or something. Shame you can't hook the mouse/kbd driver stack for activity ... – jcolebrand Apr 10 '13 at 00:30
  • Yes primary it will be a special application used by a group of managers within the company working on Windows and I can fully experiment on this. So I can convert these timings for each core to percentage [like so](http://stackoverflow.com/a/9567357/247577) multiply them or take an average value and then if I have <=4% I consider the computer is idle, right? – moogeek Apr 10 '13 at 07:03
  • 1
    well just keep in mind that Outlook may kick off in the background and use CPU, or the screensaver may user CPU, or a network file copy, or etc. – jcolebrand Apr 10 '13 at 15:05
  • 1
    This might help to establish that threshhold: http://stackoverflow.com/questions/15929790/how-specifically-does-one-detect-that-a-user-is-idle-on-windows-7 – jcolebrand Apr 10 '13 at 15:13

1 Answers1

2

According to os.cpus() documentation :

Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).

So the times field shows all the time spent on a cpu core and how it was used since startup. But this is not what you are looking for because the output details CPU usage for system not for appjs window in particular.

Now how to find out your appjs application is active/idle. You should check the appjs page. There are some events like mousemove and keydown which you can use in you application.

  window.addEventListener('keydown', function(e){
  //if last trigger is there remove it and add new trigger
  //else call makeactive()
  });

  window.addEventListener('mousemove', function(e){
  //if last trigger is there remove it and add new trigger
  //else call makeactive()
  });

//To trigger timeout activity do something like this 
setTimeout(function () {
//change icons, set status as away.
}, 60000); // after 60 seconds timeout

//To reset inactivity changes if it is already idle
makeactive(){
//reset icons, set status as active.
}
user568109
  • 47,225
  • 17
  • 99
  • 123
  • Sorry if I did not explain thoroughly. By user idle I've meant global user inactivity not only for appjs window so javascript mouse and keyboard events are not suitable for that. – moogeek Apr 06 '13 at 18:09
  • 1
    CPU idle time is a good measurement. If CPU is idle for a long time it means user is not doing something serious or he is not using the system at all. – Morteza Milani Apr 07 '13 at 05:11
  • 2
    @MortezaM. no it's not really, because there could be conditions where the CPU is idle but the user is using the computer. Think "reading a PDF". – jcolebrand Apr 08 '13 at 18:32
  • 1
    Also there are background services/jobs which take CPU cycles. So how to tell user is idle if background jobs are consuming CPU. You would have to use system commands to find out e.g 'w' in linux – user568109 Apr 09 '13 at 04:37
  • Hey guys thanks for appointment so if this cpu idle datas is good for my task could you please point me to some algorithm how can I determine user is idle from these incrementing values cause honestly I don't understand how this cpu stuff works? – moogeek Apr 09 '13 at 20:25