Added SSR

This commit is contained in:
Sebastian Seedorf
2022-08-17 11:52:56 +02:00
parent fe7e6d8ae2
commit 9660f0cf34
16 changed files with 1869 additions and 1834 deletions

View File

@@ -1,19 +1,44 @@
import {database} from "./start";
import {Dict, Group} from "../types";
import crypto from "crypto"
import {Filter, ObjectId, WithId} from "mongodb";
export async function setGroups(groups: Dict<Group>, ignored: string[], base: string[]) {
const collection = (await database.resolve())?.collection('setups')
export interface GroupData {
groups: Dict<Group>,
ignored: string[],
base: string[]
}
type InsertMeta<T> = T & {
createdOn: Date,
modifiedOn: Date,
accessedOn: Date
}
type GroupFilter = Filter<InsertMeta<GroupData>>
export async function setGroups(data: GroupData) {
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
if (!collection) return
await collection.deleteMany({}).catch(e => {
if (e.message !== 'ns not found') throw e
})
const result = await collection.insertOne({
groups,
ignored,
base,
...data,
createdOn: new Date(),
modifiedOn: new Date()
modifiedOn: new Date(),
accessedOn: new Date()
})
console.log(result.insertedId, result.insertedId.toString())
return result.insertedId.toString()
}
export async function getGroup(uuid: string) {
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
if (!collection) return
console.log(uuid)
const data = (await collection.findOne({
_id: new ObjectId(uuid)
} as GroupFilter)) ?? undefined
if (data) {
await collection.updateOne({_id: new ObjectId(uuid)}, { $set: {accessedOn: new Date()}})
}
return data
}

38
src/getServerSideProps.ts Normal file
View File

@@ -0,0 +1,38 @@
import {GetServerSideProps} from "next";
import {getGroup, setGroups} from "./database/groups";
import {Dict, Group} from "./types";
export interface PropsGroupProvider {
id: string
groups: Dict<Group>
ignored: string[]
base: string[]
}
export const getServerSidePropsGroupProvider: GetServerSideProps<PropsGroupProvider> = async ({query}) => {
const id = Array.isArray(query?.id) ? query.id[0] : query?.id
const data = id && await getGroup(id)
if (data) {
return {
props: {
id: data._id.toString(),
groups: data.groups,
ignored: data.ignored,
base: data.base
}
}
} else if (!id) {
const newId = await setGroups({groups: {}, base: [], ignored: []})
return {
redirect: {
destination: `?id=${newId}`,
permanent: false,
}
}
} else {
console.log(data)
return {
notFound: true
}
}
}

View File

@@ -4,30 +4,28 @@ import deepcopy from "deepcopy";
import {isNonNullable, shuffleInplace, sortByProperty, uniquify} from "../utils";
import seedrandom from "seedrandom";
function generateIds<T extends Dict<unknown>>(node: GraphNode<T>): GraphNodeWithIds<T> {
function generateIds<T extends Dict<unknown>>(node: GraphNode<T>, nodeUid: () => string): GraphNodeWithIds<T> {
return {
...node,
___uid: `${node.name.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${crypto.randomUUID().substring(0, 3)}`,
___uid: `${node.name.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${nodeUid()}`,
___uidInputs: [],
___uidOutputs: []
}
}
function generateAdditional<T extends Dict<unknown>>(uid: string, type: 'i'|'h'|'o'): GraphNodeWithIds<AdditionalNode> {
function generateAdditional<T extends Dict<unknown>>(uid: string, type: 'i'|'h'|'o', nodeUid: () => string): GraphNodeWithIds<AdditionalNode> {
return {
inputs: type !== 'i' ? [uid] : [],
outputs: type !== 'o' ? [uid] : [],
name: uid,
___type: type,
___uid: `${uid.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${crypto.randomUUID().substring(0, 3)}_${type}`,
___uid: `${uid.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${nodeUid()}_${type}`,
___uidInputs: [],
___uidOutputs: []
}
}
function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNode<T>[]): [string[][], Dict<GraphNodeWithIds<T>>] {
function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNode<T>[], nodeUid: () => string): [string[][], Dict<GraphNodeWithIds<T>>] {
const nodesWithId: Dict<GraphNodeWithIds<T>> = {}
const available = new Set(inputs)
let queue = [...nodes]
@@ -40,7 +38,7 @@ function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNo
queue = queue.filter((node) => {
const isPlaceable = node.inputs.every(input => available.has(input))
if (isPlaceable) {
const nodeWithId: GraphNodeWithIds<T> = generateIds(node)
const nodeWithId: GraphNodeWithIds<T> = generateIds(node, nodeUid)
rows[rows.length - 1].push(nodeWithId.___uid)
nodesWithId[nodeWithId.___uid] = nodeWithId
availableOfRow.push(...node.outputs)
@@ -58,7 +56,7 @@ function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNo
return [rows, nodesWithId]
}
function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesRaw: Dict<GraphNodeWithIds<T>>, inputs: string[], outputs: string[]): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesRaw: Dict<GraphNodeWithIds<T>>, inputs: string[], outputs: string[], nodeUid: () => string): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
const addedInputs: Dict<number> = {}
const res: string[][] = deepcopy([[], ...rowsRaw, []])
const resNodes: Dict<GraphNodeWithIds<T|AdditionalNode>> = deepcopy(nodesRaw)
@@ -67,13 +65,13 @@ function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesR
for (let rowInput of rowInputs) {
if (rowInput in addedInputs) {
for (let j = addedInputs[rowInput]+1; j < i+1; j++) {
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'h')
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'h', nodeUid)
res[j].push(nodeWithId.___uid)
resNodes[nodeWithId.___uid] = nodeWithId
}
addedInputs[rowInput] = i
} else if (inputs.includes(rowInput)) {
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'i')
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'i', nodeUid)
res[i].push(nodeWithId.___uid)
resNodes[nodeWithId.___uid] = nodeWithId
addedInputs[rowInput] = i
@@ -82,7 +80,7 @@ function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesR
const rowOutputs = uniquify(rowsRaw[i].flatMap(row => resNodes[row].outputs))
for (let rowOutput of rowOutputs) {
if (outputs?.includes(rowOutput)) {
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowOutput, 'o')
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowOutput, 'o', nodeUid)
res[i+2].push(nodeWithId.___uid)
resNodes[nodeWithId.___uid] = nodeWithId
}
@@ -232,12 +230,14 @@ export function findBest<T extends Dict<unknown>>(rowsWithInOut: string[][], nod
}
export function graphUntangled<T extends Dict<unknown>>(nodes: GraphNode<T>[], inputs: string[], outputs: string[], timeLimit = 0): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
const nodeSeed = seedrandom.alea("node")
const nodeUid = () => Math.abs(nodeSeed.int32()).toString(16).substring(0, 3)
//console.log('---------------')
const [rowsRaw, nodesRaw] = splitIntoRows(inputs, nodes)
const [rowsRaw, nodesRaw] = splitIntoRows(inputs, nodes, nodeUid)
//console.log("Step 1")
//console.table(rowsRaw)
//console.table(nodesRaw)
const [rowsWithInOut, nodesWithInOut] = addAdditionalNodes(rowsRaw, nodesRaw, inputs, outputs)
const [rowsWithInOut, nodesWithInOut] = addAdditionalNodes(rowsRaw, nodesRaw, inputs, outputs, nodeUid)
//console.log("Step 2")
//console.table(rowsWithInOut)
//console.table(nodesWithInOut)