|
34 | 34 |
|
35 | 35 | use CommonDBTM; |
36 | 36 | use CommonGLPI; |
| 37 | +use CronTask as GlpiCronTask; |
| 38 | +use Glpi\Application\View\TemplateRenderer; |
| 39 | +use GlpiPlugin\Carbon\CarbonIntensity; |
| 40 | +use GlpiPlugin\Carbon\CronTask; |
37 | 41 | use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory; |
38 | 42 | use GlpiPlugin\Carbon\Source_Zone; |
| 43 | +use GlpiPlugin\Carbon\Toolbox; |
| 44 | +use GlpiPlugin\Carbon\Zone; |
| 45 | +use RuntimeException; |
39 | 46 |
|
40 | 47 | abstract class AbstractCronTask extends CommonGLPI implements CronTaskInterface |
41 | 48 | { |
42 | 49 | protected static string $client_name; |
43 | 50 |
|
| 51 | + protected static string $downloadMethod; |
| 52 | + |
| 53 | + protected const TAB_DIAGNOSIS = 0; |
| 54 | + protected const TAB_CACHE = 1; |
| 55 | + |
| 56 | + |
44 | 57 | public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) |
45 | 58 | { |
46 | 59 | return ''; |
47 | 60 | } |
48 | 61 |
|
49 | | - public function showForCronTask(CommonDBTM $item) |
| 62 | + public function showForCronTask(CommonDBTM $item, int $tabnum) |
| 63 | + { |
| 64 | + if ($tabnum === self::TAB_DIAGNOSIS) { |
| 65 | + switch ($item->fields['name']) { |
| 66 | + case static::$downloadMethod: |
| 67 | + $this->showGapsReport(); |
| 68 | + } |
| 69 | + } elseif ($tabnum === self::TAB_CACHE) { |
| 70 | + switch ($item->fields['name']) { |
| 71 | + case static::$downloadMethod: |
| 72 | + // TODO: implement tab to clear cached data in files/ folder |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function showGapsReport() |
50 | 78 | { |
51 | | - switch ($item->fields['name']) { |
52 | | - case 'Download': |
53 | | - $client = ClientFactory::create(static::$client_name); |
54 | | - $source_name = $client->getSourceName(); |
55 | | - foreach ($client->getSupportedZones() as $zone_name) { |
56 | | - $source_zone = new Source_Zone(); |
57 | | - if (!$source_zone->getFromDbBySourceAndZone($source_name, $zone_name)) { |
58 | | - continue; |
59 | | - } |
60 | | - if (!$source_zone->fields['is_download_enabled']) { |
61 | | - continue; |
62 | | - } |
63 | | - $source_zone->showGaps(); |
64 | | - } |
| 79 | + $renderer = TemplateRenderer::getInstance(); |
| 80 | + $template = <<<TWIG |
| 81 | + {% import "components/form/fields_macros.html.twig" as fields %} |
| 82 | + {{ fields.largeTitle(__('Gaps in carbon intensity time series', 'carbon')) }} |
| 83 | + <div>{{ __('Only zones with download enabled are displayed.', 'carbon') }}</div> |
| 84 | + <div> </div> |
| 85 | +TWIG; |
| 86 | + echo $renderer->renderFromStringTemplate($template); |
| 87 | + $oldest_asset_date = (new Toolbox())->getOldestAssetDate(); |
| 88 | + $client = ClientFactory::create(static::$client_name); |
| 89 | + $source_name = $client->getSourceName(); |
| 90 | + foreach ($client->getSupportedZones() as $zone_name) { |
| 91 | + $source_zone = new Source_Zone(); |
| 92 | + if (!$source_zone->getFromDbBySourceAndZone($source_name, $zone_name)) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + if (!$source_zone->fields['is_download_enabled']) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $zone_id = $source_zone->fields['plugin_carbon_zones_id']; |
| 99 | + $carbon_intensity = new CarbonIntensity(); |
| 100 | + $entries = $carbon_intensity->findGaps( |
| 101 | + $source_zone, |
| 102 | + $oldest_asset_date |
| 103 | + ); |
| 104 | + $total = count($entries); |
| 105 | + $zone = Zone::getById($zone_id); |
| 106 | + |
| 107 | + $template = <<<TWIG |
| 108 | + {% import "components/form/fields_macros.html.twig" as fields %} |
| 109 | + {{ fields.smallTitle(__('Gaps for the zone %s', 'carbon')|format(zone_name)) }} |
| 110 | +TWIG; |
| 111 | + echo $renderer->renderFromStringTemplate($template, ['zone_name' => $zone->fields['name']]); |
| 112 | + $renderer->display('components/datatable.html.twig', [ |
| 113 | + 'is_tab' => true, |
| 114 | + 'nopager' => true, |
| 115 | + 'nofilter' => true, |
| 116 | + 'nosort' => true, |
| 117 | + 'columns' => [ |
| 118 | + 'start' => __('Start'), |
| 119 | + 'end' => __('End', 'carbon'), |
| 120 | + ], |
| 121 | + 'footers' => [ |
| 122 | + ['', '', '', __('Total'), $total, ''], |
| 123 | + ], |
| 124 | + 'footer_class' => 'fw-bold', |
| 125 | + 'entries' => $entries, |
| 126 | + 'total_number' => $total, |
| 127 | + 'filtered_number' => $total, |
| 128 | + 'showmassiveactions' => false, |
| 129 | + 'massiveactionparams' => [ |
| 130 | + 'num_displayed' => $total, |
| 131 | + 'container' => 'mass' . static::class . mt_rand(), |
| 132 | + ], |
| 133 | + ]); |
65 | 134 | } |
66 | 135 | } |
| 136 | + |
| 137 | + /** |
| 138 | + * Uniformize download method of carbon intensity clients |
| 139 | + * The design of \CronTask requires that each cron task to use a unique method name, across itemtypes |
| 140 | + * |
| 141 | + * @param string $name |
| 142 | + * @param array $arguments |
| 143 | + * @return int |
| 144 | + */ |
| 145 | + public static function __callStatic($name, $arguments) |
| 146 | + { |
| 147 | + if ($name === 'cron' . static::$downloadMethod) { |
| 148 | + return self::cronDownload(...$arguments); |
| 149 | + } |
| 150 | + throw new RuntimeException('Not implemented'); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Automatic action to download carbon intensity from a data source |
| 155 | + * |
| 156 | + * @return int |
| 157 | + */ |
| 158 | + public static function cronDownload(GlpiCronTask $task): int |
| 159 | + { |
| 160 | + $client = ClientFactory::create(static::$client_name); |
| 161 | + return CronTask::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity()); |
| 162 | + } |
67 | 163 | } |
0 commit comments