Commit 2082b4c9 authored by Sebastian Castro's avatar Sebastian Castro

Import progress bar

parent de38aece
......@@ -34,13 +34,10 @@ class ImportSourceCommand extends GoGoAbstractCommand
return;
}
$this->log('Updating source ' . $import->getSourceName() . ' for project ' . $input->getArgument('dbname') . ' begins...');
$this->log('Downloading the data...');
$importService = $this->getContainer()->get('biopen.element_import');
$dataToImport = $importService->importJson($import, true);
$this->log('Data downloaded. ' . count($dataToImport) . ' elements to import...');
$result = $importService->importData($dataToImport, $import, $this->logger);
$result = $importService->startImport($import);
$this->log($result);
} catch (\Exception $e) {
} catch (\Exception $e) {
$this->error("Source: " . $import->getSourceName() . " - " . $e->getMessage());
}
}
......
......@@ -5,6 +5,7 @@ namespace Biopen\GeoDirectoryBundle\Controller\Admin;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Biopen\GeoDirectoryBundle\Document\ImportState;
class ImportDynamicAdminController extends Controller
{
......@@ -12,12 +13,23 @@ class ImportDynamicAdminController extends Controller
{
$object = $this->admin->getSubject();
$object->setCurrState(ImportState::Started);
$object->setCurrMessage("En attente...");
$em = $this->get('doctrine_mongodb')->getManager();
$em->persist($object);
$em->flush();
$this->get('biopen.async')->callCommand('app:elements:importSource', [$object->getId()]);
$this->addFlash('sonata_flash_success', "Les éléments sont en cours d'importation. Cela peut prendre plusieurs minutes.");
// $result = $this->get('biopen.element_import')->importJson($object);
// $this->addFlash('sonata_flash_success', $result);
// $result = $this->get('biopen.element_import')->importJson($object);
$redirectionUrl = $this->admin->generateUrl('edit', ['id' => $object->getId()]);
$stateUrl = $this->generateUrl('biopen_import_state', ['id' => $object->getId()]);
return $this->redirect($this->admin->generateUrl('edit', ['id' => $object->getId()]));
return $this->render('@BiopenAdmin/pages/import/import-progress.html.twig', [
'import' => $object,
'redirectUrl' => $redirectionUrl,
'stateUrl' => $stateUrl
]);
}
}
\ No newline at end of file
......@@ -25,26 +25,32 @@ use joshtronic\LoremIpsum;
class ImportController extends Controller
{
public function generateRandom($nombre, $generateVote = false)
{
$lastElementCreated = $this->get('biopen.random_creation_service')->generate($nombre, $generateVote);
return new Response('Elements générés');
}
// public function importCsvAction($fileName, $geocode)
// {
// $this->get('biopen.element_import')->importCsv($fileName, $geocode);
// return new Response('Elements importés');
// }
public function availableOptionsAction()
{
$options = $this->get('doctrine_mongodb')->getManager()->getRepository('BiopenGeoDirectoryBundle:Option')->findAll();
$bottomOptions = array_filter($options, function($option) { return $option->getSubcategoriesCount() == 0;});
$optionsNames = array_map(function($option) { return $option->getNameWithParent(); }, $bottomOptions);
return new Response(join('<br>', $optionsNames));
}
public function generateRandom($nombre, $generateVote = false)
{
$lastElementCreated = $this->get('biopen.random_creation_service')->generate($nombre, $generateVote);
return new Response('Elements générés');
}
public function availableOptionsAction()
{
$options = $this->get('doctrine_mongodb')->getManager()->getRepository('BiopenGeoDirectoryBundle:Option')->findAll();
$bottomOptions = array_filter($options, function($option) { return $option->getSubcategoriesCount() == 0;});
$optionsNames = array_map(function($option) { return $option->getNameWithParent(); }, $bottomOptions);
return new Response(join('<br>', $optionsNames));
}
public function currStateAction($id)
{
$em = $this->get('doctrine_mongodb')->getManager();
$import = $em->getRepository('BiopenGeoDirectoryBundle:Import')->find($id);
$responseArray = array(
"state" => $import->getCurrState(),
"message" => $import->getCurrMessage()
);
$response = new Response(json_encode($responseArray));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
......@@ -7,6 +7,15 @@ use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Biopen\CoreBundle\Document\AbstractFile;
abstract class ImportState
{
const Failed = "failed";
const Started = "started";
const Downloading = "downloading";
const InProgress = "in_progress";
const Completed = "completed";
}
/**
* @MongoDB\Document
* @Vich\Uploadable
......@@ -63,7 +72,18 @@ class Import extends AbstractFile
/**
* @MongoDB\ReferenceMany(targetDocument="Biopen\CoreBundle\Document\GoGoLog", cascade={"all"})
*/
private $logs;
private $logs;
/**
*
* @MongoDB\Field(type="string")
*/
private $currState;
/**
* @MongoDB\Field(type="string")
*/
private $currMessage;
public function __construct() {
$this->logs = new \Doctrine\Common\Collections\ArrayCollection();;
......@@ -254,4 +274,48 @@ class Import extends AbstractFile
usort( $logs, function ($a, $b) { return $b->getCreatedAt()->getTimestamp() - $a->getCreatedAt()->getTimestamp(); });
return $logs;
}
/**
* Set currState
*
* @param string $currState
* @return $this
*/
public function setCurrState($currState)
{
$this->currState = $currState;
return $this;
}
/**
* Get currState
*
* @return string $currState
*/
public function getCurrState()
{
return $this->currState;
}
/**
* Set currMessage
*
* @param string $currMessage
* @return $this
*/
public function setCurrMessage($currMessage)
{
$this->currMessage = $currMessage;
return $this;
}
/**
* Get currMessage
*
* @return string $currMessage
*/
public function getCurrMessage()
{
return $this->currMessage;
}
}
......@@ -4,4 +4,10 @@ biopen_element_import_csv:
biopen_element_import_available_options:
path: /available-options
defaults: { _controller: BiopenGeoDirectoryBundle:Import:availableOptions}
\ No newline at end of file
defaults: { _controller: BiopenGeoDirectoryBundle:Import:availableOptions}
biopen_import_state:
path: /import/{id}/state
defaults: { _controller: BiopenGeoDirectoryBundle:Import:currState }
requirements:
methods: GET
\ No newline at end of file
{% extends '@BiopenAdmin/pages/page_layout.html.twig' %}
{% block page_title %}Importation de {{ import.sourceName }}{% endblock %}
{% block page_content %}
<div class="alert alert-default text-center progress-container">
<div class="curr-message">En attente...</div>
<a style="display:none" class="redirect-url" href="{{redirectUrl}}">Retourner à l'import</a>
</div>
<script>
$(document).ready(function() {
function checkCurrState($logId) {
$.ajax({
method: 'get',
url: '{{ stateUrl }}',
success: function(data, status, object) {
$('.curr-message').html(data.message);
var currClass = 'alert-default';
switch(data.state) {
case 'downloading': currClass = 'alert-info'; break;
case 'in_progress': currClass = 'alert-info'; break;
case 'failed': currClass = 'alert-danger'; break;
case 'completed': currClass = 'alert-success'; break;
}
$('.progress-container').removeClass("alert-info").addClass(currClass);
if (data.state == "failed" || data.state == 'completed') {
$('.redirect-url').show();
} else {
setTimeout(checkCurrState, 1000);
}
},
});
}
checkCurrState();
});
</script>
{% endblock %}
......@@ -17,6 +17,7 @@ use Biopen\GeoDirectoryBundle\Document\UserRoles;
use Biopen\GeoDirectoryBundle\Document\PostalAddress;
use Biopen\GeoDirectoryBundle\Document\ElementUrl;
use Biopen\GeoDirectoryBundle\Document\ElementImage;
use Biopen\GeoDirectoryBundle\Document\ImportState;
use Biopen\CoreBundle\Document\GoGoLog;
use Biopen\CoreBundle\Document\GoGoLogType;
......@@ -57,6 +58,10 @@ class ElementImportService
}
public function startImport($import) {
$import->setCurrState(ImportState::Downloading);
$import->setCurrMessage("Téléchargement des données en cours... Veuillez patienter...");
$this->em->persist($import);
$this->em->flush();
if ($import->getUrl()) return $this->importJson($import);
else return $this->importCsv($import);
}
......@@ -154,10 +159,13 @@ class ElementImportService
->getQuery()->execute();
}
$import->setCurrState(ImportState::InProgress);
// processing each data
foreach($data as $row)
{
try {
$import->setCurrMessage("Importation des données " . $i . '/' . $size . ' traitées');
$this->createElementFromArray($row, $import);
$i++;
}
......@@ -178,12 +186,14 @@ class ElementImportService
$this->em->clear();
// After flush, we need to get again the import from the DB to avoid doctrine raising errors
$import = $this->em->getRepository('BiopenGeoDirectoryBundle:ImportDynamic')->find($import->getId());
$this->em->persist($import);
}
}
$this->em->flush();
$this->em->clear();
$import = $this->em->getRepository('BiopenGeoDirectoryBundle:ImportDynamic')->find($import->getId());
$import = $this->em->getRepository('BiopenGeoDirectoryBundle:ImportDynamic')->find($import->getId());
$this->em->persist($import);
$countElemenDeleted = 0;
if ($import->isDynamicImport())
......@@ -229,6 +239,10 @@ class ElementImportService
$log = new GoGoLog($logType, $result);
$log->setSubcontent(implode('</br>', $this->errorsMessages));
$import->addLog($log);
$import->setCurrState($totalErrors > ($size / 4) ? ImportState::Failed : ImportState::Completed);
$import->setCurrMessage($result . '</br></br>' . implode('</br>', $this->errorsMessages));
$this->em->flush();
return $result;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment