mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-06-22 04:58:40 +00:00
proper cleaning function
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import request from 'superagent';
|
import request from 'superagent';
|
||||||
|
import Moment from 'moment';
|
||||||
|
|
||||||
const BrewCleanup = ({})=>{
|
const BrewCleanup = ({})=>{
|
||||||
const [count, setCount] = useState(0);
|
const [count, setCount] = useState(0);
|
||||||
|
const [brewCollection, setBrewCollection] = useState([]);
|
||||||
const [pending, setPending] = useState(false);
|
const [pending, setPending] = useState(false);
|
||||||
const [primed, setPrimed] = useState(false);
|
const [primed, setPrimed] = useState(false);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
@@ -14,6 +16,7 @@ const BrewCleanup = ({})=>{
|
|||||||
const res = await request.get('/admin/cleanup');
|
const res = await request.get('/admin/cleanup');
|
||||||
|
|
||||||
setCount(res.body.count);
|
setCount(res.body.count);
|
||||||
|
setBrewCollection(res.body.brewCollection);
|
||||||
setPrimed(true);
|
setPrimed(true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
@@ -36,6 +39,43 @@ const BrewCleanup = ({})=>{
|
|||||||
setPrimed(false);
|
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 = ()=>{
|
const renderPrimed = ()=>{
|
||||||
if(!primed) return;
|
if(!primed) return;
|
||||||
|
|
||||||
@@ -49,6 +89,7 @@ const BrewCleanup = ({})=>{
|
|||||||
}
|
}
|
||||||
</button>
|
</button>
|
||||||
<span>Found {count} Brews that could be removed. </span>
|
<span>Found {count} Brews that could be removed. </span>
|
||||||
|
{renderBrewList()}
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -41,12 +41,13 @@ export default function createAdminApi(vite) {
|
|||||||
|
|
||||||
const junkBrewPipeline = [
|
const junkBrewPipeline = [
|
||||||
{ $match : {
|
{ $match : {
|
||||||
updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
|
updatedAt : { $lt: Moment().subtract(365, 'days').toDate() },
|
||||||
lastViewed : { $lt: Moment().subtract(30, 'days').toDate() }
|
lastViewed : { $lt: Moment().subtract(365, 'days').toDate() }
|
||||||
} },
|
}},
|
||||||
{ $project: { textBinSize: { $binarySize: '$textBin' } } },
|
{ $match: {
|
||||||
{ $match: { textBinSize: { $lt: 140 } } },
|
authors : [],
|
||||||
{ $limit: 100 }
|
}},
|
||||||
|
{ $limit: 300 }
|
||||||
];
|
];
|
||||||
|
|
||||||
/* Search for brews that aren't compressed (missing the compressed text field) */
|
/* Search for brews that aren't compressed (missing the compressed text field) */
|
||||||
@@ -54,10 +55,10 @@ export default function createAdminApi(vite) {
|
|||||||
'text' : { '$exists': true }
|
'text' : { '$exists': true }
|
||||||
}).lean().limit(10000).select('_id');
|
}).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)=>{
|
router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{
|
||||||
HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
|
HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
|
||||||
.then((objs)=>res.json({ count: objs.length }))
|
.then((objs)=>res.json({ count: objs.length, brewCollection : objs }))
|
||||||
.catch((error)=>{
|
.catch((error)=>{
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Internal Server Error' });
|
res.status(500).json({ error: 'Internal Server Error' });
|
||||||
|
|||||||
Reference in New Issue
Block a user