[WIP] initial implementation of logService in server.ts, add a log action in upload dois

This commit is contained in:
argirok 2023-11-28 18:43:42 +02:00
parent 81b175a5ea
commit 00953e0382
3 changed files with 52 additions and 3 deletions

View File

@ -8,7 +8,11 @@ import { existsSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import { AppServerModule } from './src/main.server'; import { AppServerModule } from './src/main.server';
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens"; import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
var bodyParser = require('body-parser')
var jsonParser = bodyParser.json()
import * as fs from 'fs';
import {readFileSync} from "fs";
import {properties} from "./src/environments/environment";
// The Express app is exported so that it can be used by serverless Functions. // The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express { export function app(): express.Express {
const server = express(); const server = express();
@ -28,6 +32,46 @@ export function app(): express.Express {
// Example Express Rest API endpoints // Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { }); // server.get('/api/**', (req, res) => { });
// Serve static files from /browser // Serve static files from /browser
server.get('/log/', (req, res) => {
res.status(200).json(getLogs(null));
});
server.get('/log/:id', (req, res) => {
res.status(200).json(getLogs(req.params['id']));
});
function getLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
}
server.get('/logFiles', (req, res) => {
fs.readdir(properties.logFilesPath, (err, files) => {
res.status(200).json(files);
});
});
server.get('/prettyLog', (req, res) => {
const file = readFileSync('/var/log/irish-log/foo.txt', 'utf-8');
let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]");
let prettyLog = "";
for(let i = logs.length -1; i>=0; i--){
prettyLog += "On " + (logs[i].date?logs[i].date:"") + " " /*+ logs[i].action + " "*/ + logs[i].message+ "<br>";
}
res.status(200).send( prettyLog);
});
server.post('/logAction', jsonParser,(req, res) => {
let log = req.body;
log.date = new Date();
fs.appendFile('/var/log/irish-log/foo.txt', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
if (err) {
return console.error(err);
}
});
res.status(200).send({
code: 200,
message: 'action received!'
});
});
server.get('*.*', express.static(distFolder, { server.get('*.*', express.static(distFolder, {
maxAge: '1y' maxAge: '1y'
})); }));

View File

@ -7,6 +7,7 @@ import {properties} from "../../environments/environment";
import {SearchResearchResultsService} from "../openaireLibrary/services/searchResearchResults.service"; import {SearchResearchResultsService} from "../openaireLibrary/services/searchResearchResults.service";
import {map} from "rxjs/operators"; import {map} from "rxjs/operators";
import {Filter} from "../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class"; import {Filter} from "../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class";
import {LogService} from "../openaireLibrary/utils/log/log.service";
@Component({ @Component({
selector: 'upload-dois', selector: 'upload-dois',
@ -68,7 +69,7 @@ export class UploadDoisComponent implements OnInit {
keyword = ""; keyword = "";
loading = false; loading = false;
showFound = true; showFound = true;
constructor(private _searchResearchResultsService: SearchResearchResultsService) { constructor(private _searchResearchResultsService: SearchResearchResultsService, private _logService: LogService) {
} }
@ -157,6 +158,9 @@ export class UploadDoisComponent implements OnInit {
} }
this.enableUpload = true; this.enableUpload = true;
if(properties.logFilesService) {
this.subscriptions.push(this._logService.logUploadDOIs(properties, this.allIds.length).subscribe(res => { }));
}
this.fetchAllResults(); this.fetchAllResults();
}, (error) => { }, (error) => {
this.enableUpload = true; this.enableUpload = true;

View File

@ -8,12 +8,13 @@ import {SearchInputModule} from "../openaireLibrary/sharedComponents/search-inpu
import {IconsModule} from "../openaireLibrary/utils/icons/icons.module"; import {IconsModule} from "../openaireLibrary/utils/icons/icons.module";
import {DropdownFilterModule} from "../openaireLibrary/utils/dropdown-filter/dropdown-filter.module"; import {DropdownFilterModule} from "../openaireLibrary/utils/dropdown-filter/dropdown-filter.module";
import {SearchFilterModule} from "../openaireLibrary/searchPages/searchUtils/searchFilter.module"; import {SearchFilterModule} from "../openaireLibrary/searchPages/searchUtils/searchFilter.module";
import {LogServiceModule} from "../openaireLibrary/utils/log/LogService.module";
@NgModule({ @NgModule({
declarations: [UploadDoisComponent], declarations: [UploadDoisComponent],
imports: [ imports: [
CommonModule, UploadDoisRoutingModule, SearchResearchResultsServiceModule, PagingModule, SearchInputModule, IconsModule, DropdownFilterModule, SearchFilterModule CommonModule, UploadDoisRoutingModule, SearchResearchResultsServiceModule, PagingModule, SearchInputModule, IconsModule, DropdownFilterModule, SearchFilterModule, LogServiceModule
], ],
}) })