Skip to content

Fix(Perf): compute the emission history with fewer queries and stop the duplicate first day - #401

Open
Herafia wants to merge 3 commits into
support/1.3.0from
perf_history_cron_and_historizable
Open

Herafia wants to merge 3 commits into
support/1.3.0from
perf_history_cron_and_historizable

Conversation

@Herafia

@Herafia Herafia commented Nov 17, 2026

Copy link
Copy Markdown
  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.
  • I have added tests that prove my fix is effective or that my feature works.
  • This change requires a documentation update.

Description

  • Instance: GLPI 11.0 (e2e environment), carbon 1.3.1, debug mode off, demo mode off
  • Data: 2 000 computers, 500 monitors, 200 network equipments, 923 690 daily emission rows (one per asset and per day since its inventory date), 17 520 hourly RTE intensities, usage and embodied impacts per asset
  • Measured scenarios: central page, computer form and its save, environmental impact tab, report (cards cached and recomputed), computer list with the "is historizable" column, UsageImpact cron on 5 495 missing days
  • Measurement method: median of 5 runs after a warm-up (browser Performance API), 3 runs for the cron; SQL from the MariaDB slow log at a zero threshold
  • Fixes: emission rows no longer logged in the asset history (one line per asset and per day); usage profile, power, zone and source read once per asset and emission rows inserted directly, without reloading the asset for every day; gap search started at midnight, so an asset entered at a non-midnight time no longer gets its first day inserted again and the cron no longer stops on a duplicate entry; "is historizable" tests the existence of carbon intensity data instead of counting every row for each listed asset; no isPluginActive() at init; chart scripts loaded only on the pages that can draw charts

SQL Queries

Scenario Without plugin Before After Difference
History cron, 5 495 days - 103 717 23 700 -77.2 %
History cron, lines written in glpi_logs - 5 495 0 -100 %
Computer list with "is historizable" (SQL time) 13 ms 113 ms 14 ms -87.4 %
glpi_plugins per request 8 12 10 -2

Response Times

Scenario Before After Difference
History cron, 5 495 days 19.3 s 8.5 s -56.1 %
History cron, asset created at a non-midnight time Execution error on every run completed fixed
Computer list with "is historizable" (server) 186 ms 84 ms -55.1 %
Report, assets completeness card 785 ms 572 ms -27.1 %

@Herafia Herafia self-assigned this Nov 17, 2026

@Rom1-B Rom1-B left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#398 adds the same $logs_for_parent = false on 12.0/bugfixes; can you two agree on one fix so both branches don't diverge?

Comment thread setup.php Outdated
'/front/dashboard_assets.php',
'/front/dashboard_helpdesk.php',
'/front/ticket.php',
'/front/helpdesk.public.php',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 11.0 this URL redirects to /Helpdesk, whose dashboard tab can show carbon cards, so match the route instead:

Suggested change
'/front/helpdesk.public.php',
'/Helpdesk',

}
}

public function testEvaluateItemTwiceWhenHistoryStartsAtNonMidnightTime()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also assert evaluateItem() leaves glpi_logs untouched for the asset? #398 has that check for the three asset types.

Comment thread src/Location.php Outdated
Comment on lines +502 to +505
// One matching row is enough
unset($request['COUNT']);
$request['SELECT'] = [Location::getTableField('id')];
$request['LIMIT'] = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both callers of getCarbonIntensityDataSourceRequest() strip COUNT the same way, I'd rather have the builder return SELECT id with LIMIT 1 directly and drop these lines here and in SearchOptions.

Comment thread src/Engine/V1/AbstractAsset.php Outdated
Comment on lines 137 to 140
@@ -126,8 +138,24 @@ protected function requestCarbonIntensitiesPerDay(DateTimeImmutable $start_time,
* Returns the declared power for an asset
* @return TrackedInt
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getPower() docblock ended up above getSource(), can you move it back down?

@eltison

eltison commented Nov 18, 2026

Copy link
Copy Markdown

This also fixes #395 and the CarbonEmission part of #396 that we reported on GLPI 11. Thanks.

Laurent Tison, Afnic

@btry

btry commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

There is a risk that the pr breaks a fallback feature (I'm pretty sure this is not covered by a test so far). If the primary source does not have any carbon intensity data the plugin is supposed to fallback to the yearly average data for the proper territory. If there is still no data, then the yearly worldwide carbon intensity shall be used.

@Lainow Lainow left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once @Rom1-B’s comments have been addressed, that will be fine by me.

@Herafia

Herafia commented Nov 18, 2026

Copy link
Copy Markdown
Author

@Rom1-B Aligned with #398: same $logs_for_parent = false fix, and its glpi_logs assertions are now in the three testEvaluateItem()

@Herafia

Herafia commented Nov 18, 2026

Copy link
Copy Markdown
Author

@btry The fallback chain (realtime -> yearly territory average -> worldwide ember) in getCarbonEmissionPerDay is unchanged, the PR only caches the source and the assets source zone which do not depend on the day

# Conflicts:
#	tests/units/Impact/History/NetworkEquipmentTest.php
@Herafia
Herafia force-pushed the perf_history_cron_and_historizable branch from 9af745c to c2802ce Compare November 18, 2026 14:54
@Herafia
Herafia requested review from Lainow and Rom1-B November 18, 2026 14:59
@btry

btry commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Please rebase this PR on top of support/1.3.0 to fix the 2 errors in unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants