4141use Exception ;
4242use Glpi \DBAL \QueryParam ;
4343use GlpiPlugin \Carbon \DataSource \CarbonIntensity \ClientInterface ;
44- use RuntimeException ;
4544use Symfony \Component \Console \Helper \ProgressBar ;
4645
4746/**
@@ -124,15 +123,16 @@ public function rawSearchOptions()
124123 /**
125124 * get carbon intensity dates for a source and a zone
126125 *
127- * @param string $zone_name Zone to examinate
128- * @param string $source_name Source to examinate
126+ * @param Source_Zone $source_zone Source_Zone to examinate
129127 * @return array
130128 */
131- private function getKnownDatesQuery (string $ zone_name , string $ source_name )
129+ private function getKnownDatesQuery (Source_Zone $ source_zone )
132130 {
133131 $ intensity_table = CarbonIntensity::getTable ();
134132 $ source_table = Source::getTable ();
135133 $ zone_table = Zone::getTable ();
134+ $ source_fk = getForeignKeyFieldForItemType (Source::class);
135+ $ zone_fk = getForeignKeyFieldForItemType (Zone::class);
136136 return [
137137 'SELECT ' => [$ intensity_table => ['id ' , 'date ' ]],
138138 'FROM ' => $ intensity_table ,
@@ -151,25 +151,24 @@ private function getKnownDatesQuery(string $zone_name, string $source_name)
151151 ],
152152 ],
153153 'WHERE ' => [
154- Source:: getTableField ( ' name ' ) => $ source_name ,
155- Zone:: getTableField ( ' name ' ) => $ zone_name ,
154+ $ source_fk => $ source_zone -> fields [ $ source_fk ] ,
155+ $ zone_fk => $ source_zone -> fields [ $ zone_fk ] ,
156156 ],
157157 ];
158158 }
159159
160160 /**
161161 * Get the last known date of carbon emissiosn
162162 *
163- * @param string $zone_name Zone to examinate
164- * @param string $source_name Source to examinate
163+ * @param Source_Zone $source_zone Source_Zone to examinate
165164 * @return DateTimeImmutable
166165 */
167- public function getLastKnownDate (string $ zone_name , string $ source_name ): ?DateTimeImmutable
166+ public function getLastKnownDate (Source_Zone $ source_zone ): ?DateTimeImmutable
168167 {
169168 /** @var DBmysql $DB */
170169 global $ DB ;
171170
172- $ request = $ this ->getKnownDatesQuery ($ zone_name , $ source_name );
171+ $ request = $ this ->getKnownDatesQuery ($ source_zone );
173172 $ request ['ORDER ' ] = CarbonIntensity::getTableField ('date ' ) . ' DESC ' ;
174173 $ request ['LIMIT ' ] = '1 ' ;
175174 $ result = $ DB ->request ($ request )->current ();
@@ -182,16 +181,15 @@ public function getLastKnownDate(string $zone_name, string $source_name): ?DateT
182181 /**
183182 * Get the first known date of carbon emissiosn
184183 *
185- * @param string $zone_name Zone to examinate
186- * @param string $source_name Source to examinate
184+ * @param Source_Zone $source_zone
187185 * @return DateTimeImmutable
188186 */
189- public function getFirstKnownDate (string $ zone_name , string $ source_name ): ?DateTimeImmutable
187+ public function getFirstKnownDate (Source_Zone $ source_zone ): ?DateTimeImmutable
190188 {
191189 /** @var DBmysql $DB */
192190 global $ DB ;
193191
194- $ request = $ this ->getKnownDatesQuery ($ zone_name , $ source_name );
192+ $ request = $ this ->getKnownDatesQuery ($ source_zone );
195193 $ request ['ORDER ' ] = CarbonIntensity::getTableField ('date ' ) . ' ASC ' ;
196194 $ request ['LIMIT ' ] = '1 ' ;
197195 $ result = $ DB ->request ($ request )->current ();
@@ -206,12 +204,12 @@ public function getFirstKnownDate(string $zone_name, string $source_name): ?Date
206204 * Download data for a single zone
207205 *
208206 * @param ClientInterface $data_source
209- * @param string $zone_name zone name
207+ * @param Source_Zone $source_zone
210208 * @param int $limit maximum count of items to process
211209 * @param ProgressBar $progress_bar progress bar to update (CLI mode only)
212210 * @return int count of item downloaded
213211 */
214- public function downloadOneZone (ClientInterface $ data_source , string $ zone_name , int $ limit = 0 , ?ProgressBar $ progress_bar = null ): int
212+ public function downloadOneZone (ClientInterface $ data_source , Source_Zone $ source_zone , int $ limit = 0 , ?ProgressBar $ progress_bar = null ): int
215213 {
216214 $ start_date = $ this ->getDownloadStartDate ();
217215
@@ -221,14 +219,14 @@ public function downloadOneZone(ClientInterface $data_source, string $zone_name,
221219 $ source = new Source ();
222220 $ source ->getFromDBByCrit (['name ' => $ data_source ->getSourceName ()]);
223221 $ zone = new Zone ();
224- $ zone ->getFromDBByCrit (['name ' => $ zone_name ]);
222+ $ zone ->getFromDBByCrit (['id ' => $ source_zone -> fields [Zone:: getForeignKeyField ()] ]);
225223 $ gaps = $ this ->findGaps ($ source ->getID (), $ zone ->getID (), $ start_date );
226224 if (count ($ gaps ) === 0 ) {
227225 // Log a notice specifying the source and the zone
228226 trigger_error (sprintf (
229227 "No gap to fill for source %s and zone %s between %s and %s " ,
230228 $ data_source ->getSourceName (),
231- $ zone_name ,
229+ $ zone -> fields [ ' name ' ] ,
232230 $ start_date ->format ('Y-m-d ' ),
233231 'now '
234232 ), E_USER_WARNING );
@@ -249,7 +247,7 @@ public function downloadOneZone(ClientInterface $data_source, string $zone_name,
249247 foreach ($ gaps as $ gap ) {
250248 $ gap_start = DateTimeImmutable::createFromFormat ('Y-m-d H:i:s ' , $ gap ['start ' ]);
251249 $ gap_end = DateTimeImmutable::createFromFormat ('Y-m-d H:i:s ' , $ gap ['end ' ]);
252- $ count = $ data_source ->fullDownload ($ zone_name , $ gap_start , $ gap_end , $ this , $ limit , $ progress_bar );
250+ $ count = $ data_source ->fullDownload ($ source_zone , $ gap_start , $ gap_end , $ this , $ limit , $ progress_bar );
253251 $ total_count += $ count ;
254252 if ($ total_count >= $ limit ) {
255253 return $ total_count ;
@@ -281,65 +279,48 @@ public function getDownloadStartDate(): ?DateTimeImmutable
281279 return $ start_date ;
282280 }
283281
284- /**
285- * Get date where data download shall end, excluding the incremental download mode for the specified data source
286- *
287- * @param string $zone_name zone to examine
288- * @param ClientInterface $data_source data source
289- * @return DateTimeImmutable
290- */
291- public function getDownloadStopDate (string $ zone_name , ClientInterface $ data_source ): DateTimeImmutable
292- {
293- $ stop_date = $ data_source ->getMaxIncrementalAge ();
294- $ first_known_intensity_date = $ this ->getFirstKnownDate ($ zone_name , $ data_source ->getSourceName ());
295- if ($ first_known_intensity_date !== null ) {
296- $ first_known_intensity_date = $ first_known_intensity_date ->sub (new DateInterval ('PT1H ' ));
297- $ stop_date = min ($ stop_date , $ first_known_intensity_date );
298- }
299-
300- return $ stop_date ;
301- }
282+ // / **
283+ // * Get date where data download shall end, excluding the incremental download mode for the specified data source
284+ // *
285+ // * @param string $zone_name zone to examine
286+ // * @param ClientInterface $data_source data source
287+ // * @return DateTimeImmutable
288+ // */
289+ // public function getDownloadStopDate(string $zone_name, ClientInterface $data_source): DateTimeImmutable
290+ // {
291+ // $stop_date = $data_source->getMaxIncrementalAge();
292+ // $first_known_intensity_date = $this->getFirstKnownDate($zone_name, $data_source->getSourceName());
293+ // if ($first_known_intensity_date !== null) {
294+ // $first_known_intensity_date = $first_known_intensity_date->sub(new DateInterval('PT1H'));
295+ // $stop_date = min($stop_date, $first_known_intensity_date);
296+ // }
297+
298+ // return $stop_date;
299+ // }
302300
303301 /**
304302 * Save in database the carbon intensities
305303 * Give up on failures
306304 *
307- * @param string $zone_name name of the zone to store intensities
308- * @param string $source_name name of the source to store intensities
305+ * @param Source_zone $source_zone Source_zone
309306 * @param array $data as an array of arrays ['datetime' => string, 'intensity' => float]
310307 * @return int count of actually saved items,
311308 */
312- public function save (string $ zone_name , string $ source_name , array $ data ): int
309+ public function save (Source_zone $ source_zone , array $ data ): int
313310 {
314311 /** @var DBmysql $DB */
315312 global $ DB ;
316313
317314 $ count = 0 ;
318- $ source = new Source ();
319- $ source ->getFromDBByCrit ([
320- 'name ' => $ source_name ,
321- ]);
322- if ($ source ->isNewItem ()) {
323- throw new RuntimeException ('Attempt to save carbon intensity with a source which is not in the database ' );
324- // trigger_error('Attempt to save carbon intensity with a source which is not in the database', E_USER_ERROR);
325- // return 0;
326- }
327- $ zone = new Zone ();
328- $ zone ->getFromDBByCrit ([
329- 'name ' => $ zone_name ,
330- ]);
331- if ($ zone ->isNewItem ()) {
332- throw new RuntimeException ('Attempt to save carbon intensity with a zone which is not in the database ' );
333- // trigger_error('Attempt to save carbon intensity with a zone which is not in the database', E_USER_ERROR);
334- // return 0;
335- }
336315
316+ $ source_fk = getForeignKeyFieldForItemType (Source::class);
317+ $ zone_fk = getForeignKeyFieldForItemType (Zone::class);
337318 $ query = $ DB ->buildInsert (
338319 CarbonIntensity::getTable (),
339320 [
340321 'date ' => new QueryParam (),
341- Source:: getForeignKeyField () => new QueryParam (),
342- Zone:: getForeignKeyField () => new QueryParam (),
322+ $ source_fk => new QueryParam (),
323+ $ zone_fk => new QueryParam (),
343324 'intensity ' => new QueryParam (),
344325 'data_quality ' => new QueryParam (),
345326 ],
@@ -351,8 +332,8 @@ public function save(string $zone_name, string $source_name, array $data): int
351332 $ stmt ->bind_param (
352333 'siidi ' ,
353334 $ intensity ['datetime ' ],
354- $ source ->fields [' id ' ],
355- $ zone ->fields [' id ' ],
335+ $ source_zone ->fields [$ source_fk ],
336+ $ source_zone ->fields [$ zone_fk ],
356337 $ intensity ['intensity ' ],
357338 $ intensity ['data_quality ' ]
358339 );
0 commit comments