-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathProfileService.js
More file actions
142 lines (135 loc) · 5.09 KB
/
Copy pathProfileService.js
File metadata and controls
142 lines (135 loc) · 5.09 KB
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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const logger = require('@aliceo2/web-ui').LogManager
.getLogger(`${process.env.npm_config_log_label ?? 'ilg'}/profile`);
/**
* Gateway for all Infologger profile calls
*/
class ProfileService {
/**
* Initialize connector
* @param {JsonFileConnector} jsonDb - JsonFileConnector instance
*/
constructor(jsonDb) {
// TODO: Connect SQLite connector
this.jsonDb = jsonDb;
this.defaultUserConfig = {
date: { size: 'cell-m', visible: false },
time: { size: 'cell-m', visible: true },
hostname: { size: 'cell-m', visible: true },
rolename: { size: 'cell-m', visible: false },
pid: { size: 'cell-s', visible: false },
username: { size: 'cell-m', visible: false },
system: { size: 'cell-s', visible: true },
facility: { size: 'cell-m', visible: true },
detector: { size: 'cell-s', visible: true },
partition: { size: 'cell-m', visible: true },
run: { size: 'cell-s', visible: true },
errcode: { size: 'cell-s', visible: false },
errline: { size: 'cell-s', visible: false },
errsource: { size: 'cell-m', visible: false },
message: { size: 'cell-xl', visible: true },
};
this.defaultCriterias = {
timestamp: { since: '', until: '' },
hostname: { match: '', exclude: '' },
rolename: { match: '', exclude: '' },
pid: { match: '', exclude: '' },
username: { match: '', exclude: '' },
system: { match: '', exclude: '' },
facility: { match: '', exclude: '' },
detector: { match: '', exclude: '' },
partition: { match: '', exclude: '' },
run: { match: '', exclude: '' },
errcode: { match: '', exclude: '' },
errline: { match: '', exclude: '' },
errsource: { match: '', exclude: '' },
message: { match: '', exclude: '' },
severity: { in: 'I W E F' },
level: { max: 1 },
};
}
/**
* Method which handles the request for a profile, if profile doesn't exist, send back default
* @param {Request} req - HTTP Request object
* @param {Response} res - HTTP Response object
*/
async getProfile(req, res) {
const { profile } = req.query;
if (profile.trim()) {
logger.info(`User profile ${profile} fetched successfully`);
res.status(200).json({ user: profile,
content:
{ colsHeader: this.defaultUserConfig, criterias: this.defaultCriterias } });
} else {
logger.warn(`User profile ${profile} not found, sending default instead`);
res.status(200).json({ user: 'default',
content:
{ colsHeader: this.defaultUserConfig, criterias: this.defaultCriterias } });
}
}
/**
* Method which handles the request for the user profile
* @param {Request} req - HTTP Request object
* @param {Response} res - HTTP Response object
*/
async getUserProfile(req, res) {
const user = parseInt(req.query.user, 10);
this.jsonDb.getProfileByUsername(user).then((profile) => {
if (profile) {
res.status(200).json(profile);
} else {
res.status(200).json({ user: 'default', content: { colsHeader: this.defaultUserConfig } });
}
})
.catch((err) => this.handleError(res, err));
}
/**
* Method which handles the request for saving the user profile
* @param {Request} req - HTTP Request object
* @param {Response} res - HTTP Response object
*/
async saveUserProfile(req, res) {
const user = parseInt(req.body.user, 10);
const { content } = req.body;
this.jsonDb.getProfileByUsername(user).then((profile) => {
if (!profile) {
this.jsonDb.createNewProfile(user, content)
.then((newProfile) => {
if (newProfile) {
res.status(200).json({ message: 'New profile was successfully created and saved' });
} else {
res.status(500).json({ message: 'Profile was not found and a new profile could not be created' });
}
})
.catch((err) => this.handleError(res, err));
} else {
this.jsonDb.updateProfile(user, content)
.then(() => res.status(200).json({ message: 'Profile updates were saved successfully' }))
.catch((err) => this.handleError(res, err));
}
}).catch((err) => this.handleError(res, err));
}
/**
* Catch all HTTP errors
* @param {Response} res - HTTP Response object
* @param {Error} error - Error object
* @param {number} status - HTTP status code
*/
handleError(res, error, status = 500) {
logger.trace(error);
res.status(status).json({ message: error.message });
}
}
module.exports = ProfileService;