Subversion Repositories eFlore/Applications.cel

Rev

Rev 3765 | Rev 3771 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3765 Rev 3768
Line 20... Line 20...
20
 
20
 
21
	// Cache des obs id PN traitées pendant cette run
21
	// Cache des obs id PN traitées pendant cette run
Line 22... Line 22...
22
	protected $processedObsId = [];
22
	protected $processedObsId = [];
23
 
-
 
24
	// Paramètre de suivi et de pagination de l'API des obs PN
23
 
25
	protected $startDate = '';
24
	// Paramètre de suivi et de pagination de l'API des obs PN
26
	protected $currentPage = '';
25
	protected $currentPage = '';
27
	protected $currentProject = '';
26
	protected $currentProject = '';
Line 36... Line 35...
36
		'bark' => 'écorce',
35
		'bark' => 'écorce',
37
		'branch' => 'branche',
36
		'branch' => 'branche',
38
		'habit' => 'port',
37
		'habit' => 'port',
39
	];
38
	];
Line -... Line 39...
-
 
39
 
-
 
40
	// Différents types de lifecle possible pour filtrer les obs
-
 
41
	// Modified renvoi les obs en fonction de leur date de modif
-
 
42
	// Created, en fonction de la date de création
-
 
43
	// Deleted, well, you already got it
-
 
44
	protected $lifecycles = [
-
 
45
		'created',
-
 
46
		'modified',
-
 
47
		'deleted', // deleted peut être très lent
-
 
48
	];
40
 
49
 
41
	/**
50
	/**
42
	 * Paramêtres par défaut disponibles pour la ligne de commande
51
	 * Paramêtres par défaut disponibles pour la ligne de commande
43
	 * le tableau se construit de la forme suivante :
52
	 * le tableau se construit de la forme suivante :
44
	 * - clé =  nom du paramêtre '-foo'
53
	 * - clé =  nom du paramêtre '-foo'
Line 50... Line 59...
50
	 * Le dernier parametre du tableau peut avoir la valeur '...',
59
	 * Le dernier parametre du tableau peut avoir la valeur '...',
51
	 * il contiendra alors l'ensemble des paramêtres suivant trouvés sur la ligne de commande.
60
	 * il contiendra alors l'ensemble des paramêtres suivant trouvés sur la ligne de commande.
52
	 * @var array
61
	 * @var array
53
	 */
62
	 */
54
	protected $parametres_autorises = array(
63
	protected $parametres_autorises = array(
55
		'-startDate' => array(false, '1', 'Date de début (parsée par strtotime). Ex: "YYYY:MM:DD HH:II:SS"'),
64
		'-startDate' => array(false, true, 'Date de début (parsée par strtotime). Ex: "YYYY:MM:DD HH:II:SS"'),
-
 
65
		'-lifecycle' => array(false, true, 'Par défaut : modified ; Au choix parmi : created/modified/deleted (deleted est lent)'),
56
		'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
66
		'-pnObsId' => array(false, true, "ID de l'obs chez PlantNet"),
57
		'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
67
		'-project' => array(false, true, "projectId de l'obs chez PlantNet"),
58
		'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
68
		'-tbObsId' => array(false, true, "ID de l'obs chez Tela"),
59
	);
69
	);
Line 82... Line 92...
82
				$this->updateCorrespondingIdsTable();
92
				$this->updateCorrespondingIdsTable();
83
				break;
93
				break;
84
			default:
94
			default:
85
				$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
95
				$msg = "Erreur : la commande '$cmd' n'existe pas!\n"
86
						. ', utilisez plutôt :' . "\n"
96
						. ', utilisez plutôt :' . "\n"
87
						. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS"' . "\n"
97
						. 'php cli.php PullPlantnet -a updateLatest -startDate "YYYY:MM:DD HH:II:SS" [[-lifecycle [modified]/created/deleted]]' . "\n"
88
						. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
98
						. 'php cli.php PullPlantnet -a updateOnePN -pnObsId "xxxxxx" -project "xxxxxx"' . "\n"
89
						. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
99
						. 'php cli.php PullPlantnet -a updateOneTB -tbObsId "xxxxxx"' . "\n"
90
						. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
100
						. 'php cli.php PullPlantnet -a updateCorrespondingIdsTable' . "\n"
91
						;
101
						;
92
				throw new Exception($msg);
102
				throw new Exception($msg);
Line 96... Line 106...
96
	/**
106
	/**
97
	 * Considering all modified pn obs since startDate
107
	 * Considering all modified pn obs since startDate
98
	 * One pn project after an other
108
	 * One pn project after an other
99
	 * Inserting obs in tb db (if new and matching user email)
109
	 * Inserting obs in tb db (if new and matching user email)
100
	 */
110
	 */
101
	private function updateLatest(string $startDate = '1917:03:15 00:00:00') {
111
	private function updateLatest(string $startDate = '1917:03:15 00:00:00', string $lifecycle = 'modified') {
102
		$this->startDate = strtotime($startDate) * 1000; // we need a microtimestamp
112
		$startDate = strtotime($startDate) * 1000; // we need a microtimestamp
Line 103... Line 113...
103
 
113
 
104
		$pnProjects = $this->getProjects(); // refresh projects list
114
		$pnProjects = $this->getProjects(); // refresh projects list
105
		foreach ($pnProjects as $project) {
115
		foreach ($pnProjects as $project) {
106
			do {
116
			do {
107
				$observations_PN = $this->getProjectLatest($project);
117
				$observations_PN = $this->getProjectLatest($project, $startDate, $lifecycle);
108
				$this->updateObs($observations_PN);
118
				$this->updateObs($observations_PN);
109
			} while ($this->hasMore);
119
			} while ($this->hasMore);
110
		}
120
		}
Line 111... Line 121...
111
	}
121
	}
112
 
122
 
Line 113... Line 123...
113
	private function getProjectLatest(array $project): array {
123
	private function getProjectLatest(array $project, int $startDate, string $lifecycle): array {
114
		$this->currentProject = $project;
124
		$this->currentProject = $project;
Line 115... Line 125...
115
 
125
 
116
		if (!$this->currentPage) {
126
		if (!$this->currentPage) {
117
			echo 'Projet ' . $project['name'] . "\n";
127
			echo 'Projet ' . $project['name'] . "\n";
118
 
128
 
119
			// hop, on appelle le service de PN
129
			// hop, on appelle le service de PN
120
			$url_service = str_replace(
130
			$url_service = str_replace(
121
				['{project}', '{token}', '{startDate}'],
131
				['{project}', '{token}', '{startDate}', '{lifecycle}'],
122
				[$project['id'], Config::get('tokenPlantnet'), $this->startDate],
132
				[$project['id'], Config::get('tokenPlantnet'), $startDate, $lifecycle],
123
				Config::get('urlPlantnetBase').Config::get('urlPlantnetLatestChanges')
133
				Config::get('urlPlantnetBase').Config::get('urlPlantnetLatestChanges')
Line 168... Line 178...
168
		$reponse = curl_exec($ch);
178
		$reponse = curl_exec($ch);
169
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
179
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
170
		curl_close($ch);
180
		curl_close($ch);
Line 171... Line 181...
171
 
181
 
172
		if (!$reponse || 200 != $code) {
182
		if (!$reponse || 200 != $code) {
173
			throw new Exception("\nPN répond avec une $code à l'adresse $url_service : $reponse\n");
183
			throw new Exception("\nPN répond avec une $code à l'adresse $url : $reponse\n");
174
		}
184
		}
Line 175... Line 185...
175
		$pnProjects = json_decode($reponse, true);
185
		$pnProjects = json_decode($reponse, true);