Skip to content

Instantly share code, notes, and snippets.

@skynet
Created October 25, 2018 07:22
Show Gist options
  • Save skynet/c7b1e44777f2546a98d321bca8030ee9 to your computer and use it in GitHub Desktop.
Save skynet/c7b1e44777f2546a98d321bca8030ee9 to your computer and use it in GitHub Desktop.
<?php
/*
Task: create an application that:
- imports the provided XML files (which contain sector data).
- generate "alerts" based on the ingest data.
- display the generated alerts in a table.
Rules/Notes:
Alerts:
- An alert is defined by the PFDirection data point.
- An alert is generated if the current day's PFDirection is different from the previous day's.
Display:
- the display feature should take a date as a URL parameter and display all the generated alerts for that date
- the feature should display the previous day's PFDirection as well as the current day's PFDirection
*/
use Orchestra\Parser\Xml\Facade as XmlParser;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
$title = "Sector Data";
$html = '';
$valid_date = true;
$current = '';
$previous = '';
// get date from query string
$input = Input::all();
try {
$date = Carbon::createFromFormat('ymd', $input['date']);
$current = $date->format('ymd');
$title .= " for " . $date->toFormattedDateString();
$previous = $date->subDay()->format('ymd');
}
catch (Exception $err) {
$valid_date = false;
}
// check if files exist and parse
$path = storage_path();
$current_file = $path . "/data/us-sectors-{$current}.xml";
$previous_file = $path . "/data/us-sectors-{$previous}.xml";
if ($valid_date && File::exists($current_file)) {
// parse current
$xml = XmlParser::load($current_file);
$map = [
'PriceDate' => ['uses' => 'PriceDate'],
'Region' => ['uses' => 'Region'],
'Sector' => ['uses' => 'Sector[SectorId,Name,BullPC,Current,Prev,Change,PFDirection]'],
];
$data = $xml->parse($map);
// parse previous
if (File::exists($previous_file)) {
$xml = XmlParser::load($previous_file);
$data_previous = $xml->parse($map);
} else {
$title .= ' <mark><span class="warning" style="font-weight: normal">(XML file for previous date not found)</span></mark>';
$data_previous = false;
}
if (! empty($data_previous)) {
// merge data
foreach ($data['Sector'] as $k => $d) {
foreach ($data_previous['Sector'] as $kp => $dp) {
if ($d['SectorId'] == $dp['SectorId']) {
$data['Sector'][$k]['PFDirectionPrevious'] = $dp['PFDirection'];
}
}
}
}
// set alerts
foreach ($data['Sector'] as $k => $d) {
$data['Sector'][$k]['Alert'] = isset($d['PFDirectionPrevious']) ? $d['PFDirection'] != $d['PFDirectionPrevious'] : false;
if (! isset($d['PFDirectionPrevious'])) {
$data['Sector'][$k]['PFDirectionPrevious'] = 'n/a';
}
}
// var_dump($data);
// build table body
foreach ($data['Sector'] as $d) {
$row_class = $d['Alert'] ? 'bg-danger' : '';
$row_class = $d['PFDirection'] == "O" ? $row_class . ' bg-primary' : $row_class . ' bg-success';
$html .= <<<HTML
<tr class="{$row_class}">
<td scope="row">{$d['SectorId']}</td>
<td>{$d['Name']}</td>
<td>{$d['BullPC']}</td>
<td>{$d['Current']}</td>
<td>{$d['Prev']}</td>
<td>{$d['Change']}</td>
<td>{$d['PFDirection']}</td>
<td>{$d['PFDirectionPrevious']}</td>
</tr>
HTML;
}
} else {
$html = <<<HTML
<tr>
<td colspan=7 scope="row"><mark>XML file for this date not found</mark></td>
</tr>
HTML;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment