Skip to content

Commit

Permalink
Path model fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefs committed Oct 18, 2023
1 parent 4f39647 commit fea6179
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
62 changes: 35 additions & 27 deletions src/models/Path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Types, Document } from 'mongoose';
import { UrlType } from '@derzis/common';
import { urlValidator } from '@derzis/common';
import { prop, index, pre, getModelForClass } from '@typegoose/typegoose';
import {
TripleClass,
ProcessClass,
Triple,
Process,
ProcessTriple,
ProcessClass,
TripleDocument,
} from '@derzis/models';

@pre<PathClass>('save', function () {
Expand All @@ -32,12 +33,12 @@ class ResourceCount {
@prop({ default: 0 })
public count!: number;

@prop({ default: [] })
public elems!: UrlType[];
@prop({ default: [], validate: urlValidator })
public elems!: string[];
}
class HeadClass {
@prop({ required: true })
public url!: UrlType;
@prop({ required: true, validate: urlValidator })
public url!: string;

@prop({ required: true })
public domain!: string;
Expand All @@ -51,21 +52,31 @@ type RecursivePartial<T> = {
: T[P];
};

type PathSkeleton = Pick<PathClass, 'processId' | 'seed' | 'head'> &
RecursivePartial<PathClass> & {
predicates: Pick<ResourceCount, 'elems'>;
nodes: Pick<ResourceCount, 'elems'>;
};

class PathClass {
_id!: Types.ObjectId;
createdAt!: Date;
updatedAt!: Date;

@prop({ required: true })
public processId!: string;

@prop({ required: true })
public seed!: UrlType;
@prop({ required: true, validate: urlValidator })
public seed!: string;

@prop({ required: true })
public head!: HeadClass;

@prop({ default: [] })
public predicates!: ResourceCount;

@prop()
public lastPredicate?: UrlType;
@prop({ validate: urlValidator })
public lastPredicate?: string;

@prop({ default: [] })
public nodes!: ResourceCount;
Expand Down Expand Up @@ -102,29 +113,26 @@ class PathClass {
return true;
}

public tripleIsOutOfBounds(
t: TripleClass,
process: ProcessDocumentClass
): boolean {
const pathPreds: Set<UrlType> = new Set(this.predicates.elems);
public tripleIsOutOfBounds(t: TripleClass, process: ProcessClass): boolean {
const pathPreds: Set<string> = new Set(this.predicates.elems);
return (
this.nodes.count >= process.params.maxPathLength ||
(!pathPreds.has(t.predicate) &&
this.predicates.count >= process.params.maxPathProps)
);
}

public extendWithExistingTriples(): Promise<{
newPaths: PathClass[];
procTriples: string[];
public async extendWithExistingTriples(): Promise<{
newPaths: PathSkeleton[];
procTriples: Types.ObjectId[];
}> {
// if path has outOfBounds triple, try to extend with that
if (!!this.outOfBounds) {
const t: TripleClass | null = await Triple.findById(this.outOfBounds);
const process = await Process.findOne({ pid: this.processId });
if (
t &&
!this.tripleIsOutOfBounds(t, process) &&
!this.tripleIsOutOfBounds(t, process!) &&
process?.whiteBlackListsAllow(t!)
) {
const newHeadUrl: string =
Expand All @@ -150,13 +158,13 @@ class PathClass {
}
}
// find triples which include the head but dont belong to the path yet
let triples: TripleClass[] = await Triple.find({
let triples: TripleDocument[] = await Triple.find({
nodes: { $eq: this.head.url, $nin: this.nodes.elems },
});
return this.extend(triples);
}

public copy(): RecursivePartial<PathClass> {
public copy(): PathSkeleton {
const copy = {
processId: this.processId,
seed: this.seed,
Expand All @@ -167,10 +175,10 @@ class PathClass {
return copy;
}

public extend(
triples: TripleClass[]
): Promise<{ newPaths: PathClass[]; procTriples: string[] }> {
let newPaths: { [prop: string]: { [newHead: string]: PathClass } } = {};
public async extend(
triples: TripleDocument[]
): Promise<{ newPaths: PathSkeleton[]; procTriples: Types.ObjectId[] }> {
let newPaths: { [prop: string]: { [newHead: string]: PathSkeleton } } = {};
let procTriples: Types.ObjectId[] = [];
const process = await Process.findOne({ pid: this.processId });

Expand All @@ -187,7 +195,7 @@ class PathClass {
const np = this.copy();
np.head.url = newHeadUrl;

if (this.tripleIsOutOfBounds(t, process)) {
if (this.tripleIsOutOfBounds(t, process!)) {
np.outOfBounds = t._id;
} else {
procTriples.push(t._id);
Expand All @@ -199,7 +207,7 @@ class PathClass {
newPaths[prop][newHeadUrl] = np;
}
}
const nps: RecursivePartial<PathClass>[] = [];
const nps: PathSkeleton[] = [];
Object.values(newPaths).forEach((x) =>
Object.values(x).forEach((y) => nps.push(y))
);
Expand Down
12 changes: 6 additions & 6 deletions src/models/Process.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Types, Document } from 'mongoose';
import { Resource } from './Resource';
import { Triple, TripleClass } from './Triple';
import { Triple, TripleClass, TripleDocument } from './Triple';
import { humanize } from 'humanize-digest';
import { Domain } from './Domain';
import { Path, PathDocument } from './Path';
Expand Down Expand Up @@ -38,11 +38,11 @@ class NotificationClass {
public ssePath?: string;
}
class ParamsClass {
@prop({ default: 2 })
public maxPathLength?: number;
@prop({ default: 2, required: true })
public maxPathLength!: number;

@prop({ default: 1 })
public maxPathProps?: number;
@prop({ default: 1, required: true })
public maxPathProps!: number;

@prop({ default: [] })
public whiteList?: string[];
Expand Down Expand Up @@ -162,7 +162,7 @@ class ProcessClass {
}
}

public async extendPaths(triplesByNode: { [url: string]: TripleClass[] }) {
public async extendPaths(triplesByNode: { [url: string]: TripleDocument[] }) {
const newHeads = Object.keys(triplesByNode);
const paths = await Path.find({
processId: this.pid,
Expand Down
6 changes: 6 additions & 0 deletions src/models/Triple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
index,
getModelForClass,
ReturnModelType,
mongoose,
} from '@typegoose/typegoose';
import { Document } from 'cheerio';

Expand All @@ -23,6 +24,11 @@ import { Document } from 'cheerio';
@index({ nodes: 1 })
@index({ subject: 1, predicate: 1, object: 1 }, { unique: true })
class TripleClass {
_id!: mongoose.Types.ObjectId;

createdAt!: Date;
updatedAt!: Date;

@prop({ required: true, validate: urlValidator })
public subject!: string;

Expand Down

0 comments on commit fea6179

Please sign in to comment.