This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrop-vis-cache.js
73 lines (56 loc) · 1.58 KB
/
drop-vis-cache.js
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
const chalk = require('chalk');
const arg = require('arg');
const Redis = require('ioredis');
const { requireConfig } = require('@datawrapper/service-utils/findConfig');
const config = requireConfig();
const redis = new Redis(config.redis);
let args = {};
try {
args = arg(
{
'--help': Boolean
},
{ permissive: true }
);
} catch (error) {
process.stderr.write(error.message);
process.stderr.write('');
process.exit(1);
}
if (args['--help']) {
process.stdout.write(chalk`
{bold.underline drop-vis-cache}
{bold Script to delete cached visualization styles from Redis.}
- Delete cached visualization styles
node scripts/drop-vis-cache.js {underline d3-lines} {underline d3-pies}
`);
process.exit(0);
}
const visualizations = args._;
const keysToDrop = [];
const stream = redis.scanStream({
match: `*:vis-styles:*__*`
});
process.stdout.write(chalk`
{blue Scanning keys...}`);
stream.on('data', keys => {
const filteredKeys = keys.filter(key => {
key = key.split('__')[1];
return visualizations.includes(key);
});
keysToDrop.push(...filteredKeys);
});
stream.on('end', async () => {
process.stdout.write(chalk`
{grey ${keysToDrop.join('\n ')}}
{blue Found ${keysToDrop.length} ${keysToDrop.length === 1 ? 'key' : 'keys'} to delete}
`);
let delCount = 0;
if (keysToDrop.length) {
delCount = await redis.del(...keysToDrop);
process.stdout.write(
chalk.green(`Deleted ${delCount} ${delCount === 1 ? 'key' : 'keys'}\n\n`)
);
}
process.exit(0);
});