- HomeController.ts: addes api method for showing number of publications per month for given year
All checks were successful
CI Pipeline / japa-tests (push) Successful in 50s
All checks were successful
CI Pipeline / japa-tests (push) Successful in 50s
- adapted command ValidateChecksum.ts: on published files are checked. better information logging - better LineChart.vue component: showing real statistics - start/routes/apu.ts: added Route.get('/statistic/:year', 'HomeController.findPublicationsPerMonth');
This commit is contained in:
parent
8cef7390d7
commit
68928b5e07
10 changed files with 260 additions and 74 deletions
|
@ -53,4 +53,91 @@ export default class HomeController {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async findPublicationsPerMonth({ response }: HttpContextContract) {
|
||||
const serverState = 'published';
|
||||
// const year = params.year;
|
||||
// const from = parseInt(year);
|
||||
try {
|
||||
|
||||
// const datasets = await Database.from('documents as doc')
|
||||
// .select([Database.raw(`date_part('month', server_date_published) as pub_month`), Database.raw('COUNT(*) as count')])
|
||||
// .where('server_state', serverState)
|
||||
// .innerJoin('link_documents_persons as ba', 'doc.id', 'ba.document_id')
|
||||
// .andWhereRaw(`date_part('year', server_date_published) = ?`, [from])
|
||||
// .groupBy('pub_month');
|
||||
// // .orderBy('server_date_published');
|
||||
|
||||
const years = [2021, 2022, 2023]; // Add the second year
|
||||
|
||||
const result = await Database.from('documents as doc')
|
||||
.select([
|
||||
Database.raw(`date_part('year', server_date_published) as pub_year`),
|
||||
Database.raw(`date_part('month', server_date_published) as pub_month`),
|
||||
Database.raw('COUNT(*) as count'),
|
||||
])
|
||||
.where('server_state', serverState)
|
||||
// .innerJoin('link_documents_persons as ba', 'doc.id', 'ba.document_id')
|
||||
// .whereIn('pub_year', years) // Filter by both years
|
||||
.whereRaw(`date_part('year', server_date_published) IN (${years.join(',')})`) // Filter by both years
|
||||
.groupBy('pub_year', 'pub_month')
|
||||
.orderBy('pub_year', 'asc')
|
||||
.orderBy('pub_month', 'asc');
|
||||
|
||||
const labels = Array.from({ length: 12 }, (_, i) => i + 1); // Assuming 12 months
|
||||
|
||||
const inputDatasets: Map<string, ChartDataset> = result.reduce((acc, item) => {
|
||||
const { pub_year, pub_month, count } = item;
|
||||
|
||||
if (!acc[pub_year]) {
|
||||
acc[pub_year] = {
|
||||
data: Array.from({ length: 12 }).fill(0),
|
||||
label: pub_year.toString(),
|
||||
borderColor: this.getRandomHexColor, // pub_year === 2022 ? '#3e95cd' : '#8e5ea2',
|
||||
fill: false,
|
||||
};
|
||||
}
|
||||
|
||||
acc[pub_year].data[pub_month - 1] = parseInt(count);
|
||||
|
||||
return acc ;
|
||||
}, {});
|
||||
|
||||
const outputDatasets = Object.entries(inputDatasets).map(([year, data]) => ({
|
||||
data: data.data,
|
||||
label: year,
|
||||
borderColor: data.borderColor,
|
||||
fill: data.fill
|
||||
}));
|
||||
|
||||
const data = {
|
||||
labels: labels,
|
||||
datasets: outputDatasets,
|
||||
};
|
||||
|
||||
return response.json(data);
|
||||
} catch (error) {
|
||||
return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
|
||||
message: error.message || 'Some error occurred while retrieving datasets.',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private getRandomHexColor() {
|
||||
const letters = '0123456789ABCDEF';
|
||||
let color = '#';
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
color += letters[Math.floor(Math.random() * 16)];
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
}
|
||||
interface ChartDataset {
|
||||
data: Array<number>;
|
||||
label: string;
|
||||
borderColor: string;
|
||||
fill: boolean;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue