Skip to content

Commit 9416376

Browse files
committed
fix(Datasource\CarbonIntensity\ElectricityMaps\Client): allow paid API key
1 parent 677305a commit 9416376

14 files changed

Lines changed: 386 additions & 272 deletions

File tree

src/CarbonIntensity.php

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use Exception;
4242
use Glpi\DBAL\QueryParam;
4343
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientInterface;
44-
use RuntimeException;
4544
use 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
);

src/Command/CollectCarbonIntensityCommand.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class CollectCarbonIntensityCommand extends AbstractCommand
5959
{
6060
/** @var int ID of the data source being processed */
6161
private int $source_id;
62+
/** @var Source_Zone The relatin between a source and a zone to describe which data to download and save */
63+
private Source_Zone $source_zone;
6264
private ?ClientInterface $client = null;
6365
private array $zones = [];
6466

@@ -122,18 +124,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
122124
$message = __('Creating data source name', 'carbon');
123125
$output->writeln("<info>$message</info>");
124126

125-
// Create the source if it does not exist
127+
// Check the source exists
126128
$data_source = new Source();
127129
$source_name = $input->getArgument('source');
128130
if (!$data_source->getFromDBByCrit(['name' => $source_name])) {
129-
$data_source->add([
130-
'name' => $source_name,
131-
]);
132-
if ($data_source->isNewItem()) {
133-
$message = __("Source not found", 'carbon');
134-
$output->writeln("<error>$message</error>");
135-
return Command::FAILURE;
136-
}
131+
$message = __("This source does not exist", 'casrbon');
132+
$output->writeln("<error>$message</error>");
133+
return Command::FAILURE;
137134
}
138135
$this->source_id = $data_source->getID();
139136

@@ -143,22 +140,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
143140
$zone->getFromDBByCrit(['name' => $this->zones[$zone_code]]);
144141
$carbon_intensity = new CarbonIntensity();
145142

146-
// Create relation between source and zone if t does not exist
143+
// Check the relation between source and zone
147144
$source_zone = new Source_Zone();
148145
$input = [
149146
$data_source::getForeignKeyField() => $data_source->getID(),
150147
$zone::getForeignKeyField() => $zone->getID(),
151148
];
152149
$source_zone->getFromDbByCrit($input);
153150
if ($source_zone->isNewItem()) {
154-
$input['is_download_enabled'] = 1;
155-
$input['code'] = $zone_code;
156-
if ($source_zone->add($input) === false) {
157-
$message = __("Creation of relation between source and zone failed", 'carbon');
158-
$output->writeln("<error>$message</error>");
159-
return Command::FAILURE;
160-
}
151+
$message = __("The zone is not handled by the data source", 'casrbon');
152+
$output->writeln("<error>$message</error>");
153+
return Command::FAILURE;
161154
}
155+
$this->source_zone = $source_zone;
162156

163157
$message = __("Reading data...", 'carbon');
164158
$output->writeln("<info>$message</info>");
@@ -172,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
172166
$this->client->disableCache();
173167
}
174168

175-
$carbon_intensity->downloadOneZone($this->client, $this->zones[$zone_code], 0, new ProgressBar($this->output));
169+
$carbon_intensity->downloadOneZone($this->client, $this->source_zone, 0, new ProgressBar($this->output));
176170

177171
// Find start and stop dates to cover
178172
$start_date = $carbon_intensity->getDownloadStartDate();

src/CronTask.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ public static function downloadCarbonIntensityFromSource(GlpiCronTask $task, Cli
273273
$task->addVolume($done_count);
274274
}
275275

276-
$zones = $data_source->getZones(['is_download_enabled' => 1]);
277-
if (count($zones) === 0) {
276+
$rows = $data_source->getSourceZones(['is_download_enabled' => 1]);
277+
if (count($rows) === 0) {
278278
trigger_error(__('No zone to download', 'carbon'), E_USER_WARNING);
279279
return 0;
280280
}
281281

282-
$limit_per_zone = max(1, floor(($remaining) / count($zones)));
282+
$limit_per_zone = max(1, floor(($remaining) / count($rows)));
283283
$count = 0;
284-
foreach ($zones as $zone) {
285-
$zone_name = $zone['name'];
284+
foreach ($rows as $row) {
285+
$source_zone = Source_Zone::getById($row['id']);
286286
try {
287-
$added = $intensity->downloadOneZone($data_source, $zone_name, $limit_per_zone);
287+
$added = $intensity->downloadOneZone($data_source, $source_zone, $limit_per_zone);
288288
} catch (RuntimeException $e) {
289289
trigger_error($e->getMessage(), E_USER_WARNING);
290290
continue;

0 commit comments

Comments
 (0)