Commit a8ebc816 authored by Steve Streza's avatar Steve Streza
Browse files

Added search to folder accessor

parent 7d0bf964
......@@ -85,5 +85,35 @@ Yargs
})
}
})
.command("search [journal] [term]", "searches the messages in the journal for a given term", yargs => yargs
.positional("journal", {
type: "string",
default: "default",
describe: "The name of the journal (defaults to \"default\")"
})
.positional("term", {
type: "string",
describe: "The term to search for"
})
, (argv: any) => {
const accessor = new FolderAccessor(argv.path);
const store = new DataStore(accessor);
const term: string = argv.term
const journal: string = argv.journal
if (journal) {
store.accessor.search(term, journal)
.subscribe({
next: entry => {
if (entry.date) {
console.log("" + entry.date.toLocaleDateString() + " " + entry.date.toLocaleTimeString() + ": " + entry.data.message)
}
else {
console.log("" + entry.data.message)
}
}
})
}
})
.help()
.argv
......@@ -286,6 +286,11 @@
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true
},
"fuse.js": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-3.3.0.tgz",
"integrity": "sha512-ESBRkGLWMuVkapqYCcNO1uqMg5qbCKkgb+VS6wsy17Rix0/cMS9kSOZoYkjH8Ko//pgJ/EEGu0GTjk2mjX2LGQ=="
},
"get-caller-file": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz",
......
......@@ -21,6 +21,7 @@
},
"dependencies": {
"bluebird": "^3.5.3",
"fuse.js": "^3.3.0",
"mkdirp": "^0.5.1",
"readdir-rx": "0.1.2",
"rxjs": "^6.3.3",
......
import { Observable } from "rxjs"
export interface RecordData {
message: string
}
export interface Record {
date?: Date,
data: any
data: RecordData
}
export interface Journal {
......@@ -14,4 +18,5 @@ export interface Accessor {
create: (journalSlug: string, record: Record) => Observable<boolean>,
listJournals: () => Observable<Journal>,
list: (journalSlug: string, since?: Date) => Observable<Record>
search: (searchTerm: string, journalSlug: string, since?: Date) => Observable<Record>
}
import { Observable, from } from "rxjs";
import { Observable, from, empty } from "rxjs";
import { flatMap, map } from "rxjs/operators";
type Fetcher = typeof fetch;
......@@ -64,4 +64,8 @@ export default class APIAccessor implements Accessor {
flatMap(records => from(records))
);
}
search(/* searchTerm: string, journal: string, startDate?: Date */): Observable<Record> {
return empty();
}
}
......@@ -14,4 +14,8 @@ export default class EmptyAccessor implements Accessor {
list(): Observable<Record> {
return empty();
}
search(/* searchTerm: string, journal: string, startDate?: Date */): Observable<Record> {
return empty();
}
}
......@@ -2,7 +2,7 @@ import * as Bluebird from "bluebird";
import { Observable, from, empty } from "rxjs";
import { map, flatMap, concatMap, filter } from "rxjs/operators";
import { Record, Accessor } from "../accessor";
import { Record, Accessor, RecordData } from "../accessor";
import { join as joinPath, basename, dirname } from "path";
......@@ -11,6 +11,8 @@ import * as fs from "fs";
import readdir from "readdir-rx";
import { Journal } from "../accessor";
import * as Fuse from "fuse.js"
const mkdirp = Bluebird.promisify(mkdir_p);
const writeFile = Bluebird.promisify(fs.writeFile) as (
file: string,
......@@ -80,4 +82,21 @@ export default class FolderAccessor implements Accessor {
listJournals(): Observable<Journal> {
return empty();
}
search(searchTerm: string, journal: string, startDate?: Date): Observable<Record> {
const opts: Fuse.FuseOptions<RecordData> = {
keys: ['message'],
id: "horse"
}
return this.list(journal, startDate)
.pipe(
filter(record => {
const fuse = new Fuse([record.data], opts)
const result = fuse.search(searchTerm)
return !!(result.length)
}
)
)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment