mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-06-22 00:38:38 +00:00
proper cleaning function
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import React, { useState } from 'react';
|
||||
import request from 'superagent';
|
||||
import Moment from 'moment';
|
||||
|
||||
const BrewCleanup = ({})=>{
|
||||
const [count, setCount] = useState(0);
|
||||
const [brewCollection, setBrewCollection] = useState([]);
|
||||
const [pending, setPending] = useState(false);
|
||||
const [primed, setPrimed] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
@@ -14,6 +16,7 @@ const BrewCleanup = ({})=>{
|
||||
const res = await request.get('/admin/cleanup');
|
||||
|
||||
setCount(res.body.count);
|
||||
setBrewCollection(res.body.brewCollection);
|
||||
setPrimed(true);
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
@@ -36,6 +39,43 @@ const BrewCleanup = ({})=>{
|
||||
setPrimed(false);
|
||||
}
|
||||
};
|
||||
const renderBrewList = ()=>{
|
||||
if(!brewCollection) {
|
||||
console.log(brewCollection)
|
||||
return null;
|
||||
}
|
||||
|
||||
return <>
|
||||
<h2>{`Results - ${brewCollection.length} brews` }</h2>
|
||||
<table className='resultsTable'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>Share</th>
|
||||
<th>Last Update</th>
|
||||
<th>Created</th>
|
||||
<th>Storage</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{brewCollection
|
||||
.sort((a, b)=>{ // Sort brews from most recently updated
|
||||
if(a.updatedAt > b.updatedAt) return -1;
|
||||
return 1;
|
||||
})
|
||||
.map((brew, idx)=>{
|
||||
return <tr key={idx}>
|
||||
<td><strong>{brew.title || 'No Title'}</strong></td>
|
||||
<td><a href={`/share/${brew.shareId}`}>{brew.shareId}</a></td>
|
||||
<td style={{ width: '200px' }}>{Moment(brew.updatedAt).fromNow()}</td>
|
||||
<td>{brew.createdAt ? Moment(brew.createdAt).fromNow() : 'No creation date'}</td>
|
||||
<td>{brew.googleId ? 'Google' : 'Homebrewery'}</td>
|
||||
</tr>;
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</>;
|
||||
};
|
||||
const renderPrimed = ()=>{
|
||||
if(!primed) return;
|
||||
|
||||
@@ -49,6 +89,7 @@ const BrewCleanup = ({})=>{
|
||||
}
|
||||
</button>
|
||||
<span>Found {count} Brews that could be removed. </span>
|
||||
{renderBrewList()}
|
||||
</div>;
|
||||
};
|
||||
|
||||
|
||||
+9
-8
@@ -41,12 +41,13 @@ export default function createAdminApi(vite) {
|
||||
|
||||
const junkBrewPipeline = [
|
||||
{ $match : {
|
||||
updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
|
||||
lastViewed : { $lt: Moment().subtract(30, 'days').toDate() }
|
||||
} },
|
||||
{ $project: { textBinSize: { $binarySize: '$textBin' } } },
|
||||
{ $match: { textBinSize: { $lt: 140 } } },
|
||||
{ $limit: 100 }
|
||||
updatedAt : { $lt: Moment().subtract(365, 'days').toDate() },
|
||||
lastViewed : { $lt: Moment().subtract(365, 'days').toDate() }
|
||||
}},
|
||||
{ $match: {
|
||||
authors : [],
|
||||
}},
|
||||
{ $limit: 300 }
|
||||
];
|
||||
|
||||
/* Search for brews that aren't compressed (missing the compressed text field) */
|
||||
@@ -54,10 +55,10 @@ export default function createAdminApi(vite) {
|
||||
'text' : { '$exists': true }
|
||||
}).lean().limit(10000).select('_id');
|
||||
|
||||
// Search for up to 100 brews that have not been viewed or updated in 30 days and are shorter than 140 bytes
|
||||
// Search for up to 100 brews that have not been viewed or updated in a year
|
||||
router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{
|
||||
HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
|
||||
.then((objs)=>res.json({ count: objs.length }))
|
||||
.then((objs)=>res.json({ count: objs.length, brewCollection : objs }))
|
||||
.catch((error)=>{
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Internal Server Error' });
|
||||
|
||||
Reference in New Issue
Block a user