Skip to content

Commit 03c440b

Browse files
committed
Moved demo backend
1 parent f23aa22 commit 03c440b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+7485
-54
lines changed

.yarn/install-state.gz

16 Bytes
Binary file not shown.

examples/example_pro/example_backend/functions/.gitignore renamed to demo_backend/functions/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ typings/
77

88
node_modules/
99

10-
scripts
11-
1210
.runtimeconfig.json
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"algolia": {
3+
"search_key": "f084e6dcc154c04295c8124dbb797ff1",
4+
"api_key": "6a014a04544a84a51e2f78d8246b6244",
5+
"app_id": "Y6FR1MDSVW"
6+
},
7+
"mailchimp": {
8+
"api_url": "https://us5.api.mailchimp.com/3.0/lists/d356ca5a84"
9+
}
10+
}
597 KB
Binary file not shown.

examples/example_pro/example_backend/functions/package.json renamed to demo_backend/functions/package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
"main": "lib/index.js",
1515
"dependencies": {
1616
"@alpacahq/alpaca-trade-api": "^2.16.1",
17-
"@elastic/elasticsearch": "^8.7.0",
18-
"@google-cloud/firestore": "^6.4.1",
19-
"algoliasearch": "^4.14.2",
20-
"axios": "1.6.0",
17+
"@elastic/elasticsearch": "^8.13.0",
18+
"@google-cloud/firestore": "^6.8.0",
19+
"algoliasearch": "^4.23.2",
20+
"axios": "1.6.8",
2121
"cors": "^2.8.5",
22-
"dotenv": "^16.0.3",
23-
"express": "^4.18.2",
22+
"dotenv": "^16.4.5",
23+
"express": "^4.19.2",
2424
"fast-csv": "^4.3.6",
25-
"firebase-admin": "^11.5.0",
26-
"firebase-functions": "^4.2.1",
27-
"firebase-tools": "^11.23.1",
25+
"firebase-admin": "^11.11.1",
26+
"firebase-functions": "^4.8.1",
27+
"firebase-tools": "^11.30.0",
2828
"md5": "^2.3.0",
29-
"meilisearch": "^0.32.3",
30-
"typesense": "^1.5.3"
29+
"meilisearch": "^0.32.5",
30+
"typesense": "^1.7.2"
3131
},
3232
"devDependencies": {
33-
"typescript": "^4.9.4"
33+
"typescript": "^4.9.5"
3434
},
3535
"private": true
3636
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as admin from "firebase-admin";
2+
import { initServiceAccountFirestore } from "./util";
3+
4+
initServiceAccountFirestore(true);
5+
6+
const firestore = admin.firestore();
7+
8+
function getRandomInt(max: number) {
9+
return Math.floor(Math.random() * max);
10+
}
11+
12+
const addLikes = async () => {
13+
const productsIds = await firestore
14+
.collection("products")
15+
.get().then(snapshot => snapshot.docs.map(d => d.ref));
16+
firestore
17+
.collection("users")
18+
.get()
19+
.then((snapshot) => {
20+
snapshot.docs.forEach((doc) => {
21+
const count = getRandomInt(4);
22+
const products = Array.from({ length: count })
23+
.fill(0)
24+
.map((_) => productsIds[getRandomInt(productsIds.length)]);
25+
// console.log(products);
26+
doc.ref.update({ liked_products: products });
27+
});
28+
});
29+
};
30+
addLikes();
31+
32+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as admin from "firebase-admin";
2+
import * as dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
import { initServiceAccountFirestore } from "./util";
7+
import { addUserToMailchimp } from "../src/mailchimp";
8+
9+
initServiceAccountFirestore(true);
10+
11+
const firestore = admin.firestore();
12+
13+
const domains = new Set()
14+
// const startDate = new Date(2022, 1, 1);
15+
// list all users with the firebase admin sdk
16+
admin.auth().listUsers().then(async (listUsersResult) => {
17+
18+
// add user email domain to set
19+
listUsersResult.users.forEach((userRecord) => {
20+
if(userRecord?.email)
21+
domains.add(userRecord?.email.split("@")[1]);
22+
});
23+
console.log(domains);
24+
console.log(domains.size)
25+
26+
// const result = listUsersResult.users.filter((userRecord) => {
27+
// const date = new Date(Date.parse(userRecord.metadata.creationTime));
28+
// return date < startDate;
29+
// }).sort((a, b) => {
30+
// return Date.parse(b.metadata.creationTime) - Date.parse(a.metadata.creationTime);
31+
// });
32+
// for (const userRecord of result) {
33+
// const date = new Date(Date.parse(userRecord.metadata.creationTime));
34+
// console.log("user", userRecord.email, date);
35+
// try {
36+
// if (userRecord.email)
37+
// await addUserToMailchimp(userRecord.email, "import");
38+
// } catch (e) {
39+
// console.error(userRecord.email);
40+
// }
41+
// }
42+
});
43+
44+
//
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dotenv from 'dotenv';
2+
import { fetchDemoData } from "../src/alpaca";
3+
4+
dotenv.config();
5+
6+
fetchDemoData().then(console.log);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as admin from "firebase-admin";
2+
3+
import { initServiceAccountFirestore } from "./util";
4+
5+
initServiceAccountFirestore(true);
6+
7+
const firestore = admin.firestore();
8+
firestore.collection("/blog")
9+
.where("status", "==", "draft")
10+
.get()
11+
.then((snapshot) =>
12+
snapshot.docs.forEach(d => {
13+
d.ref.delete();
14+
}));
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import * as admin from "firebase-admin";
2+
import { initServiceAccountFirestore } from "./util";
3+
const crypto = require('crypto');
4+
5+
6+
initServiceAccountFirestore(true);
7+
8+
const firestore = admin.firestore();
9+
10+
// const categories = new Set<string>();
11+
// const catMap: Record<string, any> = {};
12+
13+
14+
const categories = {
15+
fiction: "Fiction",
16+
drama: "Drama",
17+
"fantasy-fiction": "Fantasy fiction",
18+
history: "History",
19+
religion: "Religion",
20+
"self-help": "Self-Help",
21+
"comics-graphic-novels": "Comics & Graphic Novels",
22+
"juvenile-fiction": "Juvenile Fiction",
23+
philosophy: "Philosophy",
24+
fantasy: "Fantasy",
25+
education: "Education",
26+
science: "Science",
27+
medical: "Medical",
28+
cooking: "Cooking",
29+
travel: "Travel"
30+
};
31+
32+
const generateBooks = async () => {
33+
const fs = require("fs")
34+
const csv = require("fast-csv");
35+
const data: any[] = []
36+
37+
fs.createReadStream("books.csv")
38+
.pipe(csv.parse({ headers: true }))
39+
.on("error", (error: any) => console.error(error))
40+
.on("data", (row: any) => data.push(row))
41+
.on("end", () => {
42+
data.sort((a: any, b: any) => {
43+
return parseFloat(b.average_rating) - parseFloat(a.average_rating)
44+
});
45+
const books = data
46+
.filter(b => parseFloat(b.ratings_count) > 1000)
47+
.slice(0, 200)
48+
.map((b) => {
49+
const catSlugs = b.categories.split(",").map((c: string) => slugify(c.trim()));
50+
const cat = catSlugs.find((c: string) => c in categories);
51+
let newVar = {
52+
isbn13: parseInt(b.isbn13),
53+
isbn10: parseInt(b.isbn10),
54+
title: b.title,
55+
subtitle: b.subtitle,
56+
authors: b.authors,
57+
thumbnail: b.thumbnail,
58+
description: b.description,
59+
published_year: parseInt(b.published_year),
60+
average_rating: parseFloat(b.average_rating),
61+
num_pages: parseInt(b.num_pages),
62+
ratings_count: parseInt(b.ratings_count),
63+
};
64+
if (cat) {
65+
// @ts-ignore
66+
newVar.category = cat;
67+
}
68+
return newVar;
69+
});
70+
71+
books.forEach((b: any) => {
72+
const hash = crypto.createHash('sha256', "sdfcerfewrf")
73+
.update(b.title)
74+
.digest('hex');
75+
firestore
76+
.collection("books")
77+
.doc(hash)
78+
// .doc(b.isbn13.toString())
79+
.set(b);
80+
});
81+
console.log(books);
82+
}
83+
);
84+
85+
86+
};
87+
generateBooks();
88+
89+
const slugify = function (text?: string) {
90+
if (!text) return "";
91+
const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;"
92+
const to = "aaaaaeeeeeiiiiooooouuuunc------"
93+
94+
for (var i = 0, l = from.length; i < l; i++) {
95+
text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
96+
}
97+
98+
return text
99+
.toString() // Cast to string
100+
.toLowerCase() // Convert the string to lowercase letters
101+
.trim() // Remove whitespace from both sides of a string
102+
.replace(/\s+/g, "-") // Replace spaces with -
103+
.replace(/&/g, "-") // Replace & with -
104+
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
105+
.replace(/\-\-+/g, "-") // Replace multiple - with single -
106+
.replace(/^\s+|\s+$/g, "");
107+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as admin from "firebase-admin";
2+
import axios from "axios";
3+
import { initServiceAccountFirestore } from "./util";
4+
5+
initServiceAccountFirestore(true);
6+
7+
const firestore = admin.firestore();
8+
9+
const generateUsers = async () => {
10+
const url = `https://randomuser.me/api/?results=500`;
11+
const response = await axios.get(url);
12+
console.log(response);
13+
response.data.results.forEach((data: any) => firestore
14+
.collection("users")
15+
.doc()
16+
.set({
17+
email: data.email,
18+
gender: data.gender,
19+
first_name: data.name.first,
20+
last_name: data.name.last,
21+
picture: data.picture,
22+
location: data.location,
23+
phone: data.phone
24+
}));
25+
26+
};
27+
generateUsers();
28+
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as admin from "firebase-admin";
2+
import * as dotenv from "dotenv";
3+
4+
dotenv.config();
5+
import {initServiceAccountFirestore} from "./util";
6+
import {indexInAlgolia} from "../src/indexing/algolia";
7+
8+
initServiceAccountFirestore(true);
9+
10+
const firestore = admin.firestore();
11+
firestore.collection("/blog")
12+
.get()
13+
.then((snapshot) =>
14+
snapshot.docs.forEach(d => {
15+
indexInAlgolia("blog", d.data(), d.id);
16+
}));
17+
18+
firestore.collection("/users")
19+
.get()
20+
.then((snapshot) =>
21+
snapshot.docs.forEach(d => {
22+
indexInAlgolia("users", d.data(), d.id);
23+
}));
24+
25+
firestore.collection("products")
26+
.get()
27+
.then((snapshot) =>
28+
snapshot.docs.forEach(d => {
29+
indexInAlgolia("products", d.data(), d.id);
30+
}));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as admin from "firebase-admin";
2+
import * as dotenv from "dotenv";
3+
4+
dotenv.config();
5+
import { initServiceAccountFirestore } from "./util";
6+
import { indexInElasticSearch } from "../src/indexing/elasticsearch";
7+
8+
initServiceAccountFirestore(true);
9+
10+
const firestore = admin.firestore();
11+
// firestore.collection("/blog")
12+
// .get()
13+
// .then(async (snapshot) => {
14+
// for (const d of snapshot.docs) {
15+
// await indexInElasticSearch("blog", d.data(), d.id);
16+
// }
17+
// });
18+
19+
firestore.collection("/users")
20+
.get()
21+
.then(async (snapshot) => {
22+
for (const d of snapshot.docs) {
23+
await indexInElasticSearch("users", d.data(), d.id);
24+
}
25+
});
26+
//
27+
// firestore.collection("products")
28+
// .get()
29+
// .then(async (snapshot) => {
30+
// for (const d of snapshot.docs) {
31+
// await indexInElasticSearch("products", d.data(), d.id);
32+
// }
33+
// });
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as admin from "firebase-admin";
2+
import * as dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
import { initServiceAccountFirestore } from "./util";
7+
import { indexInMeiliSearch } from "../src/meilisearch";
8+
9+
initServiceAccountFirestore(true);
10+
11+
const firestore = admin.firestore();
12+
firestore.collection("/blog")
13+
.get()
14+
.then((snapshot) =>
15+
snapshot.docs.forEach(d => {
16+
indexInMeiliSearch("blog", d.data(), d.id);
17+
}));
18+
19+
firestore.collection("/users")
20+
.get()
21+
.then((snapshot) =>
22+
snapshot.docs.forEach(d => {
23+
indexInMeiliSearch("users", d.data(), d.id);
24+
}));
25+
26+
firestore.collection("products")
27+
.get()
28+
.then((snapshot) =>
29+
snapshot.docs.forEach(d => {
30+
indexInMeiliSearch("products", d.data(), d.id);
31+
}));

0 commit comments

Comments
 (0)