-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.mjs
94 lines (76 loc) · 3.79 KB
/
stats.mjs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import config from './config.mjs';
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from "firebase/firestore";
import firebaseConfig from './firebaseConfig.json' assert { type: 'json' };
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
console.log(`Downloading listings from db...`)
let listings = [];
const dbSnapshot = await getDocs(collection(db, "dutch_properties"));
dbSnapshot.forEach((doc) => {
listings.push(doc.data());
})
const listingsByArea = listings.reduce((acc, listing) => {
acc[listing.area] = acc[listing.area] || [];
acc[listing.area].push(listing);
return acc;
}, {});
const areasByAveragePrice = Object.keys(listingsByArea).map(area => {
const sum = listingsByArea[area].reduce((acc, listing) => acc + listing.price, 0);
const average = Number((sum / listingsByArea[area].length).toFixed(0));
return { area, average };
}).sort((a, b) => a.average - b.average);
const areasByAveragePriceSqm = Object.keys(listingsByArea).map(area => {
const sum = listingsByArea[area].reduce((acc, listing) => acc + listing['price/m'], 0);
const average = Number((sum / listingsByArea[area].length).toFixed(1));
return { area, average };
}).sort((a, b) => a.average - b.average);
const areasByListings = Object.keys(listingsByArea).map(area => {
const listings = listingsByArea[area].length;
return { area, listings };
}).sort((a, b) => b.listings - a.listings);
const areasByPopToListing = Object.keys(listingsByArea).map(area => {
console.log(`area => ${area}`)
const listings = listingsByArea[area].length;
const target = config.targets.find(target => target.name === area);
const pop = target.population;
const popToListing = Number((pop / listings).toFixed(0));
return { area, popToListing };
}).sort((a, b) => a.popToListing - b.popToListing);
console.log(`Areas by average price =>`)
console.log(areasByAveragePrice)
console.log(`Areas by average price/sqm =>`)
console.log(areasByAveragePriceSqm)
console.log(`Areas by published listings =>`)
console.log(areasByListings)
console.log(`Areas by inhabitant per listing =>`)
console.log(areasByPopToListing)
console.log(`Gating areas by minimum ${config.minListings} listings, minimum ${config.minPopulation} population, maximum ${config.maxCommute} minute commute time...`)
const gatedAreasByPrice = areasByAveragePrice.filter(area => {
const target = config.targets.find(target => target.name === area.area);
const areaListings = listingsByArea[area.area];
return target.population >= config.minPopulation && areaListings.length >= config.minListings;
});
const gatedAreasBySqm = areasByAveragePriceSqm.filter(area => {
const target = config.targets.find(target => target.name === area.area);
const areaListings = listingsByArea[area.area];
return target.population >= config.minPopulation && areaListings.length >= config.minListings;
});
const gatedAreasByListings = areasByListings.filter(area => {
const target = config.targets.find(target => target.name === area.area);
const areaListings = listingsByArea[area.area];
return target.population >= config.minPopulation && areaListings.length >= config.minListings;
});
const gatedAreasByPopToListing = areasByPopToListing.filter(area => {
const target = config.targets.find(target => target.name === area.area);
const areaListings = listingsByArea[area.area];
return target.population >= config.minPopulation && areaListings.length >= config.minListings;
});
console.log(`Areas by average price =>`)
console.log(gatedAreasByPrice)
console.log(`Areas by average price/sqm =>`)
console.log(gatedAreasBySqm)
console.log(`Areas by published listings =>`)
console.log(gatedAreasByListings)
console.log(`Areas by inhabitant per listing =>`)
console.log(gatedAreasByPopToListing)