Skip to content

Instantly share code, notes, and snippets.

@hasezoey

hasezoey/test.js Secret

Created June 27, 2022 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasezoey/6bc112e760b05e9a059550aa8e3dbbfa to your computer and use it in GitHub Desktop.
Save hasezoey/6bc112e760b05e9a059550aa8e3dbbfa to your computer and use it in GitHub Desktop.
'use strict';
const mongoose = require('.');
const { Schema } = mongoose;
mongoose.Schema.Types.DocumentArray.set('_id', false);
mongoose.Schema.Types.Subdocument.set('_id', false);
const geoPointSchema = new Schema({
type: {
type: String,
enum: ["Point"],
default: 'Point',
required: true,
},
coordinates: {
type: [Number],
required: true,
},
});
const journeySchema = new Schema({
status: {
type: String,
enum: ["available", "completed", "cancelled"],
},
start_point_text: String,
start_point_coordinates: {
type: geoPointSchema,
index: "2dsphere",
},
end_point_text: String,
end_point_coordinates: {
type: geoPointSchema,
index: "2dsphere",
},
start_time: Date,
end_time: Date,
});
const journeySummarySchema = new Schema({
id: Schema.Types.ObjectId,
private: Boolean,
groups: [{ id: Schema.Types.ObjectId }],
created: Date,
start_time: Date,
end_time: Date,
start_point_coordinates: geoPointSchema,
start_point_text: String,
end_point_coordinates: geoPointSchema,
end_point_text: String
});
const userSummarySchema = new Schema({
role: { type: String, enum: ["driver", "requested", "passenger", "not-part-of-journey"] },
});
const journeyEventSchema = new Schema({
time: Date,
journey: journeySummarySchema,
user: userSummarySchema,
action: String, /*[
"create",
"cancel",
"join"
],*/
reconstructed: { type: Boolean, default: false },
});
const journeyStateUserSummarySchema = new Schema({
...userSummarySchema.obj,
status: String //"confirmed" | "refunded",
});
const journeyStateJourneySummarySchema = new Schema(
{
...journeySummarySchema.obj,
request_status: {
type: String,
enum: ["available", "accepted"],
},
status: {
type: String,
enum: ["available", "completed", "cancelled"],
},
}
);
const journeyStateSchema = new Schema({
_id: Schema.Types.ObjectId,
journey: journeyStateJourneySummarySchema,
complete_journey: journeySchema,
user: journeyStateUserSummarySchema,
});
const searchEventSchema = new Schema({
time: Date,
start_point_coordinates: geoPointSchema,
end_point_coordinates: geoPointSchema,
});
const userEventLogSchema = new Schema({
schema_version: Number,
/*user: {
id: Schema.Types.ObjectId,
},*/
events: {
journeys: [journeyEventSchema],
searches: [searchEventSchema],
},
states: {
journeys: [journeyStateSchema],
},
});
const UserEventLog = mongoose.model('UserEventLog', userEventLogSchema);
run().catch(err => console.log(err));
async function run() {
const doc = new UserEventLog({ events: { journeys: [], searches: [] }, states: { journeys: [] } });
//const doc = { events: { journeys: [], searches: [] }, states: { journeys: [] } };
const start = Date.now();
for (let i = 0; i < 10000; ++i) {
doc.events.journeys.push({
journey: {
created: new Date(),
start_point_coordinates: { type: 'Point', coordinates: [0, 0] }
},
user: {
role: 'driver'
}
});
doc.events.searches.push({
start_point_coordinates: { coordinates: [0, 0] }
});
doc.states.journeys.push({
journey: {
created: new Date(),
start_point_coordinates: { type: 'Point', coordinates: [0, 0] },
request_status: 'available'
},
complete_journey: {
status: 'available',
start_point_coordinates: { type: 'Point', coordinates: [0, 0] }
},
user: {
role: 'driver',
status: 'confirmed'
}
});
}
const time = Date.now() - start;
console.log('Events', doc.events.searches[0], !!doc.events.searches[0].start_point_coordinates.$__);
console.log('Done', time);
console.log('[Timer] Memory usage:', process.memoryUsage().heapUsed / (1024 ** 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment