Last active
September 17, 2015 09:32
-
-
Save xtepwxly/1878d5fd08f8019af6bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\EnviromentalData; | |
use App\Datasets\StatisticsDataset; | |
use App\Services\TransformService; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class DashboardController extends Controller | |
{ | |
public function dashboard() | |
{ | |
return view('pages.dashboard'); | |
} | |
/*public function index() | |
{ | |
$dataset = EnviromentalData::all(); | |
return TransformService::transform($dataset); | |
}*/ | |
/** | |
* Display a listing of the resource. | |
* | |
* @param Request $request | |
* @return string | |
*/ | |
public function show(Request $request) | |
{ | |
$from = $request->input('from'); | |
$to = $request->input('to'); | |
$envData = EnviromentalData::where('data_recorded', '>=', $from) | |
->where('data_recorded', '<=', $to)->get(); | |
return TransformService::transform($envData); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class EnviromentalData extends Model | |
{ | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'enviromental_data'; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = ['data_recorded', 'air_temp', 'bar_press', 'wind_speed']; | |
/** | |
* The attributes excluded from the model's JSON form. | |
* | |
* @var array | |
*/ | |
protected $hidden = ['created_at', 'updated_at']; | |
/** | |
* The Accessor for 'air_temp' attribute | |
* | |
* @param $value | |
* @return array | |
*/ | |
public function getAirTempAttribute($value) | |
{ | |
return explode(',', $value); | |
} | |
/** | |
* The Accessor for 'bar_press' attribute | |
* | |
* @param $value | |
* @return array | |
*/ | |
public function getBarPressAttribute($value) | |
{ | |
return explode(',', $value); | |
} | |
/** | |
* The Accessor for 'wind_speed' attribute | |
* | |
* @param $value | |
* @return array | |
*/ | |
public function getWindSpeedAttribute($value) | |
{ | |
return explode(',', $value); | |
} | |
/** | |
* The Mutator for 'air_temp' attribute | |
* | |
* @param $value | |
*/ | |
public function setAirTempAttribute($value) | |
{ | |
$this->attributes['air_temp'] = implode(',', $value); | |
} | |
/** | |
* The Mutator for 'bar_press' attribute | |
* | |
* @param $value | |
*/ | |
public function setBarPressAttribute($value) | |
{ | |
$this->attributes['bar_press'] = implode(',', $value); | |
} | |
/** | |
* The Mutator for 'wind_speed' attribute | |
* | |
* @param $value | |
*/ | |
public function setWindSpeedAttribute($value) | |
{ | |
$this->attributes['wind_speed'] = implode(',', $value); | |
} | |
/** | |
* Display first date | |
* | |
* @return mixed | |
*/ | |
public static function getFirstDate() | |
{ | |
$rowFirst = EnviromentalData::findOrFail(1); | |
return $rowFirst->data_recorded; | |
} | |
/** | |
* Display last date | |
* | |
* @return mixed | |
*/ | |
public static function getLastDate() | |
{ | |
$rowLast = EnviromentalData::all()->last(); | |
return $rowLast->data_recorded; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ajax call to update the dashboard | |
function loadChart() { | |
var form = $('#dashboard-date'); | |
$.ajax({ | |
url: form.prop('action'), | |
data: form.serialize(), | |
method: form.prop('method'), | |
dataType: 'json' | |
}) | |
//$.getJSON(window.location.href + '/from/' + fromDate + '/to/' + toDate) | |
.done(function (data, textStatus, jqXHR) { | |
if(jqXHR.status === 200 ) { | |
console.log(data); | |
} | |
}) | |
.fail(function (jqXHR, textStatus, errorThrown) { | |
var errorsHtml = '<div class="alert alert-danger"><ul>'; | |
/*if(jqXHR.status === 401 ) { | |
$( location ).prop( 'pathname', 'auth/login' ); | |
var errors = data.responseJSON.msg; | |
errorsHtml = '<div class="alert alert-danger">' + errors + '</div>'; | |
$( '#form-errors' ).html( errorsHtml ); | |
}*/ | |
// Laravel error message | |
if (jqXHR.status === 422 ) { | |
var errors = jqXHR.responseJSON; | |
$.each(errors , function(key, value) { | |
errorsHtml += '<li>' + value[0] + '</li>'; | |
}); | |
} | |
// if not found | |
/*if (jqXHR.status === 404) { | |
console.log(errorThrown); | |
//$('.alert.alert-danger').html(errorThrown); | |
}*/ | |
if (jqXHR.status === 500) | |
{ | |
if ($('#from').val() > $('#to').val()) { | |
errorsHtml += "<li>from date field can't be greater than to</li>"; | |
} | |
else { | |
errorsHtml += "<li>no data for these time intervals</li>"; | |
} | |
} | |
errorsHtml += '</ul></div>'; | |
$('#form-errors').html(errorsHtml); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Datasets; | |
class StatisticsDataset | |
{ | |
private $values; | |
private $valuesLength; | |
private $precision = 2; | |
public function __construct(array $vals) | |
{ | |
$this->values = $vals; | |
sort($this->values); | |
$this->valuesLength = count($vals); | |
} | |
public function getMedian() | |
{ | |
$mid = $this->valuesLength * 0.5; | |
if ($this->valuesLength % 2) | |
{ | |
$median = $this->values[$mid - 0.5]; | |
} | |
else | |
{ | |
$median = round(($this->values[$mid - 1] + $this->values[$mid]) / 2, $this->precision); | |
} | |
return $median; | |
} | |
public function getMean() | |
{ | |
$mean = round(array_sum($this->values) / $this->valuesLength, $this->precision); | |
return $mean; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use App\Datasets\StatisticsDataset; | |
class TransformService | |
{ | |
public static function transform($bigData) | |
{ | |
$airStats = array(); | |
$barStats = array(); | |
$windStats = array(); | |
foreach ($bigData as $row) | |
{ | |
$airStats = array_merge($airStats, $row['air_temp']); | |
$barStats = array_merge($barStats, $row['bar_press']); | |
$windStats = array_merge($windStats, $row['wind_speed']); | |
} | |
$airAvg = new StatisticsDataset($airStats); | |
$barAvg = new StatisticsDataset($barStats); | |
$windAvg = new StatisticsDataset($windStats); | |
$result = array( | |
'air_temp' => array( | |
'mean' => $airAvg->getMean(), | |
'median' => $airAvg->getMedian() | |
), | |
'bar_press' => array( | |
'mean' => $barAvg->getMean(), | |
'median' => $barAvg->getMedian() | |
), | |
'wind_speed' => array( | |
'mean' => $windAvg->getMean(), | |
'median' => $windAvg->getMedian() | |
) | |
); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment