-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentGraph.ts
More file actions
113 lines (92 loc) · 4.25 KB
/
contentGraph.ts
File metadata and controls
113 lines (92 loc) · 4.25 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { GoogleSpreadsheetWorksheet } from "google-spreadsheet";
import { uploadCsv } from "../csvUpload";
import { CurrentReportDoc } from "../googleDocsWrapper";
import { ContentGraph, ContentLink, ContentNode, generateContentGraph } from "../graph";
type ContentGraphRow = {
Type: string,
Title: string,
'Degree of connectivity': number,
Link: string,
Edit: string
};
type ContentLinksRow = { [key in ContentLinksHeaderTitle]: string };
type ContentGraphHeaderTitle = 'Type' | 'Title' | 'Degree of connectivity' | 'Link' | 'Edit';
type ContentLinksHeaderTitle = 'Item1' | 'Id1' | 'Link1' | 'Edit1' | 'Item2' | 'Id2' | 'Link2' | 'Edit2';
const contentGraphHeaderFields: ContentGraphHeaderTitle[] = ['Title', 'Type', 'Degree of connectivity', 'Link', 'Edit'];
const contentLinksHeaderFields: ContentLinksHeaderTitle[] = ['Item1', 'Id1', 'Link1', 'Edit1', 'Item2', 'Id2', 'Link2', 'Edit2'];
export async function runContentGraphReports(): Promise<void> {
const currentReportDoc = CurrentReportDoc.instance;
const graph = await generateContentGraph();
await runConnectivityReport(graph, await currentReportDoc.getSheet('Content Graph'));
await runLinksReport(graph, await currentReportDoc.getSheet('Connection List'));
}
async function runConnectivityReport(graph: ContentGraph, sheet: GoogleSpreadsheetWorksheet): Promise<void> {
const rows: ContentGraphRow[] = [];
for (const node of graph.nodes) {
const row: ContentGraphRow = {
Type: node.type,
Title: node.name,
'Degree of connectivity': countLinks(node.id, graph.links),
Link: hubUrl(node),
Edit: contentfulUrl(node),
}
rows.push(row);
}
rows.sort((a, b) => a["Degree of connectivity"] - b["Degree of connectivity"]);
uploadCsv(rows, 'Content Graph');
await sheet.clear();
await sheet.setHeaderRow(contentGraphHeaderFields);
await sheet.addRows(rows);
}
function countLinks(nodeId: string, links: ContentLink[]): number {
return links.filter(link => link.source === nodeId || link.target === nodeId).length
}
function hubUrl(node: ContentNode): string {
if (!process.env.CONTENTFUL_SPACE_ENV
|| (process.env.CONTENTFUL_SPACE_ENV !== 'dev' && process.env.CONTENTFUL_SPACE_ENV !== 'prod')) {
return '';
}
let baseUrl = ';'
switch (process.env.CONTENTFUL_SPACE_ENV) {
case 'dev':
baseUrl = 'https://research-hub-dev.connect.test.auckland.ac.nz';
break;
case 'prod':
baseUrl = 'https://research-hub.auckland.ac.nz';
break;
}
return `${baseUrl}/${node.type}/${node.slug}`
}
function contentfulUrl(node: ContentNode): string {
if (!process.env.CONTENTFUL_SPACE_ID || !process.env.CONTENTFUL_SPACE_ENV) {
return '';
}
return `https://app.contentful.com/spaces/${process.env.CONTENTFUL_SPACE_ID}/environments/${process.env.CONTENTFUL_SPACE_ENV}/entries/${node.id}`
}
async function runLinksReport(graph: ContentGraph, sheet: GoogleSpreadsheetWorksheet): Promise<void> {
const rows: ContentLinksRow[] = [];
for (const link of graph.links) {
const sourceNode = graph.nodes.find(node => node.id === link.source);
const targetNode = graph.nodes.find(node => node.id === link.target);
if (sourceNode === undefined || targetNode === undefined) continue;
const row: ContentLinksRow = {
Item1: sourceNode.name,
Id1: link.source,
Link1: `https://research-hub.auckland.ac.nz/${sourceNode.type}/${sourceNode.slug}`,
Edit1: process.env.CONTENTFUL_SPACE_ID
? `https://app.contentful.com/spaces/${process.env.CONTENTFUL_SPACE_ID}/entries/${sourceNode.id}`
: '',
Item2: targetNode.name,
Id2: link.target,
Link2: `https://research-hub.auckland.ac.nz/${targetNode.type}/${targetNode.slug}`,
Edit2: process.env.CONTENTFUL_SPACE_ID
? `https://app.contentful.com/spaces/${process.env.CONTENTFUL_SPACE_ID}/entries/${targetNode.id}`
: ''
}
rows.push(row);
}
uploadCsv(rows, 'Connection List');
await sheet.clear();
await sheet.setHeaderRow(contentLinksHeaderFields);
await sheet.addRows(rows);
}