mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-06-22 04:58:40 +00:00
final version
This commit is contained in:
@@ -3,21 +3,29 @@ import request from 'superagent';
|
|||||||
import Moment from 'moment';
|
import Moment from 'moment';
|
||||||
|
|
||||||
const BrewCleanup = ({})=>{
|
const BrewCleanup = ({})=>{
|
||||||
const [count, setCount] = useState(0);
|
const [junkBrewCollection, setJunkBrewCollection] = useState([]);
|
||||||
const [brewCollection, setBrewCollection] = useState([]);
|
const [lostBrewCollection, setLostBrewCollection] = useState([]);
|
||||||
const [pending, setPending] = useState(false);
|
const [pending, setPending] = useState(false);
|
||||||
const [found, setFound] = useState(false);
|
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const find = async ()=>{
|
const find = async (type)=>{
|
||||||
setPending(true);
|
setPending(true);
|
||||||
|
|
||||||
try {
|
if(type === 'junk') try {
|
||||||
const res = await request.get('/admin/cleanup');
|
const res = await request.get('/admin/cleanupJunk');
|
||||||
|
|
||||||
setCount(res.body.count);
|
setJunkBrewCollection(res.body.brewCollection);
|
||||||
setBrewCollection(res.body.brewCollection);
|
} catch (err) {
|
||||||
setFound(true);
|
setError(err);
|
||||||
|
} finally {
|
||||||
|
setPending(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(type === 'lost') try {
|
||||||
|
|
||||||
|
const res = await request.get('/admin/cleanupLost');
|
||||||
|
|
||||||
|
setLostBrewCollection(res.body.brewCollection);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -25,28 +33,42 @@ const BrewCleanup = ({})=>{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const cleanup = async ()=>{
|
const cleanup = async (type)=>{
|
||||||
setPending(true);
|
setPending(true);
|
||||||
|
|
||||||
try {
|
if(type === 'junk') try {
|
||||||
const res = await request.post('/admin/cleanup');
|
console.log('deleting junk')
|
||||||
|
const res = await request.post('/admin/cleanupJunk');
|
||||||
|
|
||||||
setCount(res.body.count);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
} finally {
|
} finally {
|
||||||
setPending(false);
|
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) {
|
const renderBrewList = (type)=>{
|
||||||
console.log(brewCollection)
|
|
||||||
|
const brewList = type === 'lost' ? lostBrewCollection : junkBrewCollection;
|
||||||
|
|
||||||
|
if(!brewList || brewList.length === 0) {
|
||||||
|
console.log(brewList);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<h2>{`Results - ${brewCollection.length} brews` }</h2>
|
<h2>{`Results - ${brewList.length} brews` }</h2>
|
||||||
<table className='resultsTable'>
|
<table className='resultsTable'>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -57,7 +79,7 @@ const BrewCleanup = ({})=>{
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{brewCollection
|
{brewList
|
||||||
.sort((a, b)=>{ // Sort brews from most recently updated
|
.sort((a, b)=>{ // Sort brews from most recently updated
|
||||||
if(a.updatedAt > b.updatedAt) return -1;
|
if(a.updatedAt > b.updatedAt) return -1;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -65,8 +87,6 @@ const BrewCleanup = ({})=>{
|
|||||||
.map((brew, idx)=>{
|
.map((brew, idx)=>{
|
||||||
return <tr key={idx}>
|
return <tr key={idx}>
|
||||||
<td><strong>{brew.title || 'No Title'}</strong></td>
|
<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 style={{ width: '200px' }}>{Moment(brew.updatedAt).fromNow()}</td>
|
||||||
<td>{brew.createdAt ? Moment(brew.createdAt).fromNow() : 'No creation date'}</td>
|
<td>{brew.createdAt ? Moment(brew.createdAt).fromNow() : 'No creation date'}</td>
|
||||||
<td>{brew.googleId ? 'Google' : 'Homebrewery'}</td>
|
<td>{brew.googleId ? 'Google' : 'Homebrewery'}</td>
|
||||||
@@ -76,36 +96,60 @@ const BrewCleanup = ({})=>{
|
|||||||
</table>
|
</table>
|
||||||
</>;
|
</>;
|
||||||
};
|
};
|
||||||
const renderFound = ()=>{
|
const renderFound = (type)=>{
|
||||||
if(!found) return;
|
|
||||||
|
|
||||||
if(!count) return <div className='result noBrews'>No Matching Brews found.</div>;
|
if(type === 'junk' && junkBrewCollection.length === 0 || type === 'lost' && lostBrewCollection.length === 0) return <div className='result noBrews'>No Matching Brews found.</div>;
|
||||||
|
|
||||||
return <div className='result'>
|
return <div className='result'>
|
||||||
<button onClick={()=>cleanup()} className='remove'>
|
{renderBrewList(type)}
|
||||||
|
<button onClick={()=>cleanup(type)} className='remove'>
|
||||||
{pending
|
{pending
|
||||||
? <i className='fas fa-spin fa-spinner' />
|
? <i className='fas fa-spin fa-spinner' />
|
||||||
: <span><i className='fas fa-times' /> Remove</span>
|
: <span><i className='fas fa-times' /> Remove</span>
|
||||||
}
|
}
|
||||||
</button>
|
</button>
|
||||||
<span>Found {count} Brews that could be removed. </span>
|
<span>Found {type === 'junk' ? junkBrewCollection.length : lostBrewCollection.length} Brews that could be removed. </span>
|
||||||
{renderBrewList()}
|
|
||||||
|
</div>;
|
||||||
|
};
|
||||||
|
const renderJunkBrewCleanup = ()=>{
|
||||||
|
return <div className='junk'>
|
||||||
|
<h3> Junk brews</h3>
|
||||||
|
<p>Removes very short brews to tidy up the database</p>
|
||||||
|
|
||||||
|
<button onClick={()=>find('junk')} className='query'>
|
||||||
|
{pending
|
||||||
|
? <i className='fas fa-spin fa-spinner' />
|
||||||
|
: 'Query Brews'
|
||||||
|
}
|
||||||
|
</button>
|
||||||
|
{renderFound('junk')}
|
||||||
|
|
||||||
|
{error && <div className='error noBrews'>{error.toString()}</div>}
|
||||||
|
</div>;
|
||||||
|
};
|
||||||
|
const renderLostBrewCleanup = ()=>{
|
||||||
|
return <div className='lost'>
|
||||||
|
<h3> Lost brews</h3>
|
||||||
|
<p>Removes very short brews to tidy up the database</p>
|
||||||
|
|
||||||
|
<button onClick={()=>find('lost')} className='query'>
|
||||||
|
{pending
|
||||||
|
? <i className='fas fa-spin fa-spinner' />
|
||||||
|
: 'Query Brews'
|
||||||
|
}
|
||||||
|
</button>
|
||||||
|
{renderFound('lost')}
|
||||||
|
|
||||||
|
{error && <div className='error noBrews'>{error.toString()}</div>}
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
return <div className='brewUtil brewCleanup'>
|
return <div className='brewUtil brewCleanup'>
|
||||||
<h2> Brew Cleanup </h2>
|
<h2> Brew Cleanup </h2>
|
||||||
<p>Removes very short brews to tidy up the database</p>
|
{renderJunkBrewCleanup()}
|
||||||
|
{renderLostBrewCleanup()}
|
||||||
|
|
||||||
<button onClick={()=>find('lost')} className='query'>
|
|
||||||
{pending
|
|
||||||
? <i className='fas fa-spin fa-spinner' />
|
|
||||||
: 'Query Brews'
|
|
||||||
}
|
|
||||||
</button>
|
|
||||||
{renderFound()}
|
|
||||||
|
|
||||||
{error && <div className='error noBrews'>{error.toString()}</div>}
|
|
||||||
</div>;
|
</div>;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-3
@@ -44,7 +44,7 @@ export default function createAdminApi(vite) {
|
|||||||
updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
|
updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
|
||||||
lastViewed : { $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 } } },
|
{ $match: { textBinSize: { $lt: 140 } } },
|
||||||
{ $limit: 300 }
|
{ $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
|
// 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)=>{
|
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)=>{
|
.then((docs)=>{
|
||||||
const ids = docs.map((doc)=>doc._id);
|
const ids = docs.map((doc)=>doc._id);
|
||||||
|
console.log(ids);
|
||||||
return HomebrewModel.deleteMany({ _id: { $in: ids } });
|
return HomebrewModel.deleteMany({ _id: { $in: ids } });
|
||||||
}).then((result)=>{
|
}).then((result)=>{
|
||||||
res.json({ count: result.deletedCount });
|
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
|
// Delete up to 300 unauthored brews that have not been viewed or updated in a year
|
||||||
router.post('/admin/cleanupLost', mw.adminOnly, (req, res)=>{
|
router.post('/admin/cleanupLost', mw.adminOnly, (req, res)=>{
|
||||||
HomebrewModel.aggregate(lostBrewPipeline).option({ maxTimeMS: 60000 })
|
HomebrewModel.aggregate(lostBrewsPipeline).option({ maxTimeMS: 60000 })
|
||||||
.then((docs)=>{
|
.then((docs)=>{
|
||||||
const ids = docs.map((doc)=>doc._id);
|
const ids = docs.map((doc)=>doc._id);
|
||||||
return HomebrewModel.deleteMany({ _id: { $in: ids } });
|
return HomebrewModel.deleteMany({ _id: { $in: ids } });
|
||||||
|
|||||||
Reference in New Issue
Block a user