From 1838a57e53ce15bc3836c91d5ee4f3e74185a5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Fri, 29 May 2026 11:32:09 +0200 Subject: [PATCH] final version --- .../brewUtils/brewCleanup/brewCleanup.jsx | 120 ++++++++++++------ server/admin.api.js | 8 +- 2 files changed, 87 insertions(+), 41 deletions(-) diff --git a/client/admin/brewUtils/brewCleanup/brewCleanup.jsx b/client/admin/brewUtils/brewCleanup/brewCleanup.jsx index be1ca0a51..cd60e6728 100644 --- a/client/admin/brewUtils/brewCleanup/brewCleanup.jsx +++ b/client/admin/brewUtils/brewCleanup/brewCleanup.jsx @@ -3,21 +3,29 @@ import request from 'superagent'; import Moment from 'moment'; const BrewCleanup = ({})=>{ - const [count, setCount] = useState(0); - const [brewCollection, setBrewCollection] = useState([]); + const [junkBrewCollection, setJunkBrewCollection] = useState([]); + const [lostBrewCollection, setLostBrewCollection] = useState([]); const [pending, setPending] = useState(false); - const [found, setFound] = useState(false); const [error, setError] = useState(null); - const find = async ()=>{ + const find = async (type)=>{ setPending(true); - try { - const res = await request.get('/admin/cleanup'); + if(type === 'junk') try { + const res = await request.get('/admin/cleanupJunk'); - setCount(res.body.count); - setBrewCollection(res.body.brewCollection); - setFound(true); + setJunkBrewCollection(res.body.brewCollection); + } catch (err) { + setError(err); + } finally { + setPending(false); + } + + if(type === 'lost') try { + + const res = await request.get('/admin/cleanupLost'); + + setLostBrewCollection(res.body.brewCollection); } catch (err) { setError(err); } finally { @@ -25,28 +33,42 @@ const BrewCleanup = ({})=>{ } }; - const cleanup = async ()=>{ + const cleanup = async (type)=>{ setPending(true); - try { - const res = await request.post('/admin/cleanup'); + if(type === 'junk') try { + console.log('deleting junk') + const res = await request.post('/admin/cleanupJunk'); - setCount(res.body.count); } catch (err) { setError(err); } finally { setPending(false); - setFound(false); + setJunkBrewCollection([]); + } + + if(type === 'lost') try { + const res = await request.post('/admin/cleanupLost'); + + } catch (err) { + setError(err); + } finally { + setPending(false); + setLostBrewCollection([]); } }; - const renderBrewList = ()=>{ - if(!brewCollection) { - console.log(brewCollection) + + const renderBrewList = (type)=>{ + + const brewList = type === 'lost' ? lostBrewCollection : junkBrewCollection; + + if(!brewList || brewList.length === 0) { + console.log(brewList); return null; } - + return <> -

{`Results - ${brewCollection.length} brews` }

+

{`Results - ${brewList.length} brews` }

@@ -57,7 +79,7 @@ const BrewCleanup = ({})=>{ - {brewCollection + {brewList .sort((a, b)=>{ // Sort brews from most recently updated if(a.updatedAt > b.updatedAt) return -1; return 1; @@ -65,8 +87,6 @@ const BrewCleanup = ({})=>{ .map((brew, idx)=>{ return - {// - } @@ -76,36 +96,60 @@ const BrewCleanup = ({})=>{
{brew.title || 'No Title'}{brew.shareId}{Moment(brew.updatedAt).fromNow()} {brew.createdAt ? Moment(brew.createdAt).fromNow() : 'No creation date'} {brew.googleId ? 'Google' : 'Homebrewery'}
; }; - const renderFound = ()=>{ - if(!found) return; + const renderFound = (type)=>{ - if(!count) return
No Matching Brews found.
; + if(type === 'junk' && junkBrewCollection.length === 0 || type === 'lost' && lostBrewCollection.length === 0) return
No Matching Brews found.
; return
- - Found {count} Brews that could be removed. - {renderBrewList()} + Found {type === 'junk' ? junkBrewCollection.length : lostBrewCollection.length} Brews that could be removed. + +
; + }; + const renderJunkBrewCleanup = ()=>{ + return
+

Junk brews

+

Removes very short brews to tidy up the database

+ + + {renderFound('junk')} + + {error &&
{error.toString()}
} +
; + }; + const renderLostBrewCleanup = ()=>{ + return
+

Lost brews

+

Removes very short brews to tidy up the database

+ + + {renderFound('lost')} + + {error &&
{error.toString()}
}
; }; return

Brew Cleanup

-

Removes very short brews to tidy up the database

+ {renderJunkBrewCleanup()} + {renderLostBrewCleanup()} - - {renderFound()} - - {error &&
{error.toString()}
}
; }; diff --git a/server/admin.api.js b/server/admin.api.js index e16024326..4033c30c6 100644 --- a/server/admin.api.js +++ b/server/admin.api.js @@ -44,7 +44,7 @@ export default function createAdminApi(vite) { updatedAt : { $lt: Moment().subtract(30, 'days').toDate() }, lastViewed : { $lt: Moment().subtract(30, 'days').toDate() } } }, - { $project: { textBinSize: { $binarySize: '$textBin' } } }, + { $project: { _id: 1, textBinSize: { $binarySize: '$textBin' } } }, { $match: { textBinSize: { $lt: 140 } } }, { $limit: 300 } ]; @@ -77,9 +77,11 @@ export default function createAdminApi(vite) { // Delete up to 300 brews that have not been viewed or updated in 30 days and are shorter than 140 bytes router.post('/admin/cleanupJunk', mw.adminOnly, (req, res)=>{ - HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 }) + console.log('deleting'); + HomebrewModel.aggregate(junkBrewsPipeline).option({ maxTimeMS: 60000 }) .then((docs)=>{ const ids = docs.map((doc)=>doc._id); + console.log(ids); return HomebrewModel.deleteMany({ _id: { $in: ids } }); }).then((result)=>{ res.json({ count: result.deletedCount }); @@ -101,7 +103,7 @@ export default function createAdminApi(vite) { // Delete up to 300 unauthored brews that have not been viewed or updated in a year router.post('/admin/cleanupLost', mw.adminOnly, (req, res)=>{ - HomebrewModel.aggregate(lostBrewPipeline).option({ maxTimeMS: 60000 }) + HomebrewModel.aggregate(lostBrewsPipeline).option({ maxTimeMS: 60000 }) .then((docs)=>{ const ids = docs.map((doc)=>doc._id); return HomebrewModel.deleteMany({ _id: { $in: ids } });