Skip to content

Commit

Permalink
CalcHandler fintech-fab#12
Browse files Browse the repository at this point in the history
  • Loading branch information
devpull committed Jul 29, 2014
1 parent 378d94b commit 29ccbc6
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 82 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace FintechFab\ActionsCalc\Components;

use FintechFab\ActionsCalc\Models\Event;
use FintechFab\ActionsCalc\Models\Rule;

/**
* Class CalcHandler
*
* @author Ulashev Roman <truetamtam@gmail.com>
*/
class CalcHandler
{
private $_aoRules;
private static $_data;

const CALC_OP_AND = '[AND]';

public function process($aRequestData)
{
$iTerminalId = $aRequestData['terminal_id'];
$sEventSid = $aRequestData['event_sid'];
self::$_data = $aRequestData['data'];

// searching rules
$this->_aoRules = $this->getEventRules($iTerminalId, $sEventSid);

$this->filterRulesWith();
}

/**
* @param int $iTerminalId
* @param string $sEventSid
*
* @return Rule[]
*/
private function getEventRules($iTerminalId, $sEventSid)
{
$oEvent = Event::whereEventSid($sEventSid)->first();

$aoRules = Rule::whereTerminalId($iTerminalId)
->whereEventId($oEvent->id)
->whereFlagActive(true)
->get();

return $aoRules;
}

/**
* @param $sData
*
* @return void
*/
private function filterRulesWith()
{
$oData = json_decode(self::$_data);

$aoFitRules = [];

foreach ($this->_aoRules as $oRule) {
if ($this->isDataFitRule($oRule, $oData)) {
$aoFitRules[] = $oRule;
}
}

// log found rules
if (count($this->_aoRules) > 0) {
$sFoundRules = '';
foreach ($this->_aoRules as $oRule) {
$sFoundRules .= "rule:=> " . $oRule->rule . "\n";
}
\Log::info("Found rules:\n" . $sFoundRules);
}
}

/**
* @param Rule $oRule
* @param $oData
*
* @return bool
*/
private function isDataFitRule($oRule, $oData)
{
if (strpos($oRule->rule, self::CALC_OP_AND)) {
$asRules = explode(self::CALC_OP_AND, $oRule->rule);
dd($asRules);
} else {

}

return true;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function process($aRequestData)
Log::info('Request-data: ' . json_encode($aRequestData));

if ($this->validate($aRequestData)) {
$oCoreHandler = new CoreHandler();
$oCalcHandler = new CalcHandler;
// incoming data should be solid here, by now
return $oCoreHandler->process($aRequestData);
$oCalcHandler->process($aRequestData);
} else {
App::abort(400, 'Bad request');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static function getRequestRules()
{
return [
'terminal_id' => 'required|integer',
'event_sid' => 'required|alpha_dash'
'event_sid' => 'required|alpha_dash',
'data' => 'required'
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Signal extends Eloquent
{
protected $connection = 'ff-actions-calc';
protected $table = 'signals';
protected $fillable = ['id', 'terminal_id', 'signal_sid'];

public function terminal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function up()

Schema::create('signals', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('signal_sid');
$table->integer('terminal_id');
$table->tinyInteger('flag_url');
Expand Down
61 changes: 48 additions & 13 deletions workbench/fintech-fab/actions-calc/tests/TestSetUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,47 @@
use FintechFab\ActionsCalc\Models\Terminal;
use FintechFab\ActionsCalc\Models\Rule;
use FintechFab\ActionsCalc\Models\Event;
use FintechFab\ActionsCalc\Models\Signal;

class TestSetUp extends TestCase
{
protected $aRequestData;

// Set here:
// 1. database seed and truncate
// 2. request data
// 3. client signature
public function setUp()
{
$sSignature = sha1("1|under_rain|key");
$this->aRequestData = [
'terminal_id' => 1,
'event_sid' => 'under_rain',
'data' => json_encode(['test' => 1]),
'data' => json_encode([
'all_wet' => true,
'time' => '15:05',
]),
'auth_sign' => $sSignature
];

parent::setUp();

// Fill in tables on every testg
// Clearing tables on every test
Terminal::truncate();
Rule::truncate();
Event::truncate();
Signal::truncate();

// Terminal user
Terminal::create([
'id' => 1,
'name' => 'Терминал 1',
'key' => 'key',
'password' => Hash::make('password'),
'flag_active' => true,
]);

// Fill in tables on every testg
// Events
Event::create([
'id' => 1,
Expand All @@ -37,35 +55,52 @@ public function setUp()
// Rules
Rule::create([
'id' => 1,
'rule' => 'cold=true[AND]sopli=true[AND]all_wet=true[AND]time!>14:00',
'name' => 'Правило раз',
'rule' => 'cold=true[AND]sopli=true[AND]all_wet=true[AND]time!>=14:00',
'terminal_id' => 1,
'event_id' => 1,
'flag_active' => true,
'signal_id' => 1,
'flag_active' => true,
]);
Rule::create([
'id' => 2,
'name' => 'Правило два',
'rule' => 'sopli=true',
'terminal_id' => 1,
'event_id' => 1,
'flag_active' => true,
'signal_id' => 3,
'flag_active' => true,
]);
Rule::create([
'id' => 3,
'name' => 'Правило три',
'rule' => 'time!<=16:00',
'terminal_id' => 1,
'event_id' => 1,
'flag_active' => true,
'signal_id' => 2,
'flag_active' => true,
]);

// Terminal user
Terminal::create([
'id' => 1,
'name' => 'Терминал 1',
'key' => 'key',
'password' => Hash::make('password'),
'flag_active' => true,
// Signals
Signal::create([
'id' => 1,
'name' => 'Орать мама',
'signal_sid' => 'cry_mommy',
'terminal_id' => 1,
]);
Signal::create([
'id' => 2,
'name' => 'Беги домой',
'signal_sid' => 'run_home',
'terminal_id' => 1,
]);
Signal::create([
'id' => 3,
'name' => 'Просто стой',
'signal_sid' => 'wasted',
'terminal_id' => 1,
]);

}
}

0 comments on commit 29ccbc6

Please sign in to comment.