Public logs update view and methods

This commit is contained in:
argirok 2023-12-19 17:22:04 +02:00
parent e9938bc61d
commit 278d3cf74f
4 changed files with 74 additions and 149 deletions

View File

@ -32,44 +32,24 @@ export function app(): express.Express {
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// 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']));
});
server.get('/logFiles', (req, res) => {
fs.readdir(properties.logFilesPath, (err, files) => {
res.status(200).json(files);
});
});
server.get('/prettyLog', (req, res) => {
res.status(200).send( getPrettyLogs(null));
});
server.get('/prettyLog/:id', (req, res) => {
res.status(200).send( getPrettyLogs(req.params['id']));
server.get('/public-logs/:year/:month', (req, res) => {
let today = new Date();
let filename = getFileName(req.params['year'], req.params['month'])
res.setHeader('content-disposition','attachment; filename='+filename);
let file = " ";
try {
file = readFileSync(properties.logFilesPath + filename, 'utf-8');
}catch (e){}
res.status(200).send( file);
});
server.post('/logAction', jsonParser,(req, res) => {
let log = req.body;
log.date = new Date();
fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
if (err) {
return console.error(err);
}
});
res.status(200).send({
code: 200,
message: 'action received!'
});
});
server.get('/testLogAction', jsonParser,(req, res) => {
let log = {message:"log", date: new Date()};
fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
let filename = getFileName(log.date.getFullYear(), log.date.getMonth()+1);
fs.appendFile(properties.logFilesPath + filename, formatPrettyLog(log)+ "\n", 'utf8', function(err) {
if (err) {
console.error(err);
return console.error(err);
}
});
@ -114,18 +94,12 @@ function run(): void {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
function getLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
function getFileName(year, month){
return 'actions' + (year && month ? "_" + year + "_" + month : "")+".log";
}
function getPrettyLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log'+ (id?"."+id:""), '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 ? formatDateAndTime(logs[i].date) : "") + " " /*+ logs[i].action + " "*/ + logs[i].message + "<br>";
}
return prettyLog + getPrevNextLog(id?id:0);
function formatPrettyLog(log){
return "On " + (log.date ? formatDateAndTime(log.date) : "") + " " /*+ logs[i].action + " "*/ + log.message + "<br>";
}
function formatDateAndTime(dateStr){
let date = new Date(dateStr);
@ -139,18 +113,6 @@ function formatDateAndTime(dateStr){
}
function getPrevNextLog(id= 0){
const files = fs.readdirSync(properties.logFilesPath);
let id_next:number = +id+1;
let id_prev:number = id-1;
let prev = (id>1 && files.indexOf("actions.log."+id_prev )!=-1)? '<a style="margin-left: 5px;" href="'+properties.logServiceUrl +
'prettyLog/' + id_prev + '">Prev</a>':"";
prev = (id ==1 && files.indexOf("actions.log")!=-1)? '<a style="margin-left: 5px;" href="'+properties.logServiceUrl +
'prettyLog">Prev</a>':prev;
let next = (id>=0 && files.indexOf("actions.log."+ id_next )!=-1)? ('<a style="margin-left: 5px;" href="'+properties.logServiceUrl +'prettyLog/' +id_next + '">Next</a>'):"";
return "<br>" + prev + next;
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.

View File

@ -1,16 +0,0 @@
import {NgModule} from "@angular/core";
import {RouterModule} from "@angular/router";
import {PublicLogsComponent} from "./public-logs.component";
@NgModule({
imports: [
RouterModule.forChild([
{
path: '',
component: PublicLogsComponent
}
])
]
})
export class PublicLogsRoutingModule {
}

View File

@ -13,50 +13,58 @@ import {CookieLawService} from "../openaireLibrary/sharedComponents/cookie-law/c
template: `
<div class="uk-container uk-container-large uk-margin-top">
<h3>Public logs</h3>
<ng-container *ngIf="!isConsent">
<ul class="uk-tab " uk-switcher>
<li><a href="#">Linking</a></li>
<li><a href="#">ORCID Claim</a></li>
<li><a href="#">Upload DOIs</a></li>
<li><a href="#">Dashboard</a></li>
<!-- <li><a href="#">Linking</a></li>
<li><a href="#">ORCID Claim</a></li>
<li><a href="#">Upload DOIs</a></li>-->
<li><a href="#">OpenOrgs</a></li>
<li><a href="#"> Web stats from Matomo</a></li>
</ul>
<ul class="uk-switcher uk-margin">
<li>
<ng-container *ngTemplateOutlet="formattedLogs; context: { logs: claimsLogs}"></ng-container>
<a [href]="properties.logServiceUrl + 'prettyLog'" target="_blank">View all logs</a> <a [href]="properties.logServiceUrl + 'log'" target="_blank">[json]</a>
</li>
<!-- <ng-container *ngTemplateOutlet="formattedLogs; context: { logs: claimsLogs}"></ng-container>-->
<div>
National Open Access Monitor - Ireland, creates public logs when users uploas dois, claim links to research outputs or claim a research output to their ORCID profile.<br>
</div>
<div>Get monthly logs for Dashboard:</div>
<ul class="uk-list">
<ng-container *ngFor="let year of years ">
<ng-container *ngFor="let month of months ">
<ng-container *ngIf="(year == today.getFullYear() && month <= today.getMonth()+1 && month >= startDate.getMonth()+1) || (year >startDate.getFullYear() || year < today.getFullYear()) ">
<li>
<a [href]="properties.logServiceUrl + 'public-logs/' + year + '/' + month " target="_blank"> {{month}}/{{year}}</a>
</li>
</ng-container>
</ng-container>
</ng-container>
</ul> </li>
<li>
<ng-container *ngTemplateOutlet="formattedLogs; context: { logs: orcidClaimsLogs}"></ng-container>
<a [href]="properties.logServiceUrl + 'prettyLog'" target="_blank">View all logs</a> <a [href]="properties.logServiceUrl + 'log'" target="_blank">[json]</a>
<div>Get monthly logs for OpenOrgs:</div>
<ul class="uk-list">
<ng-container *ngFor="let year of years ">
<ng-container *ngFor="let month of months ">
<ng-container *ngIf="(year == today.getFullYear() && month <= today.getMonth()+1 && month >= startDate.getMonth()+1) || (year >startDate.getFullYear() || year < today.getFullYear()) "> <li>
<li>
<a [href]="properties.openOrgsUrl + '/public-api/logs/' + year + '/' + month+ '/IE' " target="_blank"> {{month}}/{{year}}</a>
</li>
</ng-container>
</ng-container>
</ng-container>
</ul>
</li>
<li>
<ng-container *ngTemplateOutlet="formattedLogs; context: { logs: uploadDoisLogs}"></ng-container>
<a [href]="properties.logServiceUrl + 'prettyLog'" target="_blank">View all logs</a> <a [href]="properties.logServiceUrl + 'log'" target="_blank">[json]</a>
</li>
<li>Bazinga!
<br>
<a href="https://beta.orgs.openaire.eu/public-api/logs/IE/0/50" target="_blank"> View OpenOrgs logs</a>
</li>
<li>Bazinga!
<br>
<a target="_blank" href="https://beta.analytics.openaire.eu/index.php?apiAction=get&apiModule=API&date=2022-01-01,2023-12-31&expanded=1&filter_limit=-1&force_api_session=1&format=JSON&format_metrics=1&idSite=407&method=API.getProcessedReport&module=API&period=month&token_auth=anonymous"> View Matomo logs</a>
<li>
<div class="uk-alert uk-alert-warning">Coming soon!</div>
<!-- <a target="_blank" href="https://beta.analytics.openaire.eu/index.php?apiAction=get&apiModule=API&date=2022-01-01,2023-12-31&expanded=1&filter_limit=-1&force_api_session=1&format=JSON&format_metrics=1&idSite=407&method=API.getProcessedReport&module=API&period=month&token_auth=anonymous"> View Matomo logs</a>-->
</li>
</ul>
</ng-container>
<ng-container *ngIf="isConsent">
<div class="uk-alert uk-alert-warning">
National Open Access Monitor - Ireland, creates public logs for user actions.<br>
By using the National Open Access Monitor - Ireland portal you consent to log your actions <a
routerLink="/public-logs"> View Public logs <span class="uk-icon">
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="chevron-right"
ratio="1"><polyline fill="none" stroke="#000" stroke-width="1.03"
points="7 4 13 10 7 16"></polyline></svg>
</span></a>
<button class="uk-button uk-button-primary" (click)="redirect()">Proceed</button>
</div>
</ng-container>
</div>
<ng-template #formattedLogs let-logs="logs">
<div *ngIf="logs && logs.length > 0" class="uk-text-meta "> Viewing last {{logs.length}} actions.</div>
@ -70,12 +78,12 @@ import {CookieLawService} from "../openaireLibrary/sharedComponents/cookie-law/c
`
})
export class PublicLogsComponent extends BaseComponent implements OnInit {
allLogs;
uploadDoisLogs;
claimsLogs;
orcidClaimsLogs;
isConsent = false;
redirectUrl = null;
startDate = new Date();
today = new Date();
months = [];
years = [];
constructor(protected _router: Router,
protected _route: ActivatedRoute,
protected seoService: SEOService,
@ -85,48 +93,14 @@ export class PublicLogsComponent extends BaseComponent implements OnInit {
private _logService:LogService,
private userManagementsService: UserManagementService, private cookieLawService: CookieLawService) {
super();
this.startDate.setFullYear(2023,11,1)
this.months = Array.from({ length:12 }, (_, index) => 12 - index);
this.years = Array.from({ length:this.today.getFullYear() + 1 - this.startDate.getFullYear() }, (_, index) => this.today.getFullYear() - index);
}
ngOnInit() {
this.title = 'Public logs';
this.description = 'Public logs of user actions in OA monitor - Ireland';
this.setMetadata();
this.subscriptions.push(this._route.queryParams.subscribe(params => {
this.isConsent = params && params['redirectUrl'];
this.redirectUrl = params['redirectUrl'];
if(!this.isConsent){
this.subscriptions.push(this._logService.getLogs(this.properties).subscribe(logs => {
this.allLogs = logs.slice(logs.length -10 ,logs.length);
this.uploadDoisLogs = [];
this.claimsLogs = [];
this.orcidClaimsLogs = [];
for(let i=logs.length; i--; i==0){
let log = logs[i];
log.date = new Date(log.date);
if(log.action == 'upload-dois' && this.uploadDoisLogs.length <10){
this.uploadDoisLogs.push(log);
}else if(log.action == 'link' && this.claimsLogs.length <10){
this.claimsLogs.push(log);
} else if(log.action == 'orcid-link' && this.orcidClaimsLogs.length <10){
this.orcidClaimsLogs.push(log);
}
}
}));
}
}));
}
redirect() {
//if parameters are not read yet, force them to use the function parameter
this.cookieLawService.storeCookie("logActions-NOAMIreland")
if (this.redirectUrl && this.redirectUrl != "") {
this.redirectUrl = decodeURIComponent(this.redirectUrl);
this.userManagementsService.setRedirectUrl(this.redirectUrl);
this._router.navigate(['/reload']);
}
}
}

View File

@ -1,15 +1,20 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {PublicLogsComponent} from "./public-logs.component";
import {PublicLogsRoutingModule} from "./public-logs-routing.module";
import {LogServiceModule} from "../openaireLibrary/utils/log/LogService.module";
import {RouterModule} from "@angular/router";
import {Route, RouterModule} from "@angular/router";
const routes: Route[] = [
{
path: '', component: PublicLogsComponent
}
];
@NgModule({
declarations: [PublicLogsComponent],
imports: [
CommonModule, PublicLogsRoutingModule, LogServiceModule, RouterModule
CommonModule, RouterModule.forChild(routes), LogServiceModule, RouterModule
],
exports: [PublicLogsComponent]
})