-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmassage.js
More file actions
308 lines (279 loc) · 10.3 KB
/
massage.js
File metadata and controls
308 lines (279 loc) · 10.3 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
require('dotenv').config();
const db = require('./lib/db');
const fs = require('fs');
const path = require('path');
const { match } = require('assert');
function isFiniteNumber(n) {
return typeof n === 'number' && Number.isFinite(n);
}
function toNumberOrZero(v) {
if (isFiniteNumber(v)) return v;
const n = parseFloat(String(v ?? '').trim());
return Number.isFinite(n) ? n : 0;
}
function toIntOrZero(v) {
if (isFiniteNumber(v)) return Math.trunc(v);
const n = parseInt(String(v ?? '').trim(), 10);
return Number.isFinite(n) ? n : 0;
}
function toBool(v) {
if (typeof v === 'boolean') return v;
const s = String(v ?? '').trim().toLowerCase();
if (s === 'yes' || s === 'true' || s === '1') return true;
return false;
}
function splitFlags(v, regex, mapper = capitalize) {
const s = String(v ?? '');
if (!s.trim()) return [];
return s.split(regex).map(mapper).map(x => x.trim()).filter(flag => flag.length > 0);
}
function computeFloweringMonthsByNumber(monthStr) {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const s = String(monthStr ?? '').trim();
if (!s) return [];
// Support en dash, em dash, and hyphen ranges (e.g. "May–Jun", "May-Jun", "May—Jun")
const parts = s.split(/[–—-]/).map(p => p.trim()).filter(Boolean);
if (parts.length === 1) {
const idx = months.indexOf(parts[0]);
return idx > -1 ? [idx] : [];
}
const from = months.indexOf(parts[0]);
const to = months.indexOf(parts[1]);
if (from === -1 || to === -1) return [];
const out = [];
for (let i = from; i <= to; i++) out.push(i);
return out;
}
// Work around lack of top level await while still reporting errors properly
go().then(() => {
// All done
console.log('Massage complete');
}).catch(e => {
console.error(e);
process.exit(1);
});
async function go() {
const { plants, close } = await db();
// Helpful when schema validation is enabled: print details so failures are actionable.
const originalUpdateOne = plants.updateOne.bind(plants);
plants.updateOne = async (...args) => {
try {
return await originalUpdateOne(...args);
} catch (e) {
if (e && e.code === 121 && e.errInfo && e.errInfo.details) {
console.error('MongoDB schema validation failed during massage.updateOne');
try {
console.error(JSON.stringify(e.errInfo.details, null, 2));
} catch (jsonErr) {
console.error(e.errInfo.details);
}
}
throw e;
}
};
let values = await plants.find().toArray();
// Fix a small set of plants that already had duplicate records
// when case was not taken into account. They were all
// x-class (hybrids)
const seen = new Set();
for (const plant of values) {
if (seen.has(plant._id.toLowerCase())) {
await plants.removeOne({ _id: plant._id });
values = values.filter(value => value._id !== plant._id);
} else {
seen.add(plant._id.toLowerCase());
}
}
for (let i = 0; (i < values.length); i++) {
let plant = values[i];
// Ensure required fields exist and have valid types BEFORE any other updates.
// This allows us to repair legacy/invalid documents even when validation is strict.
const scientificName = plant['Scientific Name'] || plant._id;
const genus = (String(scientificName ?? '').split(/\s+/)[0] || '').trim();
const family = plant['Plant Family'] || plant['Family'] || '';
// Build required flags (empty arrays are acceptable to the schema).
const plantType = plant['Plant Type'] || '';
let plantTypeFlags = [];
let lifeCycleFlags = [];
const matches = String(plantType).match(/^(.*?)(\(.*?\))?$/);
if (matches) {
plantTypeFlags = matches[1].split(/, /);
if (matches[2]) {
plantTypeFlags = [...plantTypeFlags, ...matches[2].split(/ or /)];
}
plantTypeFlags = plantTypeFlags.map(flag => flag.trim().replace('(', '').replace(')', '')).map(capitalize).filter(Boolean);
const lifeCycles = ['Annual', 'Biennial', 'Perennial'];
lifeCycleFlags = plantTypeFlags.filter(pt => lifeCycles.includes(pt));
plantTypeFlags = plantTypeFlags.filter(pt => !lifeCycles.includes(pt));
}
const sunExposureFlags = splitFlags(plant['Sun Exposure'], /,\s*/, capitalize);
const soilMoistureFlags = splitFlags(plant['Soil Moisture'], /,\s*/, capitalize);
const pollinatorFlags = splitFlags(plant['Pollinators'], /\s*(?:,|;)+\s*/, capitalize);
const flowerColorFlags = splitFlags(plant['Flower Color'], /\s*[-—–,]\s*/, capitalize);
const availabilityFlags = [];
if (String(plant['Online Flag'] ?? '') === '1') availabilityFlags.push('Online');
if (String(plant['Local Flag'] ?? '') === '1') availabilityFlags.push('Local');
const requiredFixes = {
'Height (feet)': toNumberOrZero(plant['Height (feet)']),
'Spread (feet)': toNumberOrZero(plant['Spread (feet)']),
'Recommendation Score': toIntOrZero(plant['Recommendation Score']),
'Showy': toBool(plant['Showy']),
'Superplant': toBool(plant['Superplant']),
'States': splitFlags(plant['Distribution in USA'], /,\s*/, s => String(s).trim()).filter(Boolean),
'Genus': genus,
'Family': String(family ?? ''),
'Sun Exposure Flags': sunExposureFlags,
'Soil Moisture Flags': soilMoistureFlags,
'Plant Type Flags': plantTypeFlags,
'Life Cycle Flags': lifeCycleFlags,
'Pollinator Flags': pollinatorFlags,
'Flower Color Flags': flowerColorFlags,
'Availability Flags': availabilityFlags,
'Flowering Months By Number': computeFloweringMonthsByNumber(plant['Flowering Months']),
};
await plants.updateOne({ _id: plant._id }, { $set: requiredFixes });
// Check if image files exist and set hasImage and hasPreview flags
const fullImagePath = `${__dirname}/images/${plant._id}.jpg`;
const previewImagePath = `${__dirname}/images/${plant._id}.preview.jpg`;
const hasImage = fs.existsSync(fullImagePath);
const hasPreview = fs.existsSync(previewImagePath);
await plants.updateOne({
_id: plant._id
}, {
$set: {
hasImage: hasImage,
hasPreview: hasPreview
}
});
// Fix plants whose scientific names did not follow the canonical case pattern
const name = plant._id;
const renamed = name.substring(0, 1) + name.substring(1).toLowerCase();
if (name.match(/[A-Z].*?[A-Z]/)) {
console.log(`Renaming ${name} to ${renamed}`);
plant = {
...plant,
_id: renamed,
'Scientific Name': renamed
};
await plants.insertOne(plant);
await plants.removeOne({
_id: name
});
const oldName = `${__dirname}/images/${name}.jpg`;
const newName = `${__dirname}/images/${renamed}.jpg`;
if (fs.existsSync(oldName)) {
fs.renameSync(oldName, newName);
// Update hasImage flag for renamed plant
await plants.updateOne({
_id: renamed
}, {
$set: {
hasImage: true
}
});
}
}
// Process Flowering Months into an array of flags
// NOTE: This field is required by strict DB schema. Never $unset it.
const floweringMonthsByNumber = computeFloweringMonthsByNumber(plant['Flowering Months']);
await plants.updateOne({ _id: plant._id }, { $set: { 'Flowering Months By Number': floweringMonthsByNumber } });
var states = plant['Distribution in USA'].split(', ')
await plants.updateOne({
_id: plant._id
}, {
$set: {
'States': states
}
});
// Convert height to number (no ranges, just single values)
const height = plant['Height (feet)'];
if (height !== undefined && height !== null) {
const heightNum = typeof height === 'number' ? height : parseFloat(height);
if (!isNaN(heightNum)) {
await plants.updateOne({
_id: plant._id
}, {
$set: {
'Height (feet)': heightNum
}
});
}
}
// Convert spread to number for consistency
const spread = plant['Spread (feet)'];
if (spread !== undefined && spread !== null) {
const spreadNum = typeof spread === 'number' ? spread : parseFloat(spread);
if (!isNaN(spreadNum)) {
await plants.updateOne({
_id: plant._id
}, {
$set: {
'Spread (feet)': spreadNum
}
});
}
}
// Fix Reccomendation Score to be an int not a string
const score = plant['Recommendation Score'];
if (typeof score === 'string' || score instanceof String) {
const numScore = parseFloat(score);
if (!isNaN(numScore)) {
await plants.updateOne({
_id: plant._id
}, {
$set: {
'Recommendation Score': Math.trunc(numScore)
}
});
} else {
await plants.updateOne({
_id: plant._id
}, {
$set: {
'Recommendation Score': 0
}
});
}
}
if (plant['Flower Color'] === '') {
// Handle null values consistently for fields we sort on
await plants.updateOne({
_id: plant._id
}, {
$unset: {
'Flower Color': 1
}
});
}
if (plant?.metadata?.url && (!plant.imageUrl)) {
await plants.updateOne({
_id: plant._id
}, {
$set: {
imageUrl: plant.metadata.url
}
});
}
if (!plant.source || (plant.source === 'wikimediaPageSearch')) {
if (plant?.metadata?.extmetadata) {
const artist = plant?.metadata?.extmetadata?.Artist?.value || 'Unknown';
const licenseUrl = plant?.metadata?.extmetadata?.LicenseUrl?.value;
const licenseShortName = plant?.metadata?.extmetadata?.LicenseShortName?.value;
await plants.updateOne({
_id: plant._id
}, {
$set: {
source: 'wikimediaPageSearch',
attribution: `${artist}, ${licenseUrl ? `<a href="${licenseUrl}">` : ''}${licenseShortName}${licenseUrl ? '</a>' : ''}`
}
});
}
}
// NOTE: "Flags" fields + required fields are computed and fixed at the top of the loop
// in a single `$set` to keep the document schema-valid under strict validation.
}
await close();
}
function capitalize(s) {
return s.split(' ').map(s => s.charAt(0).toUpperCase() + s.substring(1)).join(' ');
}