Skip to content

Commit

Permalink
Updating webcam mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnesselr committed Dec 15, 2014
1 parent 2288db6 commit 4fc6359
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 76 deletions.
13 changes: 1 addition & 12 deletions controllers/apiv1.php
Expand Up @@ -576,18 +576,7 @@ public function api_webcamupdate()

//if we have a job, save our new image.
if ($job->isHydrated()) {
$job->set('webcam_image_id', $data_file->id);

$ids = json::decode($job->get('webcam_images'));
if ($ids == NULL) {
$ids = array();
$ids[time()] = $data_file->id;
} else {
$index = time();
$ids->$index = $data_file->id;
}

$job->set('webcam_images', json::encode($ids));
$job->addWebcamImage($data_file->id);
}

//always pull the latest image in for the bot.
Expand Down
79 changes: 15 additions & 64 deletions install/createdb.sql
Expand Up @@ -8,9 +8,6 @@ CREATE TABLE IF NOT EXISTS `activities` (
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `bots` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand Down Expand Up @@ -45,9 +42,6 @@ CREATE TABLE IF NOT EXISTS `bots` (
KEY `slice_config_id` (`slice_config_id`),
KEY `slice_engine_id` (`slice_engine_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -60,9 +54,6 @@ CREATE TABLE IF NOT EXISTS `comments` (
KEY `content_id` (`content_id`),
KEY `content_type` (`content_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `email_queue` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -78,9 +69,6 @@ CREATE TABLE IF NOT EXISTS `email_queue` (
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `error_log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -95,9 +83,6 @@ CREATE TABLE IF NOT EXISTS `error_log` (
KEY `bot_id` (`bot_id`),
KEY `queue_id` (`queue_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `job_clock` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_id` int(11) NOT NULL,
Expand All @@ -115,9 +100,6 @@ CREATE TABLE IF NOT EXISTS `job_clock` (
KEY `queue_id` (`queue_id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `jobs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand Down Expand Up @@ -145,9 +127,6 @@ CREATE TABLE IF NOT EXISTS `jobs` (
KEY `status` (`status`),
KEY `bot_id` (`bot_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `oauth_consumer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`consumer_key` varchar(255) NOT NULL,
Expand All @@ -159,9 +138,6 @@ CREATE TABLE IF NOT EXISTS `oauth_consumer` (
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `oauth_consumer_nonce` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`consumer_id` int(11) unsigned default 0,
Expand All @@ -170,9 +146,6 @@ CREATE TABLE IF NOT EXISTS `oauth_consumer_nonce` (
PRIMARY KEY (`id`),
KEY `consumer_id` (`consumer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `oauth_token` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL,
Expand All @@ -193,9 +166,6 @@ CREATE TABLE IF NOT EXISTS `oauth_token` (
KEY `type` (`type`),
KEY `ip_address` (`ip_address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `queues` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -204,9 +174,6 @@ CREATE TABLE IF NOT EXISTS `queues` (
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `s3_files` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(255) NOT NULL,
Expand All @@ -222,18 +189,12 @@ CREATE TABLE IF NOT EXISTS `s3_files` (
KEY `parent_id` (`parent_id`),
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `shortcodes` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `slice_configs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fork_id` int(11) unsigned NOT NULL,
Expand All @@ -248,9 +209,6 @@ CREATE TABLE IF NOT EXISTS `slice_configs` (
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
KEY `engine_id` (`engine_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `slice_engines` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`engine_name` varchar(255) NOT NULL,
Expand All @@ -264,17 +222,11 @@ CREATE TABLE IF NOT EXISTS `slice_engines` (
KEY `is_featured` (`is_featured`),
KEY `is_public` (`is_public`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `engine_os` (
`engine_id` int(11) unsigned NOT NULL,
`os` enum('osx','linux','win','raspberrypi'),
PRIMARY KEY (`engine_id`, `os`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `slice_jobs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -296,9 +248,6 @@ CREATE TABLE IF NOT EXISTS `slice_jobs` (
KEY `job_id` (`job_id`),
KEY `slice_config_id` (`slice_config_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `tokens` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
Expand All @@ -309,9 +258,6 @@ CREATE TABLE IF NOT EXISTS `tokens` (
KEY `pass_hash` (`hash`),
KEY `expire_date` (`expire_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
Expand All @@ -331,33 +277,38 @@ CREATE TABLE IF NOT EXISTS `users` (
KEY `pass_hash` (`pass_hash`),
KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE IF NOT EXISTS `patches` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`patch_num` int(11) unsigned NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `patch_num` (`patch_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE IF NOT EXISTS `bot_queues` (
`queue_id` int(11) unsigned NOT NULL,
`bot_id` int(11) unsigned NOT NULL,
`priority` int(11) unsigned NOT NULL,
PRIMARY KEY (`queue_id`, `bot_id`, `priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE IF NOT EXISTS `webcam_images` (
`timestamp` datetime NOT NULL,
`image_id` bigint(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL,
`bot_id` int(11) unsigned NULL,
`job_id` int(11) unsigned NULL,
PRIMARY KEY (`timestamp`, `image_id`),
FOREIGN KEY (`image_id`) REFERENCES s3_files(`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
FOREIGN KEY (`bot_id`) REFERENCES bots(`id`) ON DELETE CASCADE,
FOREIGN KEY (`job_id`) REFERENCES jobs(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE VIEW stats AS
SELECT (unix_timestamp(end_date) - unix_timestamp(start_date)) AS seconds,
bot_id, user_id, status, start_date, end_date
FROM job_clock
WHERE status != 'working'
ORDER by seconds DESC;

INSERT INTO patches(patch_num, description) VALUES(16, 'Added user ID constraint');
INSERT INTO patches(patch_num, description) VALUES(18, 'Added webcam images table');

/*!40101 SET character_set_client = @saved_cs_client */;
20 changes: 20 additions & 0 deletions models/Job/job.php
Expand Up @@ -341,4 +341,24 @@ public function complete()
}
}


public function addWebcamImage($file_id)
{
$image = new WebcamImage();
$image->set('timestamp', date("Y-m-d H:i:s"));
$image->set('image_id', $file_id);
$image->set('user_id', $this->getUser()->id);
$image->set('job_id', $this->id);
$bot = $this->getBot();
if ($bot->isHydrated()) {
$image->set('bot_id', $bot->id);
$bot->set('webcam_image_id', $file_id);
$bot->save();
}
$image->save();

$this->set('webcam_image_id', $file_id);
$this->save();
}

}
32 changes: 32 additions & 0 deletions models/webcamimage.php
@@ -0,0 +1,32 @@
<?

/*
This file is part of BotQueue.
BotQueue is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BotQueue is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BotQueue. If not, see <http://www.gnu.org/licenses/>.
*/

class WebcamImage extends Model
{
public function __construct()
{
// todo: Fix this so we can look up things not based on just id
parent::__construct(null, "webcam_images");
}

public function getUser()
{
return new User($this->get('user_id'));
}
}
4 changes: 4 additions & 0 deletions upgrade/patches.php
Expand Up @@ -21,4 +21,8 @@ function finish_patch($patchNumber, $description) {
function patch_exists($patchNumber) {
$patchSQL = "SELECT * from patches where patch_num >= ?";
return (db()->execute($patchSQL, array($patchNumber)) > 0);
}

function patch_log($message) {
print(" > ".$message."\n");
}
64 changes: 64 additions & 0 deletions upgrade/patches/v05p06.php
@@ -0,0 +1,64 @@
<?
include("../../extensions/global.php");
include("../patches.php");

$patchNumber = 18;
start_patch();

if (!patch_exists($patchNumber)) {

$sql = "CREATE TABLE IF NOT EXISTS `webcam_images` (
`timestamp` datetime NOT NULL,
`image_id` bigint(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL,
`bot_id` int(11) unsigned NULL,
`job_id` int(11) unsigned NULL,
PRIMARY KEY (`timestamp`, `image_id`),
FOREIGN KEY (`image_id`) REFERENCES s3_files(`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES users(`id`) ON DELETE CASCADE,
FOREIGN KEY (`bot_id`) REFERENCES bots(`id`) ON DELETE CASCADE,
FOREIGN KEY (`job_id`) REFERENCES jobs(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

db()->execute($sql);

$failCount = 0;

$sql = "SELECT id from jobs";
$jobsCollection = new Collection($sql);
$jobsCollection->bindType('id', 'Job');

$jobs = $jobsCollection->getAll();

foreach ($jobs as $row) {
/** @var Job $job */
$job = $row['Job'];
$images_json = $job->get('webcam_images');
if($job->isHydrated() && $images_json != "") {
$images = json_decode($images_json, true);
foreach ($images as $timestamp => $image_id) {
$file = Storage::get($image_id);
if ($file->isHydrated() && $file->getUser()->isHydrated()) {
$image = new WebcamImage();
$image->set('timestamp', date("Y-m-d H:i:s", $timestamp));
$image->set('image_id', $image_id);
$image->set('user_id', $job->getUser()->id);
$image->set('job_id', $job->id);
$bot = $job->getBot();
if ($bot->isHydrated()) {
$image->set('bot_id', $bot->id);
}
$image->save();
} else {
$failCount++;
}
}
}
}

if($failCount > 0) {
patch_log($failCount . " images no longer exist in the database");
}

finish_patch($patchNumber, "Added webcam images table");
}

0 comments on commit 4fc6359

Please sign in to comment.