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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | 6x 2x 7x 7x 7x 3x 1x 2x 2x 6x 6x 6x 5x 1x 1x 5x 4x 4x 2x 1x 1x 1x 4x 4x 2x 2x 2x 20x 20x 14x 6x 6x 12x 12x 12x 9x 3x 3x 4x 4x 3x 1x 1x 5x 5x 1x 5x 5x 2x 5x 5x 2x 2x 2x 2x 2x 2x 2x 10x 10x 1x 10x 10x 10x 10x 1x 10x 10x 36x 6x 3x 3x 3x 1x 3x 1x 3x 3x 3x 7x 6x 6x 6x 6x 5x 5x 5x 4x 5x 5x 5x 5x 5x 4x 4x 4x 4x 12x 12x 12x 12x 12x 12x 12x 12x 4x 4x 4x 4x 4x 4x 4x 4x 4x | /**
* Report generators for different report types
*
* ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
*/
import type { z } from 'zod';
import type { GenerateReportSchema } from '../../schemas/europeanParliament.js';
import { epClient } from '../../clients/europeanParliamentClient.js';
import { APIError } from '../../clients/ep/baseClient.js';
import { auditLogger, toErrorMessage } from '../../utils/auditLogger.js';
import type { MEPDetails, Committee } from '../../types/europeanParliament.js';
import type { Report } from './types.js';
import { ToolError } from '../shared/errors.js';
import {
createVotingSection,
createCommitteeSection,
createParliamentaryQuestionsSection,
createMeetingActivitySection,
createLegislativeOutputSection,
createMemberParticipationSection,
createOverallVotingSection,
createAdoptionRatesSection,
createPoliticalGroupSection,
createNewProposalsSection,
createCompletedProceduresSection,
createOngoingProceduresSection
} from './reportBuilders.js';
/**
* Extract MEP data for report generation
* Cyclomatic complexity: 1
*/
function extractMEPData(
params: z.infer<typeof GenerateReportSchema>,
mep: MEPDetails | null
): {
mepName: string;
dateFrom: string;
dateTo: string;
totalVotes: number;
committeesLength: number;
} {
return {
mepName: mep?.name ?? 'Unknown MEP',
dateFrom: params.dateFrom ?? '2024-01-01',
dateTo: params.dateTo ?? '2024-12-31',
totalVotes: mep?.votingStatistics?.totalVotes ?? 0,
committeesLength: mep?.committees.length ?? 0
};
}
/**
* Sanitize a subject ID for inclusion in error messages.
* Truncates to 100 chars and strips control characters to prevent log injection.
*/
function sanitizeSubjectId(subjectId: string): string {
return subjectId.replace(/[\x00-\x1F\x7F]/g, '').slice(0, 100);
}
/** Fetch MEP details (null if subjectId not provided; throws ToolError for 404) */
async function fetchMEPDetails(subjectId: string | undefined): Promise<MEPDetails | null> {
Iif (subjectId === undefined) return null;
try {
return await epClient.getMEPDetails(subjectId);
} catch (error: unknown) {
// 404 = invalid subjectId — throw non-retryable error immediately
if (error instanceof APIError && error.statusCode === 404) {
throw new ToolError({
toolName: 'generate_report',
operation: 'generateReport',
message: `MEP not found: '${sanitizeSubjectId(subjectId)}' is not a valid MEP identifier`,
isRetryable: false,
errorCode: 'UPSTREAM_404',
httpStatus: 404,
});
}
auditLogger.logError('generate_report.fetch_mep_details', { subjectId }, toErrorMessage(error));
return null;
}
}
/** Fetch question count for an MEP (null if unavailable) */
async function fetchQuestionCount(subjectId: string | undefined): Promise<number | null> {
Iif (subjectId === undefined) return null;
try {
const questions = await epClient.getParliamentaryQuestions({ author: subjectId, limit: 100 });
return questions.data.length;
} catch (error: unknown) {
auditLogger.logError('generate_report.fetch_question_count', { subjectId }, toErrorMessage(error));
return null;
}
}
/** Fetch committee info (null if subjectId not provided; throws ToolError for 404) */
async function fetchCommitteeInfo(subjectId: string | undefined): Promise<Committee | null> {
if (subjectId === undefined) return null;
try {
return await epClient.getCommitteeInfo({ id: subjectId });
} catch (error: unknown) {
// 404 = invalid subjectId — throw non-retryable error immediately
if (error instanceof APIError && error.statusCode === 404) {
throw new ToolError({
toolName: 'generate_report',
operation: 'generateReport',
message: `Committee not found: '${sanitizeSubjectId(subjectId)}' is not a valid committee identifier`,
isRetryable: false,
errorCode: 'UPSTREAM_404',
httpStatus: 404,
});
}
auditLogger.logError('generate_report.fetch_committee_info', { subjectId }, toErrorMessage(error));
return null;
}
}
/** Fetch committee document count for a year (null if unavailable) */
async function fetchDocumentCount(year: number): Promise<number | null> {
try {
const docs = await epClient.getCommitteeDocuments({ year, limit: 100 });
return docs.data.length;
} catch (error: unknown) {
auditLogger.logError('generate_report.fetch_document_count', { year }, toErrorMessage(error));
return null;
}
}
/** Fetch adopted text count for a year (null if unavailable) */
async function fetchAdoptedTextCount(year: number): Promise<number | null> {
try {
const adopted = await epClient.getAdoptedTexts({ year, limit: 100 });
return adopted.data.length;
} catch (error: unknown) {
auditLogger.logError('generate_report.fetch_adopted_text_count', { year }, toErrorMessage(error));
return null;
}
}
/** Fetch plenary session count for a date range (null if unavailable) */
async function fetchSessionCount(dateFrom: string, dateTo: string): Promise<number | null> {
try {
const year = parseInt(dateFrom.substring(0, 4), 10);
const sessions = await epClient.getPlenarySessions({ year, limit: 100 });
return sessions.data.length;
} catch (error: unknown) {
auditLogger.logError('generate_report.fetch_session_count', { dateFrom, dateTo }, toErrorMessage(error));
return null;
}
}
/** Fetch procedure count for a year (null if unavailable) */
async function fetchProcedureCount(year: number): Promise<number | null> {
try {
const procedures = await epClient.getProcedures({ year, limit: 100 });
return procedures.data.length;
} catch (error: unknown) {
auditLogger.logError('generate_report.fetch_procedure_count', { year }, toErrorMessage(error));
return null;
}
}
/** Build data quality warnings for MEP activity report */
function buildMEPWarnings(
mep: MEPDetails | null,
questionsSubmitted: number | null,
subjectIdProvided: boolean
): string[] {
const warnings: string[] = [];
if (mep === null) {
warnings.push(subjectIdProvided
? 'MEP details unavailable from EP API; upstream data source failed.'
: 'MEP details not available; subject ID was not provided.');
}
Iif (questionsSubmitted === null) {
warnings.push('Parliamentary questions count unavailable from EP API.');
}
if (mep !== null && mep.votingStatistics === undefined) {
warnings.push('Voting statistics not available for this MEP.');
}
warnings.push('Reports authored count is always zero; EP API does not provide per-MEP report authorship data.');
return warnings;
}
/** Build data quality warnings for committee performance report */
function buildCommitteeWarnings(
committee: Committee | null,
documentsProduced: number | null,
reportsProduced: number | null,
subjectIdProvided: boolean
): string[] {
const warnings: string[] = [];
Iif (committee === null) {
warnings.push(subjectIdProvided
? 'Committee details unavailable from EP API; upstream data source failed.'
: 'Committee details not available; subject ID was not provided.');
}
Iif (documentsProduced === null) {
warnings.push('Committee documents count unavailable from EP API.');
}
Iif (reportsProduced === null) {
warnings.push('Adopted texts count unavailable from EP API.');
}
warnings.push('Meeting count is always zero; EP API does not provide committee-specific meeting counts.');
warnings.push('Document and adopted text counts are parliament-wide (first page), not filtered by committee.');
return warnings;
}
/** Build data quality warnings for voting statistics report */
function buildVotingWarnings(
sessionCount: number | null,
adoptedCount: number | null,
dateFrom: string,
dateTo: string
): string[] {
const warnings: string[] = [];
if (sessionCount === null) {
warnings.push('Plenary session count unavailable from EP API.');
}
Iif (adoptedCount === null) {
warnings.push('Adopted texts count unavailable from EP API.');
}
warnings.push('Average turnout is always zero; EP API does not provide turnout data.');
warnings.push('Political group alignment requires the compare_political_groups tool for detailed analysis.');
if (!isFullYearRange(dateFrom, dateTo)) {
warnings.push(
'Plenary session count and adopted texts count are based on the full year derived from dateFrom; '
+ 'partial-year date ranges are not applied to these EP API calls.'
);
}
return warnings;
}
/** Check whether a date range spans an entire calendar year (Jan 1 – Dec 31). */
function isFullYearRange(dateFrom: string, dateTo: string): boolean {
return dateFrom.endsWith('-01-01') && dateTo.endsWith('-12-31')
&& dateFrom.substring(0, 4) === dateTo.substring(0, 4);
}
/**
* Throw a ToolError when all EP API data fetches failed.
* This prevents returning a successful-looking report with all-zero statistics,
* which could mislead AI agents into treating empty data as valid.
*/
function throwIfAllDataUnavailable(
reportType: string,
fetchResults: unknown[]
): void {
if (fetchResults.every((r) => r === null)) {
throw new ToolError({
toolName: 'generate_report',
operation: 'generateReport',
message: `EP API data unavailable for ${reportType} report — all upstream data sources failed`,
isRetryable: true,
errorCode: 'UPSTREAM_503',
httpStatus: 503,
});
}
}
/** Build data quality warnings for legislation progress report */
function buildLegislationWarnings(
procedureCount: number | null,
completedCount: number | null,
ongoingCount: number | null
): string[] {
const warnings: string[] = [];
Iif (procedureCount === null) {
warnings.push('Legislative procedures count unavailable from EP API.');
}
if (completedCount === null) {
warnings.push('Completed procedures (adopted texts) count unavailable from EP API.');
}
if (ongoingCount === null) {
warnings.push('Ongoing procedures count could not be calculated due to missing data.');
}
warnings.push('Counts are lower bounds based on first page of API results (limit 100).');
warnings.push('Ongoing count is estimated as total procedures minus adopted texts.');
return warnings;
}
/**
* Generate MEP activity report using real EP API data
* Cyclomatic complexity: 2
*/
export async function generateMEPActivityReport(
params: z.infer<typeof GenerateReportSchema>
): Promise<Report> {
const mep = await fetchMEPDetails(params.subjectId);
const data = extractMEPData(params, mep);
const questionsSubmitted = await fetchQuestionCount(params.subjectId);
Eif (params.subjectId !== undefined) {
throwIfAllDataUnavailable('MEP_ACTIVITY', [mep, questionsSubmitted]);
}
const warnings = buildMEPWarnings(mep, questionsSubmitted, params.subjectId !== undefined);
return {
reportType: 'MEP_ACTIVITY',
subject: data.mepName,
period: {
from: data.dateFrom,
to: data.dateTo
},
generatedAt: new Date().toISOString(),
summary: `Activity report for ${data.mepName} covering the period from ${data.dateFrom} to ${data.dateTo}.`,
sections: [
createVotingSection(data.totalVotes, mep),
createCommitteeSection(data.committeesLength, mep),
createParliamentaryQuestionsSection(questionsSubmitted)
],
statistics: {
totalVotes: mep?.votingStatistics?.totalVotes ?? 0,
attendanceRate: mep?.votingStatistics?.attendanceRate ?? 0,
questionsSubmitted: questionsSubmitted ?? 0,
reportsAuthored: 0 // EP API does not provide per-MEP report authorship counts
},
recommendations: [
'Continue active participation in committee work',
'Increase engagement with constituents',
'Consider authoring legislation on key policy areas'
],
dataQualityWarnings: warnings
};
}
/**
* Generate committee performance report using real EP API data
* Cyclomatic complexity: 2
*/
export async function generateCommitteePerformanceReport(
params: z.infer<typeof GenerateReportSchema>
): Promise<Report> {
const committee = await fetchCommitteeInfo(params.subjectId);
const committeeName = committee?.name ?? 'Unknown Committee';
const dateFrom = params.dateFrom ?? '2024-01-01';
const dateTo = params.dateTo ?? '2024-12-31';
const membersLength = committee?.members.length ?? 0;
const year = parseInt(dateFrom.substring(0, 4), 10);
// Parliament-wide counts (not filtered by committee)
const documentsProduced = await fetchDocumentCount(year);
const reportsProduced = await fetchAdoptedTextCount(year);
throwIfAllDataUnavailable('COMMITTEE_PERFORMANCE', [committee, documentsProduced, reportsProduced]);
const warnings = buildCommitteeWarnings(committee, documentsProduced, reportsProduced, params.subjectId !== undefined);
return {
reportType: 'COMMITTEE_PERFORMANCE',
subject: committeeName,
period: {
from: dateFrom,
to: dateTo
},
generatedAt: new Date().toISOString(),
summary: `Performance report for ${committeeName} committee. Note: document and adopted text counts are parliament-wide lower bounds (first page, not filtered by committee).`,
sections: [
createMeetingActivitySection(0),
createLegislativeOutputSection(reportsProduced, documentsProduced),
createMemberParticipationSection(membersLength)
],
statistics: {
meetingsHeld: 0, // EP API does not provide committee-specific meeting counts
reportsProduced: reportsProduced ?? 0,
opinionsIssued: 0, // EP API does not provide committee-specific opinion counts
averageAttendance: 0, // EP API does not provide attendance data
memberCount: membersLength
},
dataQualityWarnings: warnings
};
}
/**
* Generate voting statistics report using real EP API data
* Cyclomatic complexity: 1
*/
export async function generateVotingStatisticsReport(
params: z.infer<typeof GenerateReportSchema>
): Promise<Report> {
const dateFrom = params.dateFrom ?? '2024-01-01';
const dateTo = params.dateTo ?? '2024-12-31';
const year = parseInt(dateFrom.substring(0, 4), 10);
const sessionCount = await fetchSessionCount(dateFrom, dateTo);
const adoptedCount = await fetchAdoptedTextCount(year);
throwIfAllDataUnavailable('VOTING_STATISTICS', [sessionCount, adoptedCount]);
const warnings = buildVotingWarnings(sessionCount, adoptedCount, dateFrom, dateTo);
return {
reportType: 'VOTING_STATISTICS',
subject: 'Parliament-wide Voting Analysis',
period: {
from: dateFrom,
to: dateTo
},
generatedAt: new Date().toISOString(),
summary: 'Comprehensive voting statistics for European Parliament based on EP Open Data.',
sections: [
createOverallVotingSection(sessionCount),
createAdoptionRatesSection(adoptedCount),
createPoliticalGroupSection()
],
statistics: {
totalSessions: sessionCount ?? 0,
adopted: adoptedCount ?? 0,
averageTurnout: 0 // EP API does not provide turnout data
},
dataQualityWarnings: warnings
};
}
/**
* Generate legislation progress report using real EP API data
* Cyclomatic complexity: 1
*/
export async function generateLegislationProgressReport(
params: z.infer<typeof GenerateReportSchema>
): Promise<Report> {
const dateFrom = params.dateFrom ?? '2024-01-01';
const dateTo = params.dateTo ?? '2024-12-31';
const year = parseInt(dateFrom.substring(0, 4), 10);
const procedureCount = await fetchProcedureCount(year);
const completedCount = await fetchAdoptedTextCount(year);
throwIfAllDataUnavailable('LEGISLATION_PROGRESS', [procedureCount, completedCount]);
const ongoingCount = (procedureCount !== null && completedCount !== null)
? Math.max(0, procedureCount - completedCount)
: null;
const warnings = buildLegislationWarnings(procedureCount, completedCount, ongoingCount);
return {
reportType: 'LEGISLATION_PROGRESS',
subject: 'Legislative Activity Overview',
period: {
from: dateFrom,
to: dateTo
},
generatedAt: new Date().toISOString(),
summary: 'Progress report on legislative procedures based on EP Open Data (lower bounds, first page).',
sections: [
createNewProposalsSection(procedureCount),
createCompletedProceduresSection(completedCount),
createOngoingProceduresSection(ongoingCount)
],
statistics: {
totalProcedures: procedureCount ?? 0,
completed: completedCount ?? 0,
ongoing: ongoingCount ?? 0
},
dataQualityWarnings: warnings
};
}
|