PHP MySQL Charts/Graphs

From: Matt 6 May 2013 21:11
To: ANT_THOMAS 27 of 47
That looks fine. How are you limiting the results when selecting the different scales, are you just assuming there will always be a maximum number of readings a day and adjusting the limit?
From: ANT_THOMAS 6 May 2013 21:20
To: Matt 28 of 47
Yeah maximum plus 20 more to give few extra at the start.

1hour - 80
2 hours - 140
6 hours - 380
12 hours - 740
1 day - 1440
1.5 days - 2180
2 days - 2900
7 days - 10100

It defaults to 2900

To me graphs are about being able to see trends and anything less than 1 day doesn't really tell you much so I might get rid of them when I have a decent amount of data.
EDITED: 6 May 2013 21:21 by ANT_THOMAS
From: koswix 6 May 2013 21:53
To: ANT_THOMAS 29 of 47
  • Good
  1. I'm glad
January) You're right, but it often causes offence. Which is nice.

MMX) put foil over the windows/walls. It'll help stabilse the temperature variance and also reflect back the light from your halogens to reduce your energy consumption. As an added bonus, your weed farm will also show up a bit less on the infra-red camera in the police helicopter.
From: Ken (SHIELDSIT) 6 May 2013 22:13
To: ANT_THOMAS 30 of 47
Soon you will be able to tell us all that it has been the hottest day in the history of hot!
From: af (CAER) 6 May 2013 22:15
To: ANT_THOMAS 31 of 47
FWIW: http://caer.me:2988/server.js

That's the source to the Node app that's serving the monitoring page.

Also, in the time between writing the above paragraph and starting to write this one, I rewrote a big chunk of both the monitoring, server and client-side code. The monitoring code now stores counts etc. in the database (previously it appended to a log file), while the server code now sends the data in a nicer format. The client code has been updated to take that into account, and is now simpler as a result.

Oh and Redis is cool, you should try it. Much more lightweight and straightforward than *SQL.

edit:

oops, just noticed my MIME type thing is a bit stupid.

edit 2:

How come you're not requesting the graph data via AJAX? Seems a simpler way to do it, imo, and less for your server to do.
EDITED: 6 May 2013 22:22 by CAER
From: ANT_THOMAS 6 May 2013 22:28
To: af (CAER) 32 of 47
AJAX...because I don't know how to. I've never knowingly used any AJAX code in anything I've made. Basically all I tend to do when I make something online is tack together the bits of things I already know and I've already made else where and then learn a little bit more to get whatever I want working sufficiently :$
From: af (CAER) 6 May 2013 23:36
To: ANT_THOMAS 33 of 47

It's really simple. First you make a PHP thing to serve the data:

<?php
// this is get_data.php
$time = $_GET['time'];
$dataset1 = magic_function_to_fetch_outdoors_data_from_db($time);
$dataset2 = magic_function_to_fetch_inoors_data_from_db($time);

$data = array(
  "dataset1" => $dataset1,
  "dataset2" => $dataset2
);

echo json_encode($data);
?>

Then you modify your HTML links like so:

<ul id="period">
  <li><a href="get_data.php?time=1h">1 hour</a></li>
  <li><a href="get_data.php?time=2h">2 hour</a></li>
  <li><a href="get_data.php?time=6h">6 hour</a></li>
</ul>

Then you have some JS that looks something like this:

var $chart = $("#placeholder"),
    options = {
        lines: { show: true },
        points: { show: false }
        // etc.
    };

function plotChart(data) {
    $.plot($chart, [
        { data: data.dataset2, color: "#DE1B55", label: "Indoors" },
        { data: data.dataset1, color: "#5F2BCF", label: "Outdoors" }
    ], options);
}

$("#period").on("click", "a", function () {
    var $link = $(this);

    $link.addClass("working");

    $.ajax({
        url: this.href,
        type: "GET",
        dataType: "json"
    }).done(function (data) {
        plotChart(data);
    }).fail(function () {
        alert("O noes!");
    }).always(function () {
        $link.removeClass("working");
    });

    return false; // stops the browser from trying to follow the link
});

And that's basically it.

EDITED: 6 May 2013 23:50 by CAER
From: Drew (X3N0PH0N) 8 May 2013 09:58
To: ALL34 of 47
Not really directly relevant but I saw this today, seems pretty coool: https://plot.ly/plot
From: CHYRON (DSMITHHFX) 8 May 2013 10:54
To: ANT_THOMAS 35 of 47
Might you not speed it up with fewer samples? Every minute seems excessive (maybe you needed that for developing/debugging).
From: CHYRON (DSMITHHFX) 8 May 2013 10:57
To: Drew (X3N0PH0N) 36 of 47
That choked my dual core 2.5g.
From: ANT_THOMAS 8 May 2013 11:29
To: CHYRON (DSMITHHFX) 37 of 47
There isn't really any developing/debugging as such. I've been playing with temperature sensors with my RPis and I wanted to put one outdoors and I decided it would be nice to look at the data in a graph. Every minutes is definitely overkill but it's very quick when I access the data locally, I think it would be slow even if the data was held on the hosting server, especially when it gets to weeks and months worth of data.
From: af (CAER) 8 May 2013 19:22
To: CHYRON (DSMITHHFX) 38 of 47
At that rate, 24 hours' worth is only 60k or so – 1440 data points per series, which is not a huge amount, and like I said earlier, even my phone can handle that with no noticeable slowing.
From: koswix 8 May 2013 23:29
To: Drew (X3N0PH0N) 39 of 47
No drop shadow! :O
From: ANT_THOMAS 9 May 2013 00:46
To: Matt 40 of 47
A MySQL question.

Due to there being missing parts on the chart for the shorter readings because of the missing entries I've changed how the data is pulled from the database by calculating the current time minus the length of time required then only fetching the rows that are greater than this calculated time. It works nicely and gives a proper day, week length etc.

My question is should I be indexing my "unixtime" column to make it easier on the DB? 

New code is...

code: 
$currtime = time();
$newlimit = $currtime - $limit;
SELECT * FROM outside WHERE unixtime > $newlimit ORDER BY id DESC
With $limit now being the length of time in seconds.
edit: I think that looks better.

EDITED: 9 May 2013 00:48 by ANT_THOMAS
From: ANT_THOMAS 9 May 2013 00:47
To: ALL41 of 47
I suck at using the new editor.
From: Matt 9 May 2013 06:57
To: ANT_THOMAS 42 of 47
Yes, an index would be useful there.
From: af (CAER) 9 May 2013 11:07
To: ANT_THOMAS 43 of 47
The editor sucks at being used, more like.
From: ANT_THOMAS 9 May 2013 11:34
To: Matt 44 of 47
Is it a cache issue that is causing no grey box to appear when I want to add code in the new editor? Only "code:" appears I end up having to go in to the HTML and put things between the pre tags but there's divs all over the place.
From: Matt 9 May 2013 13:17
To: ANT_THOMAS 45 of 47
Could be a cache issue, it's showing here on both "Teh Forum" and "Yellow" themes.
From: Drew (X3N0PH0N)14 May 2013 06:24
To: ALL46 of 47
Another slightly irrelevant cool thing: http://ankane.github.io/chartkick/