0
AndrewKarnowski

php help?

Recommended Posts

I really really need some help with a tiny script I'm writing for our dropzone.

We have a wind meter that hooks up to a computer and outputs the data every _minute_ into a 6 column csv file.

I'm trying to graph the data, and it was all working peachy until the data file hit 30,000+ rows of data.

what would be the best way to only grab the last 30 data entries. without any sort of lag?

Here is my current loop:


if ($myFile = @fopen(stripslashes($csvFile), "r")) {
while ($data = fgetcsv($myFile, 100, $delimiter)) {
}
}


Thanks, Andrew

Share this post


Link to post
Share on other sites
Depending upon the O/S, that may have an effect on the technique. Unless you're looking to provide historical data, you don't need a database. What you do need is a way to free up that file from sole access by that program, while keeping the program running in a consistent fashion.
We are all engines of karma

Share this post


Link to post
Share on other sites
How often does your current script execute? Does it automatically run at an interval to update your graph or do you need to do some kind of user input to grab the last 30 entries?

I dont typically like throwing data away, a simple solution to your problem might be to write a small batch file and use MS Task Scheduler to run it every day at 0:00. Have the batch file copy the contents into a new file that has some sort of a date relation, and then purge the csv file that the wind meter writes to. I recon you probably wont be checking winds between 0:00 and 0:30 in the morning? If you will be, schedule it for like 2:00 or something like that. If you used a batch file, you wouldn't have to modify your php script. On the other hand, you could modify your php script to do the same thing. If my math is right (60 minutes in an hour and 24 hours in a day) your csv file will never grow larger than 1440 entries.

And lastly, if keeping a history isn't important, load all the entries from the csv file into an array, pull your data for the graph from the array, then purge the file and write the last 30 entries from the array back to the file.

Maybe that will give you some idears?
________________________________________
I have proof-read this post 500 times, but I guarantee you'll still manage to find a flaw.

Share this post


Link to post
Share on other sites
at the moment, the graph and data are retrieved on the fly. so, when someone opens the page it requests new data and creates a new image.

The only problem with your idea is the fact that the wind meter program has the file open for access continuouslly. I can't delete the file, so i'm under the assumption that i wouldn't be able to write to it either.....

Share this post


Link to post
Share on other sites
Quote

at the moment, the graph and data are retrieved on the fly. so, when someone opens the page it requests new data and creates a new image.

The only problem with your idea is the fact that the wind meter program has the file open for access continuouslly. I can't delete the file, so i'm under the assumption that i wouldn't be able to write to it either.....



I thought about that scenario and figured I'd wait and see what you said. In this case, the batch file would probably work. you can google the command "taskkill" should have a syntax like taskkill /F /IM windMeter.exe

Have the batch file kill the task, do the file copy/purge operations, then start the task again.

I dunno, just thinking aloud. I am sure there are more elegant solutions to the problem.
________________________________________
I have proof-read this post 500 times, but I guarantee you'll still manage to find a flaw.

Share this post


Link to post
Share on other sites
depending how good you are at programing you can download a program that monitors the serial port. (I am assuming it has a serial hook up) and figure out how the data is coming in. Then write a php script that executes every minute to read the data into a file and truncate the file to the length you want it to be. Then you just write a program to read the data file and create your graphs.
~D
Where troubles melt like lemon drops Away above the chimney tops That's where you'll find me.
Swooping is taking one last poke at the bear before escaping it's cave - davelepka

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0