Skip to content

Instantly share code, notes, and snippets.

@fibo
Last active March 2, 2017 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fibo/861df9c835aa2480bfe206bad06e4a45 to your computer and use it in GitHub Desktop.
Save fibo/861df9c835aa2480bfe206bad06e4a45 to your computer and use it in GitHub Desktop.
Problem: array of streams. There are two input CSV files streamed to one output JSON file stream. THe code works, but what about scaling the number of inputs? Solution: reduce an array of streams.
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
/**
* Converts a tree path to number
*
* BE.2.8 -> 2 + 100 * 8 + 100^2 * 0 + 100^3 * 0
*
* @param {String} treePath
*
* @returns {Number}
*/
function treePathToNumber (treePath) {
const fields = treePath.split('\.')
const num1 = parseInt(fields[1]) || 0
const num2 = parseInt(fields[2]) || 0
const num3 = parseInt(fields[3]) || 0
const num4 = parseInt(fields[4]) || 0
const base = 100
return num1 + base * num2 + Math.pow(base, 2) * num3 + Math.pow(base, 3) * num4
}
const taxonomyItFile = path.join(__dirname, 'taxonomy_it.csv')
const taxonomyEsFile = path.join(__dirname, 'taxonomy_es.csv')
const taxonomyFile = path.join(__dirname, 'taxonomy.json')
const taxonomyStream = fs.createWriteStream(taxonomyFile, {
flags: 'w',
defaultEncoding: 'utf8',
autoClose: true
})
const taxonomyItStream = fs.createReadStream(taxonomyItFile)
/**
* Converts a taxonmy line to JSON string
*
* @param {String} line
*
* @returns {String}
*/
function lineToJSON (line) {
const fields = line.split('\t')
const name = fields[0]
const treePath = fields[1]
const id = treePathToNumber(treePath)
return JSON.stringify({
id,
name,
price: 0.1
})
}
var isFirstLine = true
var lastChunk = ''
taxonomyItStream.on('data', function (data) {
if (isFirstLine) {
taxonomyStream.write('{\n"IT":[\n')
isFirstLine = false
} else {
taxonomyStream.write('\n,')
}
var chunks = data.toString('utf8').split('\n')
var numChunks = chunks.length
// Concat previous last chunk and first one.
taxonomyStream.write(lineToJSON(lastChunk + chunks[0]))
lastChunk = chunks[numChunks - 1]
for (var i = 1; i < numChunks - 1; i++) {
if (!isFirstLine) taxonomyStream.write('\n,')
taxonomyStream.write(lineToJSON(chunks[i]))
}
})
taxonomyItStream.on('close', function () {
taxonomyStream.write('\n],')
// Reset vars.
isFirstLine = true
lastChunk = ''
const taxonomyEsStream = fs.createReadStream(taxonomyEsFile)
taxonomyEsStream.on('data', function (data) {
if (isFirstLine) {
taxonomyStream.write('\n"ES":[\n')
isFirstLine = false
} else {
taxonomyStream.write('\n,')
}
var chunks = data.toString('utf8').split('\n')
var numChunks = chunks.length
// Concat previous last chunk and first one.
taxonomyStream.write(lineToJSON(lastChunk + chunks[0]))
lastChunk = chunks[numChunks - 1]
for (var i = 1; i < numChunks - 1; i++) {
if (!isFirstLine) taxonomyStream.write('\n,')
taxonomyStream.write(lineToJSON(chunks[i]))
}
})
taxonomyEsStream.on('close', function () {
taxonomyStream.write('\n]\n}')
taxonomyStream.end()
})
})
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 1 column, instead of 2. in line 2.
MOBILE_AUDIENCE BE.1 Mobile Audience This category contains people whose behaviour was determinated from their Mobile device
AUTOMOTIVE BE.1.1 Mobile Audience Automotive This category contains people who are interested in automotive
CAR_DEALER BE.1.1.1 Mobile Audience Automotive In-Market Auto Buyer In-market auto shoppers who have visited car dealerships for a new vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
CHRYSLER BE.1.1.1.10 Mobile Audience Automotive In-Market Auto Buyer Chrysler In-market auto shoppers who have visited car dealerships for a new Chrysler vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
RETAIL BE.1.7 Mobile Audience Retail Shoppers who often visited different kind of retailers (e.g. electronic retailers, grocery shops, big box stores, apparel shops etc.)
APPAREL_RETAIL BE.1.7.1 Mobile Audience Retail Apparel Shoppers Shoppers who have visited Apparel Shops. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
PIMKIE BE.1.7.1.106 Mobile Audience Retail Apparel Shoppers Pimkie Shoppers who have visited Pimkie. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
PRIMARK BE.1.7.1.109 Mobile Audience Retail Apparel Shoppers Primark Shoppers who have visited Primark. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
PROMOD BE.1.7.1.110 Mobile Audience Retail Apparel Shoppers Promod Shoppers who have visited Promod. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
PULL_AND_BEAR BE.1.7.1.111 Mobile Audience Retail Apparel Shoppers Pull and Bear Shoppers who have visited Pull and Bear. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
PUMA_ITALIA BE.1.7.1.112 Mobile Audience Retail Apparel Shoppers Puma Shoppers who have visited Puma. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
STRADIVARIUS BE.1.7.1.124 Mobile Audience Retail Apparel Shoppers Stradivarius Shoppers who have visited Stradivarius. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
SUPERDRY BE.1.7.1.125 Mobile Audience Retail Apparel Shoppers Superdry Shoppers who have visited Superdry. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
THE_NORTH_FACE BE.1.7.1.131 Mobile Audience Retail Apparel Shoppers The North Face Shoppers who have visited The North Face. Members of this category on average visit an apparel shop at least once per month, spending more than 10 minutes at each visit.
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 1 column, instead of 2. in line 2.
MOBILE_AUDIENCE BE.1 Mobile Audience This category contains people whose behaviour was determinated from their Mobile device
AUTOMOTIVE BE.1.1 Mobile Audience Automotive This category contains people who are interested in automotive
CAR_DEALER BE.1.1.1 Mobile Audience Automotive In-Market Auto Buyer In-market auto shoppers who have visited car dealerships for a new vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
CITROEN BE.1.1.1.11 Mobile Audience Automotive In-Market Auto Buyer Citroen In-market auto shoppers who have visited car dealerships for a new Citroen vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
DACIA BE.1.1.1.12 Mobile Audience Automotive In-Market Auto Buyer Dacia In-market auto shoppers who have visited car dealerships for a new Dacia vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
DR BE.1.1.1.14 Mobile Audience Automotive In-Market Auto Buyer Dr In-market auto shoppers who have visited car dealerships for a new Dr vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
DS BE.1.1.1.15 Mobile Audience Automotive In-Market Auto Buyer DS In-market auto shoppers who have visited car dealerships for a new DS vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
FERRARI BE.1.1.1.16 Mobile Audience Automotive In-Market Auto Buyer Ferrari In-market auto shoppers who have visited car dealerships for a new Ferrari vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
FIAT BE.1.1.1.17 Mobile Audience Automotive In-Market Auto Buyer Fiat-Abarth In-market auto shoppers who have visited car dealerships for a new Fiat vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
FORD BE.1.1.1.18 Mobile Audience Automotive In-Market Auto Buyer Ford In-market auto shoppers who have visited car dealerships for a new Ford vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
ALFA_ROMEO BE.1.1.1.2 Mobile Audience Automotive In-Market Auto Buyer Alfa romeo In-market auto shoppers who have visited car dealerships for a new Alfa Romeo vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
HONDA BE.1.1.1.20 Mobile Audience Automotive In-Market Auto Buyer Honda In-market auto shoppers who have visited car dealerships for a new Honda vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
HYUNDAI BE.1.1.1.21 Mobile Audience Automotive In-Market Auto Buyer Hyundai In-market auto shoppers who have visited car dealerships for a new Hyundai vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
INFINITI BE.1.1.1.22 Mobile Audience Automotive In-Market Auto Buyer Infiniti In-market auto shoppers who have visited car dealerships for a new Infiniti vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
JAGUAR BE.1.1.1.23 Mobile Audience Automotive In-Market Auto Buyer Jaguar In-market auto shoppers who have visited car dealerships for a new Jaguar vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
JEEP BE.1.1.1.24 Mobile Audience Automotive In-Market Auto Buyer Jeep In-market auto shoppers who have visited car dealerships for a new Jeep vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
KIA BE.1.1.1.25 Mobile Audience Automotive In-Market Auto Buyer Kia In-market auto shoppers who have visited car dealerships for a new Kia vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
LAMBORGHINI BE.1.1.1.26 Mobile Audience Automotive In-Market Auto Buyer Lamborghini In-market auto shoppers who have visited car dealerships for a new Lamborghini vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
LANCIA BE.1.1.1.27 Mobile Audience Automotive In-Market Auto Buyer Lancia In-market auto shoppers who have visited car dealerships for a new Lancia vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
LAND_ROVER BE.1.1.1.28 Mobile Audience Automotive In-Market Auto Buyer Land Rover In-market auto shoppers who have visited car dealerships for a new Land Rover vehicle. Profiles in this audience visited at least one car dealer in the last month, spending more than 30 minutes per visit. .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment