Restarting from scratch

This commit is contained in:
vdv committed 2022-10-17 10:08:00 +02:00
1 parent ed951208e8
commit a4bd11aab0
88 files changed
+1714 -17027

No files matched your search

-101
View File
@@ -1,101 +0,0 @@
import { initDb } from '.';
import PouchDB from 'pouchdb';
import { appendTrkpt, deleteCurrent, saveCurrent } from './gpx';
import PouchDBFind from 'pouchdb-find';
import { mkdtempSync, rmdirSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
PouchDB.plugin(PouchDBFind);
//PouchDB.plugin(require('pouchdb-find'));
// export class PouchService {
// constructor() {
// PouchDB.plugin(PouchDBFind);
// }
// }
// jest.useFakeTimers();
const tmpDir = mkdtempSync(join(tmpdir(), 'dyomedea-testDB'));
var db: PouchDB.Database;
beforeEach(async () => {
db = new PouchDB(tmpDir);
await initDb(db, () => {});
});
afterEach(async () => {
// await db.close();
await db.destroy();
});
describe('Checking that trkpts are beeing inserted', () => {
test('and that we have two documents after the first call', async () => {
await appendTrkpt(db, {});
const results = await db.find({ selector: {} });
const docs = results.docs;
// console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(2);
});
test('and that we have three documents after the second call', async () => {
await appendTrkpt(db, {});
await appendTrkpt(db, {});
const results = await db.find({ selector: {} });
const docs = results.docs;
console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(3);
});
});
describe('Checking that saveCurrent() is working as expected', () => {
test(', that we still have two documents after saving.', async () => {
await appendTrkpt(db, {});
await saveCurrent(db);
const results = await db.find({
selector: {},
});
const docs = results.docs;
// console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(2);
});
test("and that we don't have current tracks after saving.", async () => {
await appendTrkpt(db, {});
await saveCurrent(db);
const results = await db.find({
selector: {
type: 'gpx',
subtype: 'current',
},
});
const docs = results.docs;
// console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(0);
});
});
describe('Checking that deleteCurrent() is working as expected', () => {
test(', that we have no more documents after deleting.', async () => {
await appendTrkpt(db, { time: '' });
await deleteCurrent(db);
const results = await db.find({
selector: {},
});
const docs = results.docs;
// console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(0);
});
test(', that we have two documents from a saved track after deleting the current one.', async () => {
await appendTrkpt(db, { time: '' });
await saveCurrent(db);
await appendTrkpt(db, { time: '' });
await deleteCurrent(db);
const results = await db.find({
selector: {},
});
const docs = results.docs;
// console.log(`docs: ${JSON.stringify(docs)}`);
expect(docs.length).toBe(2);
});
});
-196
View File
@@ -1,196 +0,0 @@
import {
Sha256,
string_to_bytes,
bytes_to_base64,
} from '@openpgp/asmcrypto.js';
import GPX from '../lib/gpx-parser-builder';
export const pushGpx = async (db: any, payload: any) => {
const gpxString = JSON.stringify(payload.gpx);
const sha = new Sha256();
const result = sha.process(string_to_bytes(gpxString)).finish().result;
var _id;
if (result === null) {
console.log(`Can't hash`);
_id = crypto.randomUUID();
} else {
_id = bytes_to_base64(result);
console.log(`Digest: ${_id}`);
}
try {
await db.get(_id);
alert('This file has alerady been imported.');
return;
} catch {}
var gpx = JSON.parse(gpxString);
var points: any[] = [];
const prune = (object: any) => {
for (var key in object) {
if (key === 'trkpt') {
points.push(...object[key]);
object[key] = [];
} else {
if (typeof object[key] === 'object') {
prune(object[key]);
}
}
}
};
prune(gpx);
const doc = { ...payload, _id, type: 'gpx', subtype: 'other', gpx };
console.log(JSON.stringify(doc));
await db.put(doc);
for (var point in points) {
const docPoint = { type: 'trkpt', gpx: _id, trkpt: points[point] };
console.log(JSON.stringify(docPoint));
await db.post(docPoint);
console.log(JSON.stringify(docPoint));
}
};
const initialGpx = {
type: 'gpx',
subtype: 'current',
gpx: {
$: {
version: '1.1',
creator: 'dyomedea version 0.000001',
xmlns: 'http://www.topografix.com/GPX/1/1',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation':
'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd',
'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3',
'xmlns:wptx1': 'http://www.garmin.com/xmlschemas/WaypointExtension/v1',
'xmlns:gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
},
metadata: {
name: 'Tbd',
desc: 'Tbd',
time: '2022-08-27T21:35:01.000Z',
},
trk: [
{
name: 'Tbd',
trkseg: [
{
trkpt: [],
},
],
},
],
},
metadata: {
lastModified: '2022-08-27T21:35:02.000Z',
},
};
export const appendTrkpt = async (db: any, trkpt: any) => {
const currents = await db.find({
selector: {
type: 'gpx',
subtype: 'current',
},
fields: ['_id'],
});
console.log(`appendTrkpt - db.find() : ${JSON.stringify(currents)}`);
var currentId;
if (currents.docs.length > 0) {
currentId = currents.docs[0]._id;
} else {
initialGpx.metadata.lastModified = trkpt.time;
initialGpx.gpx.metadata.time = trkpt.time;
const response = await db.post(initialGpx);
currentId = response.id;
}
console.log(`currentId: ${currentId}`);
const docPoint = { type: 'trkpt', gpx: currentId, trkpt: trkpt };
await db.post(docPoint);
console.log(JSON.stringify(docPoint));
};
export const saveCurrent = async (db: any) => {
const currents = await db.find({
selector: {
type: 'gpx',
subtype: 'current',
},
});
console.log(`saveCurrent - db.find() : ${JSON.stringify(currents)}`);
if (currents.docs.length > 0) {
const doc = currents.docs[0];
doc.subtype = 'other';
await db.put(doc);
}
};
export const deleteGps = async (
db: any,
gps: { _id: string; _rev: string }
) => {
console.log(`Deleting document ${JSON.stringify(gps)}`);
await db.put({ _deleted: true, ...gps });
console.log(`done, id: ${gps}`);
const currentTrkpts = await db.find({
selector: {
type: 'trkpt',
gpx: gps._id,
},
fields: ['_id', '_rev'],
});
console.log(`deleteGps - db.find(trkpts) : ${JSON.stringify(currentTrkpts)}`);
const trkpts: { _id: string; _rev: string }[] = currentTrkpts.docs;
for (let j = 0; j < trkpts.length; j++) {
await db.put({ _deleted: true, ...trkpts[j] });
}
await await db.compact();
await db.viewCleanup();
};
export const deleteCurrent = async (db: any) => {
const currents = await db.find({
selector: {
type: 'gpx',
subtype: 'current',
},
fields: ['_id', '_rev'],
});
console.log(`deleteCurrent - db.find(gpx) : ${JSON.stringify(currents)}`);
const docs: { _id: string; _rev: string }[] = currents.docs;
for (let i = 0; i < docs.length; i++) {
console.log(`Deleting document ${JSON.stringify(docs[i])}`);
await deleteGps(db, docs[i]);
}
};
export const getGpx = async (db: any, gpxId: string) => {
console.log(`getGpx(db, "${gpxId}")`);
var gpxResult = await db.get(gpxId);
var gpx = gpxResult.gpx;
var trkptResults = await db.find({
selector: {
type: 'trkpt',
gpx: gpxId,
},
});
var trkpts = trkptResults.docs;
trkpts.sort((first: any, second: any) =>
first.trkpt.time.localeCompare(second.trkpt.time)
);
trkpts.map((trkptDoc: any) => {
gpx.trk[0].trkseg[0].trkpt.push(trkptDoc.trkpt);
return undefined;
});
return gpx;
};
export const getGpxAsXmlString = async (db: any, gpxId: string) => {
const gpx = new GPX(await getGpx(db, gpxId));
return gpx.toString({});
};
-107
View File
@@ -1,107 +0,0 @@
import _ from 'lodash';
import uri from '../lib/ids';
const dbDefinitionId = uri('dbdef', {});
const currentDbDefinition = {
_id: dbDefinitionId,
type: dbDefinitionId,
def: { version: '0.000001' },
};
export const initDb = async (db: any, setDbReady: any) => {
var previousDbDefinition = {
_id: dbDefinitionId,
type: dbDefinitionId,
def: { version: '0' },
};
try {
previousDbDefinition = await db.get(dbDefinitionId);
} catch (error: any) {
if (error.status !== 404) {
console.log(
`Unexpected error fetching db definition: ${JSON.stringify(error)}`
);
return;
}
}
if (previousDbDefinition.def.version < currentDbDefinition.def.version) {
previousDbDefinition.def = currentDbDefinition.def;
db.put(previousDbDefinition);
// TODO: support migrations
}
await await db.compact();
await db.viewCleanup();
// WARNING: defs must use the canonical form and be identical to what will be returned by db.getIndexes
const requiredIndexes: any = [
{
name: 'type-subtype',
def: {
fields: [{ type: 'asc' }, { subtype: 'asc' }],
},
},
{
name: 'type-trkpt-gpx-time',
def: {
fields: [{ type: 'asc' }, { gpx: 'asc' }, { 'trkpt.time': 'asc' }],
},
},
];
const existingIndexes = (await db.getIndexes()).indexes;
const pruneIndex = ({ name, def }: any) => ({ name, def });
const isSameIndex = (idx1: any, idx2: any) => {
return _.isEqual(pruneIndex(idx1), pruneIndex(idx2));
};
const findIndex = (targetIndexes: any, index: any) =>
targetIndexes.find((targetIndex: any) => {
return isSameIndex(targetIndex, index);
});
for (var index of existingIndexes) {
if (index.type === 'json') {
// Non system indexes
// console.log(`Checking existing index :${JSON.stringify(index)}`);
if (!findIndex(requiredIndexes, index)) {
// console.log(`db.deleteIndex(${JSON.stringify(index)})`);
await db.deleteIndex(index);
}
}
}
for (index of requiredIndexes) {
if (!findIndex(existingIndexes, index)) {
// console.log(`db.createIndex(${JSON.stringify(index)})`);
await db.createIndex({ name: index.name, ...index.def });
}
}
setDbReady(true);
/* const indexes = await db.getIndexes();
console.log(`indexes: ${JSON.stringify(indexes)}`);
const explain1 = await db.explain({
selector: {
type: 'trkpt',
gpx: 'xxxx',
},
// sort: ['trkpt.time'],
// use_index: 'type-trkpt-gpx-time',
});
console.log(`explain1: ${JSON.stringify(explain1)}`);
const explain2 = await db.explain({
selector: {
type: 'gpx',
},
// sort: ['trkpt.time'],
// use_index: 'type-trkpt-gpx-time',
});
console.log(`explain2: ${JSON.stringify(explain2)}`);
*/
};
-1
View File
@@ -1 +0,0 @@
declare module 'react-pouchdb';
File diff suppressed because it is too large. Load diff