-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLogAdapter.js
More file actions
187 lines (166 loc) · 5.17 KB
/
Copy pathLogAdapter.js
File metadata and controls
187 lines (166 loc) · 5.17 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* 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.
*/
/**
* LogAdapter
*/
class LogAdapter {
/**
* Constructor
*/
constructor() {
/**
* @type {RunAdapter|null}
*/
this.runAdapter = null;
/**
* @type {AttachmentAdapter|null}
*/
this.attachmentAdapter = null;
/**
* @typedef {EnvironmentAdapter|null}
*/
this.environmentAdapter = null;
/**
* @type {TagAdapter|null}
*/
this.tagAdapter = null;
/**
* @type {LhcFillAdapter|null}
*/
this.lhcFillAdapter = null;
/**
* @type {UserAdapter|null}
*/
this.userAdapter = null;
this.toEntity = this.toEntity.bind(this);
this.toSummary = this.toSummary.bind(this);
this.toDatabase = this.toDatabase.bind(this);
}
/**
* Converts the given database object to an entity object.
*
* @param {SequelizeLog} databaseObject Object to convert.
* @returns {Log} Converted entity object.
*/
toEntity(databaseObject) {
const {
id,
title,
text,
user,
createdAt,
origin,
subtype,
rootLogId,
parentLogId,
replies,
tags: sequelizeTags,
runs,
lhcFills: sequelizeLhcFills,
attachments: sequelizeAttachments,
environments: sequelizeEnvironments,
} = databaseObject;
const minifiedRuns = (runs || []).map(this.runAdapter.toMinifiedEntity);
const tags = (sequelizeTags || []).map(this.tagAdapter.toEntity);
const attachments = (sequelizeAttachments || []).map(this.attachmentAdapter.toEntity);
const lhcFills = (sequelizeLhcFills || []).map(this.lhcFillAdapter.toEntity);
const environments = (sequelizeEnvironments || []).map(this.environmentAdapter.toEntity);
const entityObject = {
id,
title,
text,
author: this.userAdapter.toEntity(user),
createdAt: new Date(createdAt).getTime(),
origin,
subtype,
rootLogId: rootLogId || id,
parentLogId: parentLogId || id,
runs: minifiedRuns,
tags,
lhcFills,
attachments,
environments,
};
if (replies) {
entityObject.replies = replies;
}
return entityObject;
}
/**
* Adapts the log entity to a minified version.
* @param {SequelizeLog} databaseObject Object to convert.
* @returns {MinifiedLog} minified version of the log entity
*/
toSummary(databaseObject) {
const {
id,
title,
text,
user,
createdAt,
rootLogId,
parentLogId,
replies,
tags = [],
runs = [],
lhcFills = [],
attachments = [],
environments = [],
} = databaseObject;
const entityObject = {
id,
title,
text,
author: user ? this.userAdapter.toNameOnly(user) : { name: 'Anonymous' },
createdAt: new Date(createdAt).getTime(),
rootLogId: rootLogId || id,
parentLogId: parentLogId || id,
runs: runs.map(this.runAdapter.toMinifiedEntity),
tags: tags.map(this.tagAdapter.toSummary),
lhcFills: lhcFills.map(({ fillNumber }) => ({ fillNumber })),
attachments: attachments.map(({ id }) => ({ id })),
environments: environments.map(({ id }) => ({ id })),
};
if (replies) {
entityObject.replies = replies;
}
return entityObject;
}
/**
* Converts the given entity object to a database object.
*
* @param {Partial<Log>} entityObject Object to convert.
* @returns {Partial<SequelizeLog>} Converted database object.
*/
toDatabase(entityObject) {
const databaseObject = {
id: entityObject.id,
userId: entityObject.userId,
title: entityObject.title,
text: entityObject.text.replace(/\\n/g, '\n'),
tags: entityObject.tags,
environments: entityObject.environments,
lhcFills: entityObject.lhcFills,
subtype: 'run',
origin: 'process',
};
if (entityObject.rootLogId) {
databaseObject.rootLogId = entityObject.rootLogId;
}
if (entityObject.parentLogId) {
databaseObject.parentLogId = entityObject.parentLogId;
}
return databaseObject;
}
}
module.exports = LogAdapter;