Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 10x 2x 2x 2x 2x 10x 1x | import { Logger } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { Company, DataConnectionName, Plan } from '@amalia/core/models';
import { ConfigurationError, EngineErrorBase, isEngineErrorHandledProperly } from '@amalia/core/types';
import {
StatementCalculationCacheFactory,
type StatementCalculationCache,
} from '@amalia/payout-calculation/compute-engine/core-statement-calculation-cache';
import { type PlanTemplateDehydrated } from '@amalia/payout-calculation/types';
import { PlansService } from '@amalia/payout-definition/designer/core';
import { AdminScopesRepository } from '@amalia/tenants/companies/admin-scopes/auth';
import { PlanTemplate } from './planTemplateBuilder/PlanTemplate';
export class PlanTemplateService {
private readonly logger = new Logger(PlanTemplateService.name);
public constructor(
private readonly planService: PlansService,
private readonly connection: DataSource,
@InjectDataSource(DataConnectionName)
private readonly dataConnection: DataSource,
private readonly statementCalculationCacheFactory: StatementCalculationCacheFactory,
private readonly adminScopesRepository: AdminScopesRepository,
) {}
/**
* Build a PlanTemplate outside the context of calculation.
*
* Useful for debug, onboarding, analyze... It allows admins to lint their plan and
* find issues. It does not throw if there is a configuration error, which allows us
* to debug the progress.
*
* @param company
* @param planId
* @param buildConfiguration
*/
public async buildPlanTemplateStandalone(
company: Company,
planId: string,
buildConfiguration?: {
canSeeSqlRequests?: boolean;
includeDefinitions?: boolean;
debugMode?: boolean;
},
): Promise<{
planTemplate: PlanTemplateDehydrated | null;
error?: EngineErrorBase;
}> {
let plan: Plan | undefined;
try {
plan = await this.planService.findById(company, planId, [
'highlightedKpis',
'planRules',
'planRules.rule',
'planRules.rule.filter',
]);
} catch {
return {
planTemplate: null,
error: new ConfigurationError(
`Plan cannot be found when recomputing the plan template for company ${company.name}, plan ${planId}`,
),
};
}
const statementCalculationCache = await this.statementCalculationCacheFactory.instantiate(company, { planId });
const { planTemplate, error } = await this.buildTemplate(
company,
plan,
statementCalculationCache,
!!buildConfiguration?.debugMode,
);
return {
planTemplate: planTemplate.getDehydratedPlanTemplate({
canSeeSqlRequests: !!buildConfiguration?.canSeeSqlRequests,
}),
error,
};
}
/**
* Builds a plan template in the context of a calculation.
*
* @param company
* @param plan
* @param statementCalculationCache
* @param debugMode
*/
public async buildTemplate(
company: Company,
plan: Plan,
statementCalculationCache: StatementCalculationCache,
debugMode: boolean,
): Promise<{ planTemplate: PlanTemplate; error?: EngineErrorBase }> {
const startDate = Date.now();
const scopes = await this.adminScopesRepository.listScopesForPlan(company.id, plan);
const planTemplate = new PlanTemplate(plan, statementCalculationCache, scopes);
try {
await planTemplate.build();
const postBuildStartDate = Date.now();
await planTemplate.postBuild();
planTemplate.buildQueryContainers(this.connection, this.dataConnection, debugMode);
const buildTimeMs = postBuildStartDate - startDate;
const postBuildTimeMs = Date.now() - postBuildStartDate;
this.logger.debug({
message: `Plan template ${plan.name}: build ${buildTimeMs}ms, post-build ${postBuildTimeMs}ms, hits ${planTemplate.hits}, complexity ${planTemplate.recursionLevel}`,
buildTimeMs,
postBuildTimeMs,
hits: planTemplate.hits,
recursionLevel: planTemplate.recursionLevel,
});
planTemplate.metadata = {
...planTemplate.metadata,
buildTimeMs,
postBuildTimeMs,
};
return { planTemplate };
} catch (e) {
// Properly catch, so we can still inspect the plan after its error.
if (isEngineErrorHandledProperly(e)) {
return { planTemplate, error: e };
}
// Bubble up unwanted errors.
throw e;
}
}
}
|