|
| 1 | +import {generateReportFromData} from '../helpers.js'; |
| 2 | +import { querySudo as query } from '@lblod/mu-auth-sudo'; |
| 3 | +import { getToday } from "./utils/date.js"; |
| 4 | +import { MONTHS } from "./utils/constants.js"; |
| 5 | + |
| 6 | +export default { |
| 7 | + cronPattern: "0 17 * * * *", |
| 8 | + name: "dailyLoginErrorReport", |
| 9 | + execute: async () => { |
| 10 | + const { day, month, year } = getToday(); |
| 11 | + const metadata = { |
| 12 | + title: `Login errors ${day} ${MONTHS[month]} ${year}`, |
| 13 | + description: "Dagelijks overzicht van errors gelogged door de login service", |
| 14 | + filePrefix: `login-errors-${day}-${month +1 }-${year}`, |
| 15 | + }; |
| 16 | + const data = await fetchErrorLogs(); |
| 17 | + await generateReportFromData( |
| 18 | + data, |
| 19 | + [ |
| 20 | + "message", |
| 21 | + "date", |
| 22 | + "specificInfo", |
| 23 | + "type" |
| 24 | + ], |
| 25 | + metadata |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function fetchErrorLogs() { |
| 31 | + const queryString = ` |
| 32 | + PREFIX rlog: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/rlog#> |
| 33 | + PREFIX dct: <http://purl.org/dc/terms/> |
| 34 | + PREFIX ext: <http://mu.semte.ch/vocabularies/ext/> |
| 35 | + SELECT ?message ?date ?specificInfo ?type |
| 36 | + WHERE { |
| 37 | + GRAPH <http://mu.semte.ch/graphs/login-error-logs> { |
| 38 | + ?log a rlog:Entry ; |
| 39 | + rlog:className ?type ; |
| 40 | + rlog:message ?message; |
| 41 | + rlog:date ?date; |
| 42 | + rlog:level ?level; |
| 43 | + ext:specificInformation ?specificInfo. |
| 44 | + FILTER (?date >= xsd:dateTime(NOW() - "P1D"^^xsd:duration)) |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | +`; |
| 49 | + const queryResponse = await query(queryString); |
| 50 | + const data = queryResponse.results.bindings.map((entry) => { |
| 51 | + return { |
| 52 | + message: entry.message.value, |
| 53 | + date: entry.date.value, |
| 54 | + specificInfo: entry.specificInfo.value, |
| 55 | + type: entry.type.value |
| 56 | + }; |
| 57 | + }); |
| 58 | + return data; |
| 59 | +} |
0 commit comments