-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathparseInputData.ts
277 lines (251 loc) · 7.81 KB
/
parseInputData.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { compact, groupBy, min, sortBy } from 'lodash';
import objectHash from 'object-hash';
import { canResourceHaveChildren } from '../../Frontend/util/can-resource-have-children';
import {
Attributions,
AttributionsToResources,
BaseUrlsForSources,
Criticality,
DiscreteConfidence,
FrequentLicenses,
PackageInfo,
RawAttributions,
Resources,
ResourcesToAttributions,
} from '../../shared/shared-types';
import { RawFrequentLicense } from '../types/types';
function addTrailingSlashIfAbsent(resourcePath: string): string {
return resourcePath.endsWith('/') ? resourcePath : resourcePath.concat('/');
}
function getListOfResourcePaths(
basePath: string,
resourceName: string,
resources: Resources,
): Array<string> {
const fullResourcePath =
basePath + resourceName + (canResourceHaveChildren(resources) ? '/' : '');
return [fullResourcePath].concat(
Object.keys(resources)
.map((childPath) =>
getListOfResourcePaths(
fullResourcePath,
childPath,
resources[childPath] as Resources,
),
)
.flat(),
);
}
export function getAllResourcePaths(resources: Resources): Set<string> {
return new Set(getListOfResourcePaths('', '', resources));
}
export function sanitizeResourcesToAttributions(
resources: Resources,
rawResourcesToAttributions: ResourcesToAttributions,
): ResourcesToAttributions {
const allResourcePaths = getAllResourcePaths(resources);
return Object.fromEntries(
Object.entries(rawResourcesToAttributions).reduce(
(
accumulatedResult: Array<[string, Array<string>]>,
[path, attributions],
) => {
const pathWithSlashes = addTrailingSlashIfAbsent(path);
if (allResourcePaths.has(path)) {
accumulatedResult.push([path, attributions]);
} else if (allResourcePaths.has(pathWithSlashes)) {
accumulatedResult.push([pathWithSlashes, attributions]);
}
return accumulatedResult;
},
[],
),
);
}
export function getAttributionsToResources(
resourcesToAttributions: ResourcesToAttributions,
): AttributionsToResources {
return Object.entries(
resourcesToAttributions,
).reduce<AttributionsToResources>((acc, [resource, attributionIds]) => {
attributionIds.forEach((attributionId) => {
if (acc[attributionId]) {
acc[attributionId].push(resource);
} else {
acc[attributionId] = [resource];
}
});
return acc;
}, {});
}
export const HASH_EXCLUDE_KEYS = [
'attributionConfidence',
'comment',
'id',
'originIds',
'preSelected',
'wasPreferred',
] satisfies Array<keyof PackageInfo>;
export function mergePackageInfos(a: PackageInfo, b: PackageInfo): PackageInfo {
const diff: Required<Pick<PackageInfo, (typeof HASH_EXCLUDE_KEYS)[number]>> =
{
attributionConfidence:
min([a.attributionConfidence, b.attributionConfidence]) ??
DiscreteConfidence.High,
comment: compact([a.comment, b.comment]).join('\n\n'),
id: a.id,
originIds: Array.from(
new Set([...(a.originIds ?? []), ...(b.originIds ?? [])]),
),
preSelected: a.preSelected || b.preSelected || false,
wasPreferred: a.wasPreferred || b.wasPreferred || false,
};
return { ...a, ...diff };
}
export function mergeAttributions({
attributions,
resourcesToAttributions,
attributionsToResources,
}: {
attributions: Attributions;
resourcesToAttributions: ResourcesToAttributions;
attributionsToResources: AttributionsToResources;
}): [Attributions, ResourcesToAttributions] {
const attributionsWithResources = Object.values(
attributions,
).map<PackageInfo>((attribution) => ({
...attribution,
resources: sortBy(attributionsToResources[attribution.id]),
}));
const groups = Object.values(
groupBy<PackageInfo>(attributionsWithResources, (attribution) =>
objectHash(attribution, {
excludeKeys: (key) =>
HASH_EXCLUDE_KEYS.some((excludeKey) => excludeKey === key),
}),
),
);
return groups.reduce<[Attributions, ResourcesToAttributions]>(
([attributions, resourcesToAttributions], group) => {
const { resources, ...attribution } = group
.slice(1)
.reduce(
(mergedAttribution, attribution) =>
mergePackageInfos(mergedAttribution, attribution),
group[0],
);
// Re-assign merged attribution to first attribution in group
attributions[attribution.id] = attribution;
// Remove obsolete attributions from attributions map
group.slice(1).forEach(({ id }) => {
delete attributions[id];
});
// Delete references to removed attributions
resources?.forEach((resource) => {
resourcesToAttributions[resource] = [
attribution.id,
...resourcesToAttributions[resource].filter(
(attributionId) =>
!group.map(({ id }) => id).includes(attributionId),
),
];
});
return [attributions, resourcesToAttributions];
},
[attributions, resourcesToAttributions],
);
}
export function deserializeAttributions(
rawAttributions: RawAttributions,
): Attributions {
return Object.entries(rawAttributions).reduce<Attributions>(
(
attributions,
[
attributionId,
{ followUp, comment, criticality, originId, originIds, ...attribution },
],
) => {
const isCritical =
!!criticality && Object.values(Criticality).includes(criticality);
const sanitizedComment = comment?.replace(/^\s+|\s+$/g, '');
attributions[attributionId] = {
...attribution,
...((originId || originIds?.length) && {
originIds: (originIds ?? []).concat(originId ?? []),
}),
...(followUp === 'FOLLOW_UP' && { followUp: true }),
...(sanitizedComment && { comment: sanitizedComment }),
...(isCritical && { criticality }),
id: attributionId,
};
return attributions;
},
{},
);
}
export function serializeAttributions(
attributions: Attributions,
): RawAttributions {
return Object.entries(attributions).reduce<RawAttributions>(
(
rawAttributions,
[
attributionId,
{
count,
followUp,
id,
relation,
resources,
source,
suffix,
synthetic,
...attribution
},
],
) => {
rawAttributions[attributionId] = {
...attribution,
...(followUp && { followUp: 'FOLLOW_UP' }),
};
return rawAttributions;
},
{},
);
}
export function parseFrequentLicenses(
rawFrequentLicenses: Array<RawFrequentLicense> | undefined,
): FrequentLicenses {
const parsedFrequentLicenses: FrequentLicenses = { nameOrder: [], texts: {} };
if (!rawFrequentLicenses) {
return parsedFrequentLicenses;
}
rawFrequentLicenses.forEach((rawFrequentLicense) => {
parsedFrequentLicenses.nameOrder.push({
shortName: rawFrequentLicense.shortName,
fullName: rawFrequentLicense.fullName,
});
parsedFrequentLicenses.texts[rawFrequentLicense.shortName] =
rawFrequentLicense.defaultText;
parsedFrequentLicenses.texts[rawFrequentLicense.fullName] =
rawFrequentLicense.defaultText;
});
return parsedFrequentLicenses;
}
export function sanitizeRawBaseUrlsForSources(
rawBaseUrlsForSources: BaseUrlsForSources | undefined,
): BaseUrlsForSources {
return rawBaseUrlsForSources
? Object.fromEntries(
Object.entries(rawBaseUrlsForSources).map(([path, url]) => {
return [addTrailingSlashIfAbsent(path), url];
}),
)
: {};
}