Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 70a3df53

Přidáno uživatelem Jakub Vašta před asi 4 roky(ů)

+ Re #8086
+ php refactoring and comments

Zobrazit rozdíly:

website/public/js/zcu-heatmap.js
17 17
var isAnimationRunning = false
18 18
var data = []
19 19

  
20
var info = []
21
var currenInfo = 0
22

  
20 23
// holds all instances of markers for bind/unbind popup purpose
21 24
// contains: {key:[L.circle,L.pupup]}
22 25
// key: x and y, x + '' + y string
......
51 54
  ${genPopUpControls([previousButton, posInfo, nextButton])}
52 55
  `
53 56
}
57
/**
58
 * Initialize leaflet map on start position which can be default or set based on user action
59
 */
54 60
// eslint-disable-next-line no-unused-vars
55 61
function initMap () {
56 62
  startX = localStorage.getItem('lat') || startX
......
67 73
  mymap.on('click', showInfo)
68 74
}
69 75

  
70
var info = []
71
var currenInfo = 0
72 76

  
73 77
function showInfo (e) {
74 78
  info = []
......
137 141
  mymap.setView([latitude, longitude], zoom)
138 142
}
139 143

  
144
/**
145
 * Change animation start from playing to stopped or the other way round
146
 */
140 147
// eslint-disable-next-line no-unused-vars
141 148
function changeAnimationState () {
142 149
  isAnimationRunning = !isAnimationRunning
......
173 180
  changeUrl()
174 181
}
175 182

  
183
/**
184
 * Change browser url based on animation step
185
 */
176 186
function changeUrl () {
177 187
  window.history.pushState(
178 188
    '',
......
190 200
  $('#timeline').attr('class', 'time hour-' + currentTime)
191 201
}
192 202

  
203
/**
204
 * Load and display heatmap layer for current data
205
 * @param {string} opendataRoute route to dataset source
206
 * @param {string} positionsRoute  route to dataset postitions source
207
 */
193 208
// eslint-disable-next-line no-unused-vars
194 209
function loadCurrentTimeHeatmap (opendataRoute, positionsRoute) {
195 210
  dataSourceRoute = opendataRoute
......
292 307
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
293 308
}
294 309

  
310
/**
311
 * Checks dataset availibility
312
 * @param {string} route authority for datasets availibility checks 
313
 */
295 314
// eslint-disable-next-line no-unused-vars
296 315
function checkDataSetsAvailability (route) {
297 316
  $.ajax({
website/src/Controller/HeatmapController.php
11 11

  
12 12
class HeatmapController extends AbstractController {
13 13
    /**
14
     * Map page controller.
15
     *
16
     * @param IOpenDataManager $manager connection to database autowired
17
     *
14 18
     * @Route("/heatmap", name="heatmap")
15 19
     */
16 20
    public function index(Request $request, IOpenDataManager $manager) {
......
22 26
        $isSubmitted = $form->isSubmitted();
23 27
        if ($isSubmitted) {
24 28
            $dataSet = $form->getData();
29
            // Chceck whether the data is valid i.e. such a collection exists
25 30
            if (false == $manager->isCollectionAvailable($dataSet->getType(), $dataSet->getDate())) {
31
                // Not? Populate form with empty data
26 32
                $dataSet = new DataSet();
27 33
            }
28 34
            $form = $this->createForm(DataSetType::class, $dataSet);
......
39 45
    }
40 46

  
41 47
    /**
48
     * Provides specific dataset information i.e. position, number and other informations depending on data type.
49
     *
50
     * @param IOpenDataManager $manager connection to database autowired
51
     * @param string           $name    dataset name
52
     * @param string           $date    dataset date in format YYYY-mm-dd
53
     * @param string           $time    dataset time, number between 0 - 23
54
     *
55
     * @return json with dataset information
56
     *
42 57
     * @Route("heatmap/opendata/{name}/{date}/{time}", name="opendata")
43 58
     */
44 59
    public function opendata(IOpenDataManager $manager, $name = 'NONE', $date = '2020-01-01', $time = '1') {
......
49 64
    }
50 65

  
51 66
    /**
67
     * Provides available datasets in given date.
68
     *
69
     * @param IOpenDataManager $manager connection to database autowired
70
     * @param string           $date    dataset date in format YYYY-mm-dd
71
     *
72
     * @return json with available datasets names
73
     *
52 74
     * @Route("heatmap/available/{date}", name="available")
53 75
     */
54 76
    public function availableDatasets(IOpenDataManager $manager, $date = '2020-01-01') {
......
56 78
    }
57 79

  
58 80
    /**
81
     * Provides dates with at least one available dataset.
82
     *
83
     * @param IOpenDataManager $manager connection to database
84
     *
85
     * @return json with dates with at least one available dataset in format YYYY-mm-dd
86
     *
59 87
     * @Route("heatmap/dates", name="dates")
60 88
     */
61 89
    public function datesWithAvailableDatasets(IOpenDataManager $manager) {
......
63 91
    }
64 92

  
65 93
    /**
94
     * Provides for dataset with given name all locations.
95
     *
96
     * @param IOpenDataManager $manager connection to database autowired
97
     * @param string           $name    dataset name
98
     *
99
     * @return json with all locations for dataset with given name as [x => lat, y => lng]
100
     *
66 101
     * @Route("heatmap/positions/{name}", name="positions")
67 102
     */
68 103
    public function dataSourcePoistions(IOpenDataManager $manager, $name = 'NONE') {
......
70 105
    }
71 106

  
72 107
    /**
108
     * Provides last available date for each available dataset type.
109
     *
110
     * @param IOpenDataManager $manager connection to database autowired
111
     *
112
     * @return json with last available date for each available dataset type as [collection-type-name => YYYY-mm-dd]
113
     *
73 114
     * @Route("heatmap/last", name="last")
74 115
     */
75 116
    public function lastAvailableCollections(IOpenDataManager $manager) {
website/src/Controller/HomeController.php
7 7

  
8 8
class HomeController extends AbstractController {
9 9
    /**
10
     * Home page controller.
11
     *
10 12
     * @Route("/", name="home")
11 13
     */
12 14
    public function index() {
website/src/Entity/DataSet.php
2 2

  
3 3
namespace App\Entity;
4 4

  
5
//Todo some checks
5
/**
6
 * Class wich represent From on heatmap page and is bind to that form.
7
 *
8
 * @see https://symfony.com/doc/current/forms.html
9
 */
6 10
class DataSet {
7 11
    protected $time;
8 12
    protected $date;
website/src/Form/DataSetType.php
5 5
use App\Utils\Utils;
6 6
use App\Entity\DataSet;
7 7
use App\Repository\IOpenDataManager;
8
use Symfony\Component\Form\FormView;
9 8
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\FormInterface;
11 9
use Symfony\Component\Form\FormBuilderInterface;
12 10
use Symfony\Component\OptionsResolver\OptionsResolver;
13 11
use Symfony\Component\Form\Extension\Core\Type\TextType;
......
15 13
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16 14

  
17 15
class DataSetType extends AbstractType {
16
    /**
17
     * @var IOpenDataManager autowired connection to database
18
     */
18 19
    private $manager;
19 20

  
21
    /**
22
     * @param IOpenDataManager autowired connection to database
23
     */
20 24
    public function __construct(IOpenDataManager $manager) {
21 25
        $this->manager = $manager;
22 26
    }
23 27

  
28
    /**
29
     * @see https://symfony.com/doc/current/forms.html
30
     */
24 31
    public function buildForm(FormBuilderInterface $builder, array $options) {
25 32
        $builder
26 33
            ->add('date', TextType::class)
34
            // Populate time select with data
27 35
            ->add('time', ChoiceType::class, [
28 36
                'choices' => [
29 37
                    '0:00-1:00' => 0,
......
52 60
                    '23:00-0:00' => 23,
53 61
                ],
54 62
            ])
63
            // Populet type select with data
55 64
            ->add('type', ChoiceType::class, [
56 65
                'choices' => Utils::prepareDatasetsNames($this->manager->getAvailableCollections()),
57 66
            ])
58 67
            ->add('submit', SubmitType::class);
59 68
    }
60 69

  
70
    /**
71
     * @see https://symfony.com/doc/current/forms.html
72
     */
61 73
    public function configureOptions(OptionsResolver $resolver) {
62 74
        $resolver->setDefaults([
63 75
            'data_class' => DataSet::class,
64 76
            'method' => 'GET',
65 77
        ]);
66 78
    }
67

  
68
    // public function buildView(FormView $view, Forminterface $form, array $options) {
69

  
70
    // }
71 79
}
website/src/Repository/IOpenDataManager.php
2 2

  
3 3
namespace App\Repository;
4 4

  
5
/**
6
 * interface for database connection management
7
 * autowired @see website/config/services.yaml and https://symfony.com/doc/current/service_container/autowiring.html.
8
 */
5 9
interface IOpenDataManager {
10
    /**
11
     * Provides specific dataset information i.e. position, number and other informations depending on data type.
12
     *
13
     * @param string $name dataset name
14
     * @param string $date dataset date in format YYYY-mm-dd
15
     * @param string $hour dataset hour, number between 0 - 23
16
     *
17
     * @return array with dataset information
18
     */
6 19
    public function getCollectionDataByName($name, $date, $hour);
7 20

  
21
    /**
22
     * Provides all available data sets types.
23
     *
24
     * @return array with avalable datasets names
25
     */
8 26
    public function getAvailableCollections();
9 27

  
28
    /**
29
     * Provides available datasets in given date.
30
     *
31
     * @param string $date dataset date in format YYYY-mm-dd
32
     *
33
     * @return array with available datasets names
34
     */
10 35
    public function getAvailableCollectionsByDay($date);
11 36

  
37
    /**
38
     * Check if dataset with given name is available for given date.
39
     *
40
     * @param string $name dataset name
41
     * @param string $date dataset date in format YYYY-mm-dd
42
     *
43
     * @return bool true if dataset with given name is available in given date otherwise false
44
     */
12 45
    public function isCollectionAvailable($name, $date);
13 46

  
47
    /**
48
     * Provides dates with at least one available dataset.
49
     *
50
     * @return array with dates with at least one available dataset in format YYYY-mm-dd
51
     */
14 52
    public function getDatesWithAvailableCollection();
15 53

  
54
    /**
55
     * Provides max value of all locations (data dources) in dataset with given date on given date.
56
     *
57
     * @param string $name dataset name
58
     * @param string $date dataset date in format YYYY-mm-dd
59
     *
60
     * @return number max value of all locations (data dources) in dataset with given date on given date
61
     */
16 62
    public function getMaxCollectionNumberAtDay($name, $date);
17 63

  
64
    /**
65
     * Provides for dataset with given name all locations.
66
     *
67
     * @param string $name dataset name
68
     *
69
     * @return array with all locations for dataset with given name as [x => lat, y => lng]
70
     */
18 71
    public function getDataSourcePositions($name);
19 72

  
73
    /**
74
     * Provides last available date for each available dataset type.
75
     *
76
     * @return array with last available date for each available dataset type as [collection-type-name => YYYY-mm-dd]
77
     */
20 78
    public function getLastAvailableCollections();
21 79
}
website/src/Repository/OpenDataManager.php
5 5
use MongoDB\Driver\Query;
6 6
use MongoDB\Driver\Manager;
7 7

  
8
/**
9
 * Implementation of IOpenDataManager
10
 * autowired @see website/config/services.yaml and https://symfony.com/doc/current/service_container/autowiring.html.
11
 *
12
 * @see IOpenDataManager for methods comments
13
 */
8 14
class OpenDataManager implements IOpenDataManager {
15
    /**
16
     * @var IOpenDataManager autowired connection to database
17
     */
9 18
    private $manager;
10 19

  
20
    /**
21
     * @param IOpenDataManager autowired connection to database
22
     */
11 23
    public function __construct($connectionString) {
12 24
        $this->manager = new Manager(
13 25
            $connectionString
14
            // $_ENV['DATABASE_CONNECTION_STRING']
15 26
        );
16 27
    }
17 28

  
......
20 31
            return [];
21 32
        }
22 33

  
34
        // Number has to be two digit
23 35
        $valh = $hour < 10 ? '0'.$hour : $hour;
24 36
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
25 37

  
38
        // Converts result to php array
26 39
        $openData->setTypeMap([
27 40
            'array' => 'array',
28 41
            'document' => 'array',
......
35 48
    public function getAvailableCollections() {
36 49
        $openData = $this->manager->executeQuery('open-data-db.DATASETS', new Query([], ['projection' => ['key-name' => 1, 'display-name' => 1, '_id' => 0]]));
37 50

  
51
        // Converts result to php array
38 52
        $openData->setTypeMap([
39 53
            'array' => 'array',
40 54
            'document' => 'array',
......
104 118

  
105 119
        $max = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query([], ['sort' => ['number' => -1], 'limit' => 1]));
106 120

  
121
        // Converts result to php array
107 122
        $max->setTypeMap([
108 123
            'array' => 'array',
109 124
            'document' => 'array',
......
122 137

  
123 138
        $positions = $this->manager->executeQuery('open-data-db.'.$name.'DEVICES', new Query([], []));
124 139

  
140
        // Converts result to php array
125 141
        $positions->setTypeMap([
126 142
            'array' => 'array',
127 143
            'document' => 'array',
website/src/Utils/Utils.php
2 2

  
3 3
namespace App\Utils;
4 4

  
5
/**
6
 * Class for static helper functions.
7
 */
5 8
class Utils {
9
    /**
10
     * Transforms array of arrays in form [['display-name' => whatever, 'key-name' => whatever, ...], ...]
11
     * to simple array in form [dataset-display-name => dataset-key-name, ...].
12
     *
13
     * @param array of arrays for tranformation
14
     *
15
     * @return array in form [dataset-display-name => dataset-key-name, ...]
16
     */
6 17
    public static function prepareDatasetsNames($datasets) {
7 18
        $names = [];
8 19

  

Také k dispozici: Unified diff