Skip to content

Instantly share code, notes, and snippets.

@isocroft
Forked from romuald/processlist.sql
Created July 24, 2025 22:30
Show Gist options
  • Select an option

  • Save isocroft/adf3263c2c3ff5d2119712ea6485f0c1 to your computer and use it in GitHub Desktop.

Select an option

Save isocroft/adf3263c2c3ff5d2119712ea6485f0c1 to your computer and use it in GitHub Desktop.
Show PostgreSQL current (running) process list;
SELECT user, pid, client_addr, waiting, query, query_start, NOW() - query_start AS elapsed
FROM pg_stat_activity
WHERE query != '<IDLE>'
-- AND EXTRACT(EPOCH FROM (NOW() - query_start)) > 1
ORDER BY elapsed DESC;
@isocroft
Copy link
Author

    const fs = require('fs');
    const PNG = require('pngjs').PNG;
    const pixelMatch = require('pixelmatch');

    module.exports = {
        getPNGImageSimilarityPercentage (sourceImagePath, targetImagePath, diffImagePath = './diff.png') {

            if (typeof(sourceImageFile) !== 'string'
                || !sourceImageFile.toLowerCase().endsWith('.png')) {
                return Promise.reject(throw new Error(''));
            }

            if (typeof(targetImagePath) !== 'string'
                || !targetImagePath.toLowerCase().endsWith('.png')) {
                return Promise.reject(throw new Error(''));
            }

            return new Promise ((res, rej) => {

                const sourceImageFile = fs.createReadStream(sourceImagePath).pipe(new PNG()).on('parsed', doneReading);
                const targetImageFile = fs.createReadStream(targetImagePath).pipe(new PNG()).on('parsed', doneReading);

                let filesReadCount = 0;

                function donReading () {
                    if (++filesReadCount < 2) return;

                    if (sourceImageFile.width !== targetImageFile.width
                        || sourceImageFile.height !== targetImageFile.height) {
                        if (process.env.NODE_ENV === 'development') {
                            console.error('images have different dimensions');
                        }
                        rej(new Error('source and target image dimensions differ'));
                    }

                    const diffPNGImageFile = new PNG({
                        width: sourceImageFile.width,
                        height: sourceImageFile.height
                    });

                    const diffPixelsCount = pixelMatch(
                        sourceImageFile.data,
                        targetImageFile.data,
                        diffPNGImageFile.data,
                        sourceImageFile.width,
                        sourceImageFile.height
                    );

                    const sourceImageFileTotalPixelsCount = sourceImageFile.width * sourceImageFile.height;
                    const similarity = 100 - (diffPixelsCount / sourceImageFileTotalPixelsCount) * 100;

                    diffPNGImageFile.pack().pipe(fs.createWriteStream(diffImagePath)).on('done', () => {
                        res(`${similarity.toFixed(2)}%`);
                    });
                }
            });
        }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment