Author Topic: A better solution for KittyCam  (Read 887 times)

Valiant

  • aka Sam
    • Radiance Audio
A better solution for KittyCam
« on: 23 March, 2012, 07:20:27 pm »
So my camera is an EasyN F Series IP camera thing. At the moment it ftps an image every 5 seconds to my webhosts which then executes the following code
Code: [Select]
<meta http-equiv="refresh" content="5" >
<?php

$dir 
'/home/selfhar1/public_html/kittycam/pics/';
$base_url 'http://www.selfharmony.co.uk/kittycam/pics/';
$newest_mtime 0;
$show_file 'BROKEN';
if (
$handle opendir($dir)) {
while (
false !== ($file readdir($handle))) {
    if ((
$file != '.') && ($file != '..')) {
       
$mtime filemtime("$dir/$file");
       if (
$mtime $newest_mtime) {
          
$newest_mtime $mtime;
          
$show_file "$base_url/$file";
       }
    }
  }
}
print 
'<img src="' .$show_file'" alt="Loading KittyCam!">'

?>

Trouble is with the popularity of the page the hosts servers are being a bit overloaded apparently.

So is there any way to make that less resource hungry?
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

iddu

  • Are we there yet?
Re: A better solution for KittyCam
« Reply #1 on: 23 March, 2012, 11:26:26 pm »
1) Does the (emitted) filename have time context in naming convention (e.g. YYYYMMDDhhmmss.img)
    Use ScanDir to get directory contents in one hit, sort, and pick top/bottom entry....

2) Once you've (slowly) walked the set and found latest, delete/move the prior stuff to sub-folder - don't leave it hanging around for next dir scan

[edit]
3) Kludge, and may not be permitted by your hosting provider:
    exec( ) an OpSys call to return sorted directory, redirecting output to temp file, open temp file, take 1st line...
I'd offer you some moral support - but I have questionable morals.

Valiant

  • aka Sam
    • Radiance Audio
Re: A better solution for KittyCam
« Reply #2 on: 23 March, 2012, 11:54:42 pm »
Files are based on: Cam ID_yearmonthdatehourminutesecond.jpg
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

iddu

  • Are we there yet?
Re: A better solution for KittyCam
« Reply #3 on: 24 March, 2012, 12:21:00 am »
Go for #1 then
I'd offer you some moral support - but I have questionable morals.

Valiant

  • aka Sam
    • Radiance Audio
Re: A better solution for KittyCam
« Reply #4 on: 24 March, 2012, 12:51:03 am »
If the directory is updating every 5 secs then surely it's the same thing?
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

iddu

  • Are we there yet?
Re: A better solution for KittyCam
« Reply #5 on: 24 March, 2012, 10:04:01 pm »
If the directory is updating every 5 secs then surely it's the same thing?
Yes and No

The code as stands is calling off single details entry by entry in code and working out if it's the latest timestamped file from the directory time attributes, so its making two calls to the underlying OS, (i) to get the name (ii) to get the OS timestamp. There is no indication that the OS will perform in any particular manner with regard to whether it goes and reads the filesystem content of the HD for each call, or finds the information in cached memory, and you have to walk the entire set of files each time to manually determine the 'latest' entry.  Since there is the possibility under heavy load that new file is injected while you are walking current list (because you're taking > 5s to do so), and the specs for ReadDir indicate that the 'next file (details) according to [underlying] directory order' is delivered, you may actually miss the later posting(s), until such time as the loop execution drops below 5s

Collecting the entire directory contents in one hit may/should be a faster operation (from the OS perspective). Once you've been given the entire set, the sort call and top/tail selection should be memory based. Since you intimate that the filename has embedded timestamp information, there is no need to query the filesystem for the OS timestamp - derive the 'latest' from the filename ordering returned by the sort.

Either way, unless you're doing code/other stuff elsewhere that's not shown , the fundemental issue that you need to shift the old stuff. You're posting images at 12 per minute/720 hour/17,280 day/120960 week/483840 ~'month' - with the best will in the world, no servers gonna like walking 1/2 a million plus entries in single folder every 5 seconds.

You only need the 'latest' to show, + 1 prior at maximum...anything over 10+s old gets shifted. Move it to year\month\day filesystem structure, giving you the ability to quickly select and ditch whole period content. If people want to see old images, implement display from year\month\day folder based on datepicker selection...

Best. P.
I'd offer you some moral support - but I have questionable morals.

David Martin

  • Thats Dr Oi You thankyouverymuch
Re: A better solution for KittyCam
« Reply #6 on: 24 March, 2012, 10:14:10 pm »
When you upload the image, why don't you then copy it to 'kittypic.jpg'? Stuff the directory walking and use a fixed filename.

"By creating we think. By living we learn" - Patrick Geddes

iddu

  • Are we there yet?
Re: A better solution for KittyCam
« Reply #7 on: 24 March, 2012, 10:26:24 pm »
"Bloody hell," said Majikthise, "now that is what I call thinking. Here Vroomfondel, why do we never think of things like that?"
 :facepalm:
I'd offer you some moral support - but I have questionable morals.