Skip to content

Commit 865335e

Browse files
Acq 2276 generate catalog variant (#145)
* javascript for generating the nav * translate rakefile catalog update to JS, and generate updated yamls * add sorting * make suggested changes * add category mapping * add catalog variant and source/destination category mappings * remove old script * resolve merge conflict
1 parent 0c28dd7 commit 865335e

File tree

5 files changed

+19803
-0
lines changed

5 files changed

+19803
-0
lines changed

scripts/catalogV2.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
const axios = require('axios');
2+
const path = require('path');
3+
const fs = require('fs');
4+
const yaml = require('js-yaml');
5+
6+
require('dotenv').config();
7+
8+
PLATFORM_API_URL = "https://platform.segmentapis.com"
9+
10+
const slugify = (displayName) => {
11+
let slug = displayName.toLowerCase().replace(/\s+/g, '-')
12+
if (slug === '.net') slug = 'net'
13+
if (slug === 'roku-(alpha)') slug = 'roku'
14+
return slug
15+
}
16+
17+
const getCatalog = async (url, page_token = "") => {
18+
try {
19+
const res = await axios.get(url, {
20+
headers: {
21+
'Content-Type': 'application/json',
22+
'Authorization': `Bearer ${process.env.PLATFORM_API_TOKEN}`
23+
},
24+
params: {
25+
page_token,
26+
page_size: 100
27+
}
28+
});
29+
return res.data
30+
} catch (error) {
31+
console.log(error)
32+
}
33+
}
34+
35+
const updateSources = async () => {
36+
let sources = []
37+
let sourcesUpdated = []
38+
let categories = new Set();
39+
let sourceCategories = []
40+
let nextPageToken = null
41+
42+
while (nextPageToken !== "") {
43+
const res = await getCatalog(`${PLATFORM_API_URL}/v1beta/catalog/sources`, nextPageToken)
44+
sources = sources.concat(res.sources)
45+
nextPageToken = res.next_page_token
46+
}
47+
48+
sources.sort((a, b) => {
49+
if(a.display_name < b.display_name) { return -1; }
50+
if(a.display_name > b.display_name) { return 1; }
51+
return 0;
52+
})
53+
54+
const libraryCategories = [
55+
'server',
56+
'mobile',
57+
'ott',
58+
'roku',
59+
'website'
60+
]
61+
62+
sources.forEach(source => {
63+
let slug = slugify(source.display_name)
64+
let url = ''
65+
let mainCategory = source.categories[0] ? source.categories[0].toLowerCase() : ''
66+
67+
if (libraryCategories.includes(mainCategory)) {
68+
url = `connections/sources/catalog/libraries/${mainCategory}/${slug}`
69+
} else {
70+
url = `connections/sources/catalog/cloud-apps/${slug}`
71+
}
72+
73+
let updatedSource = {
74+
display_name: source.display_name,
75+
slug,
76+
name: source.name,
77+
description: source.description,
78+
url,
79+
logo: {
80+
url: source.logos.logo
81+
},
82+
mark: {
83+
url: source.logos.mark
84+
},
85+
categories: source.categories
86+
}
87+
sourcesUpdated.push(updatedSource)
88+
89+
// add unique source categories to set
90+
source.categories.reduce((s, e) => s.add(e), categories);
91+
})
92+
93+
const sourceArray = Array.from(categories)
94+
sourceArray.forEach(category => {
95+
sourceCategories.push({
96+
display_name: category,
97+
slug: slugify(category)
98+
})
99+
})
100+
101+
// Create source catalog yaml file
102+
const options = { noArrayIndent: true };
103+
let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
104+
output += yaml.safeDump({ items: sourcesUpdated }, options);
105+
fs.writeFileSync(path.resolve(__dirname, `../src/_data/catalogV2/sources.yml`), output);
106+
107+
// Create source-category mapping yaml file
108+
output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
109+
output += yaml.safeDump({ items: sourceCategories }, options);
110+
fs.writeFileSync(path.resolve(__dirname, `../src/_data/catalogV2/source_categories.yml`), output);
111+
}
112+
113+
const updateDestinations = async () => {
114+
let destinations = []
115+
let destinationsUpdated = []
116+
let destinationCategories = new Set();
117+
let nextPageToken = null
118+
119+
while (nextPageToken !== "") {
120+
const res = await getCatalog(`${PLATFORM_API_URL}/v1beta/catalog/destinations`, nextPageToken)
121+
destinations = destinations.concat(res.destinations)
122+
nextPageToken = res.next_page_token
123+
}
124+
destinations.sort((a, b) => {
125+
if(a.display_name < b.display_name) { return -1; }
126+
if(a.display_name > b.display_name) { return 1; }
127+
return 0;
128+
})
129+
destinations.forEach(destination => {
130+
let slug = slugify(destination.display_name)
131+
132+
let categories = [destination.categories.primary, destination.categories.secondary, ...destination.categories.additional]
133+
categories = categories.filter(category => category != '')
134+
135+
let updatedDestination = {
136+
display_name: destination.display_name,
137+
slug,
138+
name: destination.name,
139+
description: destination.description,
140+
url: `connections/destinations/catalog/${slug}`,
141+
status: destination.status,
142+
logo: {
143+
url: destination.logos.logo
144+
},
145+
mark: {
146+
url: destination.logos.mark
147+
},
148+
categories
149+
}
150+
destinationsUpdated.push(updatedDestination)
151+
152+
// add unique destination categories to set
153+
categories.reduce((s, e) => s.add(e), destinationCategories);
154+
})
155+
156+
const destinationArray = Array.from(categories)
157+
destinationArray.forEach(category => {
158+
destinationCategories.push({
159+
display_name: category,
160+
slug: slugify(category)
161+
})
162+
})
163+
164+
// Create destination catalog yaml file
165+
const options = { noArrayIndent: true };
166+
let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
167+
output += yaml.safeDump({ items: destinationsUpdated }, options);
168+
fs.writeFileSync(path.resolve(__dirname, `../src/_data/catalogV2/destinations.yml`), output);
169+
170+
// Create source-category mapping yaml file
171+
let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
172+
output += yaml.safeDump({ items: destinationCategories }, options);
173+
fs.writeFileSync(path.resolve(__dirname, `../src/_data/catalogV2/destination_categories.yml`), output);
174+
}
175+
176+
updateSources()
177+
updateDestinations()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT
2+
items:
3+
- display_name: Advertising
4+
slug: advertising
5+
- display_name: Attribution
6+
slug: attribution
7+
- display_name: Analytics
8+
slug: analytics
9+
- display_name: Enrichment
10+
slug: enrichment
11+
- display_name: A/B Testing
12+
slug: a/b-testing
13+
- display_name: Email Marketing
14+
slug: email-marketing
15+
- display_name: Performance Monitoring
16+
slug: performance-monitoring
17+
- display_name: Video
18+
slug: video
19+
- display_name: Referrals
20+
slug: referrals
21+
- display_name: Heatmaps & Recordings
22+
slug: heatmaps-&-recordings
23+
- display_name: SMS & Push Notifications
24+
slug: sms-&-push-notifications
25+
- display_name: Personalization
26+
slug: personalization
27+
- display_name: Customer Success
28+
slug: customer-success
29+
- display_name: Surveys
30+
slug: surveys
31+
- display_name: Tag Managers
32+
slug: tag-managers
33+
- display_name: Livechat
34+
slug: livechat
35+
- display_name: CRM
36+
slug: crm
37+
- display_name: Raw Data
38+
slug: raw-data
39+
- display_name: Feature Flagging
40+
slug: feature-flagging
41+
- display_name: Marketing Automation
42+
slug: marketing-automation
43+
- display_name: Deep Linking
44+
slug: deep-linking
45+
- display_name: Security & Fraud
46+
slug: security-&-fraud
47+
- display_name: Email
48+
slug: email

0 commit comments

Comments
 (0)