Skip to content

Commit

Permalink
use mysql for postprocess list data
Browse files Browse the repository at this point in the history
  • Loading branch information
glynhudson committed Sep 3, 2018
1 parent a11007f commit 2e90cb6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
39 changes: 25 additions & 14 deletions postprocess-module/postprocess_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

function postprocess_controller()
{
global $homedir,$session,$route,$mysqli,$redis,$feed_settings;
global $log,$homedir,$session,$route,$mysqli,$redis,$feed_settings;
if (!isset($homedir)) $homedir = "/home/pi";

$result = false;
$route->format = "text";


include "Modules/postprocess/postprocess_model.php";
$postprocess = new PostProcess($mysqli);

include "Modules/feed/feed_model.php";
$feed = new Feed($mysqli,$redis,$feed_settings);

Expand Down Expand Up @@ -84,14 +88,19 @@ function postprocess_controller()
if ($route->action == "list" && $session['write']) {

$userid = $session['userid'];
$processlist = json_decode($redis->get("postprocesslist:$userid"));
if ($processlist==null) $processlist = array();

$processlistout = array();
$processlist = $postprocess->get($userid);

if ($processlist==null) $processlist = array();
$processlist_long = array();
$processlist_valid = array();

for ($i=0; $i<count($processlist); $i++) {
$valid = true;
$process = $processlist[$i]->process;

$item = json_decode(json_encode($processlist[$i]));

$process = $item->process;
if (isset($processes[$process])) {
foreach ($processes[$process] as $key=>$option)
{
Expand All @@ -105,7 +114,8 @@ function postprocess_controller()
$f['interval'] = $meta->interval;
$f['npoints'] = $meta->npoints;
$f['id'] = (int) $f['id'];
$processlist[$i]->$key = $f;

$item->$key = $f;
} else {
$valid = false;
}
Expand All @@ -115,14 +125,15 @@ function postprocess_controller()
$valid = false;
}

if ($valid) $processlistout[] = $processlist[$i];
if ($valid) {
$processlist_long[] = $item;
$processlist_valid[] = $processlist[$i];
}
}

if (json_encode($processlistout)!=json_encode($processlist)) {
$redis->set("postprocesslist:$userid",json_encode($processlistout));
}
$postprocess->set($userid,$processlist_valid);

$result = $processlistout;
$result = $processlist_long;

$route->format = "json";
}
Expand Down Expand Up @@ -186,13 +197,13 @@ function postprocess_controller()
// If we got this far the input parameters where valid.

$userid = $session['userid'];
$processlist = json_decode($redis->get("postprocesslist:$userid"));
$processlist = $postprocess->get($userid);
if ($processlist==null) $processlist = array();

$params->process = $process;
$processlist[] = $params;

$redis->set("postprocesslist:$userid",json_encode($processlist));
$postprocess->set($userid,$processlist);
$redis->lpush("postprocessqueue",json_encode($params));

// -----------------------------------------------------------------
Expand Down Expand Up @@ -247,7 +258,7 @@ function postprocess_controller()
// If we got this far the input parameters where valid.

$userid = $session['userid'];
$processlist = json_decode($redis->get("postprocesslist:$userid"));
$processlist = $postprocess->get($userid);
if ($processlist==null) $processlist = array();

$params->process = $process;
Expand Down
56 changes: 56 additions & 0 deletions postprocess-module/postprocess_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/

// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');

class PostProcess
{
private $mysqli;

public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}

public function set($userid,$data)
{
$userid = (int) $userid;
// $data = preg_replace('/[^\w\s-.",:#{}\[\]]/','',$data);
$data = json_encode($data);

if ($this->get($userid)===false) {
$stmt = $this->mysqli->prepare("INSERT INTO postprocess ( userid, data ) VALUES (?,?)");
$stmt->bind_param("is", $userid, $data);
if ($stmt->execute()) return true;
} else {
$stmt = $this->mysqli->prepare("UPDATE postprocess SET `data`=? WHERE userid=?");
$stmt->bind_param("si", $data, $userid);
if ($stmt->execute()) return true;
}
return false;
}

public function get($userid)
{
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT data FROM postprocess WHERE `userid`='$userid'");
if ($result->num_rows > 0) {
if ($row = $result->fetch_object()) {
$data = json_decode($row->data);
if (!$data || $data==null) $data = array();
return $data;
}
}
return false;
}
}
6 changes: 6 additions & 0 deletions postprocess-module/postprocess_schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$schema['postprocess'] = array(
'userid' => array('type' => 'int(11)'),
'data' => array('type' => 'text')
);

0 comments on commit 2e90cb6

Please sign in to comment.