UNPKG

1.33 kBJavaScriptView Raw
1'use strict'
2
3const co = require('co')
4const cli = require('heroku-cli-util')
5const pg = require('heroku-pg')
6
7function * run (context, heroku) {
8 let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
9
10 let query = `
11 WITH RECURSIVE Z(IX, IY, CX, CY, X, Y, I) AS (
12 SELECT IX, IY, X::float, Y::float, X::float, Y::float, 0
13 FROM (select -2.2 + 0.031 * i, i from generate_series(0,101) as i) as xgen(x,ix),
14 (select -1.5 + 0.031 * i, i from generate_series(0,101) as i) as ygen(y,iy)
15 UNION ALL
16 SELECT IX, IY, CX, CY, X * X - Y * Y + CX AS X, Y * X * 2 + CY, I + 1
17 FROM Z
18 WHERE X * X + Y * Y < 16::float
19 AND I < 100
20 )
21SELECT array_to_string(array_agg(SUBSTRING(' .,,,-----++++%%%%@@@@#### ', LEAST(GREATEST(I,1),27), 1)),'')
22FROM (
23 SELECT IX, IY, MAX(I) AS I
24 FROM Z
25 GROUP BY IY, IX
26 ORDER BY IY, IX
27 ) AS ZT
28GROUP BY IY
29ORDER BY IY
30 `
31
32 let output = yield pg.psql.exec(db, query)
33 process.stdout.write(output)
34}
35
36const cmd = {
37 topic: 'pg',
38 description: 'show the mandelbrot set',
39 needsApp: true,
40 needsAuth: true,
41 args: [{name: 'database', optional: true}],
42 run: cli.command(co.wrap(run))
43}
44
45module.exports = [
46 Object.assign({command: 'mandelbrot'}, cmd)
47]