From ff513a2d1c6888382a1f4c73f9496cfec82b4ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Sverre=20Lien=20Sell=C3=A6g?= Date: Sun, 11 Jul 2021 15:39:04 +0200 Subject: [PATCH] write some cli tools to agregate data easier --- bin/flatten-array | 21 +++++++++++++++++++++ bin/get-hosts-from-event-nodes | 25 +++++++++++++++++++++++++ bin/get-pages-from-hosts | 24 ++++++++++++++++++++++++ bin/unique-by-id | 27 +++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100755 bin/flatten-array create mode 100755 bin/get-hosts-from-event-nodes create mode 100755 bin/get-pages-from-hosts create mode 100755 bin/unique-by-id diff --git a/bin/flatten-array b/bin/flatten-array new file mode 100755 index 0000000..a109504 --- /dev/null +++ b/bin/flatten-array @@ -0,0 +1,21 @@ +#!/usr/bin/env node + +const flatten = require('ramda/src/flatten'); + +let input = []; + +process.stdin.resume(); +process.stdin.setEncoding('utf8'); + +process.stdin.on('data', (data) => { + input.push(data); +}); + +process.stdin.on('end', () => { + const str = input.join(''); + let events = JSON.parse(str); + + events = flatten(events); + + console.log(JSON.stringify(events)); +}); diff --git a/bin/get-hosts-from-event-nodes b/bin/get-hosts-from-event-nodes new file mode 100755 index 0000000..549a065 --- /dev/null +++ b/bin/get-hosts-from-event-nodes @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +const pathOr = require('ramda/src/pathOr'); + +let input = []; + +process.stdin.resume(); +process.stdin.setEncoding('utf8'); + +process.stdin.on('data', (data) => { + input.push(data); +}); + +process.stdin.on('end', () => { + const str = input.join(''); + + const events = JSON.parse(str); + + const s = events.reduce((acc, event) => { + const pages = pathOr([], ['hosts', 'edges'], event); + return [...acc, pages]; + }, []); + + console.log(JSON.stringify(s)); +}); diff --git a/bin/get-pages-from-hosts b/bin/get-pages-from-hosts new file mode 100755 index 0000000..e4aad72 --- /dev/null +++ b/bin/get-pages-from-hosts @@ -0,0 +1,24 @@ +#!/usr/bin/env node + +const pathOr = require('ramda/src/pathOr'); + +let input = []; + +process.stdin.resume(); +process.stdin.setEncoding('utf8'); + +process.stdin.on('data', (data) => { + input.push(data); +}); + +process.stdin.on('end', () => { + const str = input.join(''); + + const events = JSON.parse(str); + + const pages = events + .map(({ node }) => node) + .filter(({ __typename }) => __typename == 'Page'); + + console.log(JSON.stringify(pages)); +}); diff --git a/bin/unique-by-id b/bin/unique-by-id new file mode 100755 index 0000000..c3ecee5 --- /dev/null +++ b/bin/unique-by-id @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +const pathOr = require('ramda/src/pathOr'); + +let input = []; + +process.stdin.resume(); +process.stdin.setEncoding('utf8'); + +process.stdin.on('data', (data) => { + input.push(data); +}); + +process.stdin.on('end', () => { + const str = input.join(''); + const events = JSON.parse(str); + + let uniquePages = []; + + events.forEach((page) => { + if (uniquePages.findIndex(({ id = null }) => id === page.id) === -1) { + uniquePages.push(page); + } + }); + + console.log(JSON.stringify(uniquePages)); +});