Query and Display Results Every 'X' amount of Minutes from Oracle Database - php

Is there a way that I can query an Oracle 10g database, and display the results in a dynamically refreshed html file every 3 minutes, for example?
Here is my predicament: I have several queries that I would LOVE to display the results of to a whole organization on a basic HTML web page with some CSS. The problem is that I do NOT want a user to be able to constantly refresh a page in his/her browser, and thus severely bog down the database. I have no problem writing the queries, or writing the HTML and CSS needed to display the tables. It's almost as if I would like to query, export results to XML every 3 minutes, and constantly have an HTML or PHP file that is pointing to the dynamically updated XML file. I am open to other options as well...
I have basic user access with the Oracle DB...nothing Admin like. I do have access to a server, though, and have experience with PHP, PL/SQL, and HTML. Perhaps I would have to get into a lower level programming language like Python? I am kind of stuck here. Any kind of help would be appreciated!

you can also execute an Ajax Request every 3 minutes using the setTimeout() function.
Using jQuery framework
$(document).ready(function() {
setTimeout("getFeed()", 180000); //180000 = 3 minutes in milliseconds
});
function getFeed() {
//ajaxRequest here
}
For more info on ajax you can go here: http://api.jquery.com/jQuery.ajax/

Setup a materialized view(mv), point your app to this mv, and then setup a scheduler job to refresh it on whatever frequency you like.
See dbms_scheduler for setting up scheduler jobs in Oracle.
One note: you may want to do an atomic_refresh=>true to do deletes/inserts into the mv instead of truncate/insert (if atomic_refresh=>false, there will be 0 rows in mv until refresh is completed).
An simple example mv creation:
create materialized view MY_MV
tablespace MY_TS
build immediate
refresh complete on demand
with primary key
as
SELECT a.foo, b.bar
from table_a a, table_b b
where a.col1 = b.col2
and a.baz='BLAH'
;
An example refresh call:
dbms_mview.refresh('MY_MV', 'C', atomic_refresh=>true);

Related

Executing function to alter database every 15 minutes

I am currently working on a project that runs online tournaments. Normally admins of the site will generate brackets when it is time for the tournaments to start, but we have run into inconsistent start times, etc. and i am looking into proper ways to automate this process.
I have looked into running cronjobs every x min to check if a bracket needs to be generated but i am worried about issue when it comes to overlapping cronjobs, having to create/manage cronjobs through cpanel etc.
I was thinking about other solutions and thought it would be great if a user could load a page, the backend checks timestamps and determines if the bracket should be generated. An event is then fired/set to begin the auto-generation process elsewhere so it does not impact user load times. I just do not know the best route of going about this.
PS: I just need an idea of the direction i should be looking into so i can learn how to solve this issue i am not looking to copy and paste code. I just haven't been able to find anything. All of my search results provide cronjob examples.
EDIT
After thinking about things could using this work?
$(document).ready(function() {
$.ajax('Full Url Path Here');
})
I don't need to pass user input, or return any data i simply need a way to fire an event, it would be easy to include this only when needed via a helper class. Also i won't necessarily have to worry about users attempting to access i can restrict the route to ajax only requests and since nothing is needed/used on input or returned as output what can happen?
You could do it everytime a user loads a page (idea not tested, but theoretically possible):
1) Create a file and store the timestamp of the last time you updated the database.
2) Everytime a user loads a page, read that timestamp and check if 15 minutes passed.
3) If 15 minutes passed: Run a background script (with shell_exec?) that will do what you want and update the timestamp when it's done executing.
One obvious flaw with this system is that if you have no visitors in let's say a 30 minute frame, you will miss 2 updates. Though I guess that if you have no visitors you also have no point in generating brackets?

PHP DRUPAL: How to detect changes to table in database

Current situation: I have a web page that uses AJAX/JQUERY to refresh all the content on the page every 17 seconds. Every time this happens the server queries the database for data from two tables, one of which is large (450MiB in size, 11 columns) and processes all the data.
This is too resource intensive. I want:
The server queries the database only when one of the two tables have changed.
The page then reloads the page through AJAX only when the tables have been updated and the server has re-processed the data.
I think this falls under the category of comet programming. I'm not sure.
2 is easy. The webpage calls 'update.php' every 17 (or maybe less) seconds. The PHP script returns no data if no changes have been made. Only if data is returned then the current page is replaced with the new data. Please advise me if there is a better way.
As for 1 my googling tells that every time one of my two tables is updated I can put a notification in a table (or maybe just a single byte in a file) to indicate that I must query the database again and then the next time that the webpage sends an AJAX request I return the data.
The problem is that I'm working with a rather large code base I didn't write and I don't know of all the places that either of the two tables may be updated. Is there an easier way to check when the database is modified.
I'm working with PHP, Apache, Drupal and MYSQL.
You can chekout Server Sent Events
A server-sent event is when a web page automatically gets updates from a server.
there is an excellent article on HTML5rocks.com - Server Sent Events
All You have to do is create an object
var source = new EventSource('xyz.php'); //Your php files which will return the updates.
Once you create an object,you can listen to the events
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);

Data-aware PHP components?

Many desktop applications (e.g, those built with Delphi) use "Database aware components", such as a grid, which display the contents of a database - usually the result of a query - and auto-update their display when the database contents change.
Does such a thing exist for PHP (maybe displaying the result of a query in an HTML table, and updating)?
If not, how would we go about creating it?
(Note: This seemingly similar question didn't help me)
It's not technically possible to update a HTML page once rendered with pure PHP because of the static nature of the HTTP protocol so any solution would have to include JavaScript and AJAX calls.
Emulating using AJAX to re-render the table every 5 minutes
It wouldn't be hard to emulate though, just make a PHP page which gets the results of a database and puts them in a table, then use jQuery's .load() function to get that table and render it in a DIV of your choice on an interval of 5 seconds or whatever.
Something like:
function updateTable(){
$('#tableDiv').load(url);
}
var url = 'renderTable.php';
setInterval(updateTable,5000);
You can put this in any PHP (or HTML) page with a DIV with id tableDiv and it will render the output of renderTable.php in that div every 5 seconds without refreshing.
Actually monitoring the database
This is possible, you'd have to set up a PHP file on a cron for every 5 seconds (or an AJAX call every 5 seconds) to run something like SELECT MD5(CONCAT(rows,'_',modified)) AS checksum FROM information_schema.innodb_table_stats WHERE table_schema='db' AND table_name='some_table'; (assuming innoDB), you'd then compare this to the previous checksum.
If the checksums are identical, you could pass 'false' to your modified AJAX call, which would tell it not to render anything over the table. If they aren't, you could pass it the HTML of the new table to render in place.
it could be done but with mix of different technologies
if I would like to monitor in real time changes made in database I would think about triggers and sockets - trigger in database should call (on insert or update) function that will add event to queue - here's example function for postgresql (plsh is custom handler)
CREATE FUNCTION propagate_event(tablename) RETURNS text AS '
#!/bin/sh
# execute script that will add event to message queue
/var/scripts/change_in_table.sh "$1"
' LANGUAGE plsh;
client connects to socket and receives that events in real time

Run a SQL query (count) every 30 sec, and then save the output to some file

I am developing a website and got a database where people can insert data (votes). I want to keep a counter in the header like "x" votes have been cast. But it is possible that there will be a lot of traffic on the website soon. Now I can do it with the query
SELECT COUNT(*) FROM `tblvotes
and then display number in the header, but then every time the users changes page, it will redo the query, so I am thinking, maybe it is better to the query once every 30 sec (so much less load on the mysql server) but then I need to save the output of it to some place (this shouldn't be so hard; I can write it to a textfile?) But how can I let my website automatically every 30 sec run the query and put the number in the file. I got no SSH to the server so I can t crontab it?
If there is something you might not understand feel free to ask!
Simplest approach: Write the result into a local textfile, check the filetime of the textfile on every request to be less than now() + 30 seconds, and if so, update the file. To update, you should lock the file. While the file is being updated, other users for whom the condition now() + 30 is met should only read the currently existing file to avoid race conditions.
Hope that helps,
Stefan
Crontabs can only run every minute, at its fastest.
I think there is a better solution to this. You should make an aggregate table in which the statistical information is stored.
With a trigger on the votes_table, you can do 'something' every time the table receives a INSERT statement.
The aggregate table will then store the most accurate information, which you then can query to display the count.
Better solution will be using some cache mechanism (e.g. APC) instead of files if your server allows it.
If you can, you may want to look into using memcached. It allows you to set an expiry time for any data you add to it.
When you first do the query, you write the md5 of the query text associated with the result. On subsequent queries, look for the data in memcached. If it is expired, you can redo the sql query and then rewrite it to memcached.
Okay, so the first part of your question is basically about caching the result of the total votes to be included in the header of your page. Its a very good idea - here is an idea of how to implement it...
If you can not enable a crontab (even with out SSH access you might be able to set this up using your hostings control panel), you might be able to get away with using an external 3rd party cronjob service. (Google has many results for this)...
Everytime your cronjob runs, you can create/update a file that simply contains some PHP arrays -
$fileOutput = "<"."?php\n\n";
$fileOutput .= '$'.$arrayName.'=';
$fileOutput .= var_export($yourData,true);
$fileOutput .= ";\n\n?".">";
$handle = fopen(_path_to_cache_file,'w+');
fwrite($handle,$fileOutput);
fclose($handle);
That will give you a PHP file that you can simply include() into your header markup and then you'll have access to the $yourData variable under the name $arrayName.

PHP: Making a simple screen transition Dashboard

im planning to make a Simple Screen transition Dashboard. I am going to make several stored procedures that returns columns to be displayed between transitions.
My question is what would be the best way to do this in PHP, since this is the only accessible language to be used with MSSQL and that doesnt need license.
I was thinking to make a hidden Pane(Frame) that handles the delay and loop that loads the Main Page. But im worried that between transition it will take time to Load and might not be a good presentation.
Any tips?
Sorry.. Simple Screen Transition..
Ex. First page, shows columns (customer, number of transactions, total sales)
Second Page, Pie Chart of Last Month vs This Month
Third Page, shows columns (seller, number of transactions, total costs )
Fourth Page, shows the Bargraph of Costs for this years months vs last years months
and so on.. like 10 of like this.
I was adviced on making like an advertisement of sort. Like an Infinite Loop of Power Point presentation that does real time query as the Data is updated every minute.
Hope I did some clarifications, if it didnt please let me know.
FYI. The client PC that will handle this will only have the local webserver ( i will use xampp) and will connect to MSSQL thru network, they wont have access to the internet for security reasons that I dont know.
Let your php print them as normal html (nothing special there).
For your panels, have a page with div containers and use AJAX to load your php intermittently (JQuery and JQuery UI would be the easiest. Make a JavaScript function that uses JQuery delay() and then calls itself to set up a timer that loads the pages into the div containers using $.ajax()

Categories