Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 3f8128cb84a14f0f6bcf6f6952bb7263af963878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/**
 * Mongo DB DAO for program server
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { ServerDao, Callback } from "./ServerDao";
import { MongoCollection, MongoUpdate, MongoDb } from "mongodb";
import { PositiveInteger } from "../../numeric";
import { ServerSideQuote } from "../quote/ServerSideQuote";
import { QuoteId } from "../../document/Document";
import { WorksheetData } from "../rater/Rater";

const EventEmitter = require( 'events' ).EventEmitter;

type ErrorCallback = ( err: NullableError ) => void;

/**
 * Uses MongoDB as a data store
 */
export class MongoServerDao extends EventEmitter implements ServerDao
{
    /** Collection used to store quotes */
    readonly COLLECTION: string = 'quotes';

    /** Sequence (auto-increment) collection */
    readonly COLLECTION_SEQ: string = 'seq';

    /** Sequence key for quote ids */
    readonly SEQ_QUOTE_ID: string = 'quoteId';

    /** Sequence quoteId default */
    readonly SEQ_QUOTE_ID_DEFAULT: number = 200000;


    /** Whether the DAO is initialized and ready to be used */
    private _ready: boolean = false;

    /** Collection to save data to */
    private _collection?: MongoCollection | null;

    /** Collection to read sequences (auto-increments) from */
    private _seqCollection?: MongoCollection | null;


    /**
     * Initializes DAO
     *
     * @param {Mongo.Db} db mongo database connection
     */
    constructor(
        private readonly _db: MongoDb
    )
    {
        super();
    }


    /**
     * Initializes error events and attempts to connect to the database
     *
     * connectError event will be emitted on failure.
     *
     * @param Function callback function to call when connection is complete
     *                          (will not be called if connection fails)
     *
     * @return MongoServerDao self to allow for method chaining
     */
    init( callback: () => void ): this
    {
        var dao = this;

        // map db error event (on connection error) to our connectError event
        this._db.on( 'error', function( err: Error )
        {
            dao._ready      = false;
            dao._collection = null;

            dao.emit( 'connectError', err );
        });

        this.connect( callback );
        return this;
    }


    /**
     * Attempts to connect to the database
     *
     * connectError event will be emitted on failure.
     *
     * @param Function callback function to call when connection is complete
     *                          (will not be called if connection fails)
     *
     * @return MongoServerDao self to allow for method chaining
     */
    connect( callback: () => void ): this
    {
        var dao = this;

        // attempt to connect to the database
        this._db.open( function( err: any, db: any )
        {
            // if there was an error, don't bother with anything else
            if ( err )
            {
                // in some circumstances, it may just be telling us that we're
                // already connected (even though the connection may have been
                // broken)
                if ( err.errno !== undefined )
                {
                    dao.emit( 'connectError', err );
                    return;
                }
            }

            var ready_count = 0;
            var check_ready = function()
            {
                if ( ++ready_count < 2 )
                {
                    return;
                }

                // we're ready to roll!
                dao._ready = true;
                dao.emit( 'ready' );

                // connection was successful; call the callback
                if ( callback instanceof Function )
                {
                    callback.call( dao );
                }
            }

            // quotes collection
            db.collection(
                dao.COLLECTION,
                function(
                    _err:       any,
                    collection: MongoCollection,
                ) {
                    // for some reason this gets called more than once
                    if ( collection == null )
                    {
                        return;
                    }

                    // initialize indexes
                    collection.createIndex(
                        [ ['id', 1] ],
                        true,
                        function(
                            _err: NullableError,
                            _index: { [P: string]: any,
                        } )
                        {
                            // mark the DAO as ready to be used
                            dao._collection = collection;
                            check_ready();
                        }
                    );
                }
            );

            // seq collection
            db.collection(
                dao.COLLECTION_SEQ,
                function(
                    err:        Error,
                    collection: MongoCollection,
                ) {
                    if ( err )
                    {
                        dao.emit( 'seqError', err );
                        return;
                    }

                    if ( collection == null )
                    {
                        return;
                    }

                    dao._seqCollection = collection;

                    // has the sequence we'll be referencing been initialized?
                    collection.find(
                        { _id: dao.SEQ_QUOTE_ID },
                        { limit: <PositiveInteger>1 },
                        function( err: NullableError, cursor )
                        {
                            if ( err )
                            {
                                dao._initQuoteIdSeq( check_ready )
                                return;
                            }

                            cursor.toArray( function( _err: Error, data: any[] )
                            {
                                if ( data.length == 0 )
                                {
                                    dao._initQuoteIdSeq( check_ready );
                                    return;
                                }

                                check_ready();
                            });
                        }
                    );
                }
            );
        });

        return this;
    }


    private _initQuoteIdSeq( callback: () => void )
    {
        var dao = this;

        this._seqCollection!.insert(
            {
                _id: this.SEQ_QUOTE_ID,
                val: this.SEQ_QUOTE_ID_DEFAULT,
            },
            function( err: NullableError, _docs: any )
            {
                if ( err )
                {
                    dao.emit( 'seqError', err );
                    return;
                }

                dao.emit( 'seqInit', dao.SEQ_QUOTE_ID );
                callback.call( dao );
            }
        );
    }


    /**
     * Saves a quote to the database
     *
     * A full save will include all metadata.  This should not cause any
     * problems with race conditions for pending Data API calls on meta
     * fields because those results write to individual indexes and do not
     * rely on existing data.
     *
     * @param Quote    quote            the quote to save
     * @param Function success_callback function to call on success
     * @param Function failure_callback function to call if save fails
     * @param Object   save_data        quote data to save (optional)
     * @param Object   push_data        quote data to push (optional)
     */
    saveQuote(
        quote:            ServerSideQuote,
        success_callback: Callback,
        failure_callback: Callback,
        save_data?:       any,
        push_data?:       any,
    ): this
    {
        var dao                       = this;
        var meta: Record<string, any> = {};

        // if we're not ready, then we can't save the quote!
        if ( this._ready === false )
        {
            this.emit( 'saveQuoteError',
                { message: 'Database server not ready' },
                Error( 'Database not ready' ),
                quote
            );

            failure_callback.call( this, quote );
            return dao;
        }

        if ( save_data === undefined )
        {
            save_data = {
                data: quote.getBucket().getData(),
            };

            // full save will include all metadata
            meta = quote.getMetabucket().getData();
        }

        var id = quote.getId();

        // some data should always be saved because the quote will be created if
        // it does not yet exist
        save_data.id                 = id;
        save_data.pver               = quote.getProgramVersion();
        save_data.importDirty        = 1;
        save_data.published          = false;
        save_data.lastPremDate       = quote.getLastPremiumDate();
        save_data.initialRatedDate   = quote.getRatedDate();
        save_data.explicitLock       = quote.getExplicitLockReason();
        save_data.explicitLockStepId = quote.getExplicitLockStep();
        save_data.importedInd        = +quote.isImported();
        save_data.boundInd           = +quote.isBound();
        save_data.lastUpdate         = Math.round(
            ( new Date() ).getTime() / 1000
        );

        // meta will eventually take over for much of the above data
        meta.liza_timestamp_initial_rated = [ quote.getRatedDate() ];

        // save the stack so we can track this call via the oplog
        save_data._stack = ( new Error() ).stack;

        // avoid wiping out other metadata (since this may not be a full set)
        Object.keys( meta ).forEach(
            key => save_data[ 'meta.' + key ] = meta[ key ]
        );

        // do not push empty objects
        const document = ( !push_data || !Object.keys( push_data ).length )
            ? { '$set': save_data }
            : { '$set': save_data, '$push': push_data };

        // update the quote data if it already exists (same id), otherwise
        // insert it
        this._collection!.update( { id: id },
            document,

            // create record if it does not yet exist
            { upsert: true },

            // on complete
            function( err, _docs )
            {
                // if an error occurred, then we cannot continue
                if ( err )
                {
                    dao.emit( 'saveQuoteError', err, quote );

                    // let the caller handle the error
                    if ( failure_callback instanceof Function )
                    {
                        failure_callback.call( dao, quote );
                    }

                    return;
                }

                // successful
                if ( success_callback instanceof Function )
                {
                    success_callback.call( dao, quote );
                }
            }
        );

        return this;
    }


    /**
     * Merges quote data with the existing (rather than overwriting)
     *
     * @param {Quote}    quote     quote to save
     * @param {Object}   data      quote data
     * @param {Function} scallback successful callback
     * @param {Function} fcallback failure callback
     */
    mergeData(
        quote:     ServerSideQuote,
        data:      MongoUpdate,
        scallback: Callback,
        fcallback: Callback,
    ): this
    {
        // we do not want to alter the original data; use it as a prototype
        var update = data;

        // save the stack so we can track this call via the oplog
        var _self = this;
        this._collection!.update( { id: quote.getId() },
            { '$set': update },
            {},

            function( err, _docs )
            {
                if ( err )
                {
                    _self.emit( 'saveQuoteError', err, quote );

                    if ( typeof fcallback === 'function' )
                    {
                        fcallback( quote );
                    }

                    return;
                }

                if ( typeof scallback === 'function' )
                {
                    scallback( quote );
                }
            }
        );

        return this;
    }


    /**
     * Merges bucket data with the existing bucket (rather than overwriting the
     * entire bucket)
     *
     * @param {Quote}    quote     quote to save
     * @param {Object}   data      bucket data
     * @param {Function} scallback successful callback
     * @param {Function} fcallback failure callback
     *
     * @return {MongoServerDao} self
     */
    mergeBucket(
        quote:   ServerSideQuote,
        data:    MongoUpdate,
        success: Callback,
        failure: Callback,
    ): this
    {
        var update: MongoUpdate = {};

        for ( var field in data )
        {
            if ( !field )
            {
                continue;
            }

            update[ 'data.' + field ] = data[ field ];
        }

        return this.mergeData( quote, update, success, failure );
    }


    /**
     * Saves the quote state to the database
     *
     * The quote state includes the current step, the top visited step and the
     * explicit lock message.
     *
     * @param Quote    quote            the quote to save
     * @param Function success_callback function to call on success
     * @param Function failure_callback function to call if save fails
     *
     * @return MongoServerDao self
     */
    saveQuoteState(
        quote:            ServerSideQuote,
        success_callback: Callback,
        failure_callback: Callback,
    )
    {
        var update = {
            currentStepId:    quote.getCurrentStepId(),
            topVisitedStepId: quote.getTopVisitedStepId(),
            topSavedStepId:   quote.getTopSavedStepId(),
        };

        return this.mergeData(
            quote, update, success_callback, failure_callback
        );
    }


    saveQuoteClasses(
        quote:   ServerSideQuote,
        classes: any,
        success: Callback,
        failure: Callback,
    )
    {
        return this.mergeData(
            quote,
            { classData: classes },
            success,
            failure
        );
    }


    /**
     * Save document metadata (meta field on document)
     *
     * Only the provided indexes will be modified (that is---data will be
     * merged with what is already in the database).
     *
     * @param {Quote}    quote    destination quote
     * @param {Object}   new_meta bucket-formatted data to write
     * @param {Function} success  callback on success
     * @param {Function} failure  callback on error
     *
     * @return {undefined}
     */
    saveQuoteMeta(
        quote:    ServerSideQuote,
        new_meta: any,
        success:  Callback,
        failure:  Callback,
    ): void
    {
        const update: MongoUpdate = {};

        for ( var key in new_meta )
        {
            var meta = new_meta[ key ];

            for ( var i in meta )
            {
                update[ 'meta.' + key + '.' + i ] = new_meta[ key ][ i ];
            }
        }

        this.mergeData( quote, update, success, failure );
    }


    /**
     * Saves the quote lock state to the database
     *
     * @param Quote    quote            the quote to save
     * @param Function success_callback function to call on success
     * @param Function failure_callback function to call if save fails
     *
     * @return MongoServerDao self
     */
    saveQuoteLockState(
        quote:            ServerSideQuote,
        success_callback: Callback,
        failure_callback: Callback,
    ): this
    {
        // lock state is saved by default
        return this.saveQuote(
            quote,
            success_callback,
            failure_callback,
            {}
        );
    }


    /**
     * Pulls quote data from the database
     *
     * @param Integer          quote_id id of quote
     * @param Function( data ) callback function to call when data is available
     *
     * @return MongoServerDao self to allow for method chaining
     */
    pullQuote(
        quote_id: PositiveInteger,
        callback: ( data: Record<string, any> | null ) => void
    ): this
    {
        var dao = this;

        // XXX: TODO: Do not read whole of record into memory; filter out
        // revisions!
        this._collection!.find( { id: quote_id }, { limit: <PositiveInteger>1 },
            function( _err, cursor )
            {
                cursor.toArray( function( _err: NullableError, data: any[] )
                {
                    // was the quote found?
                    if ( data.length == 0 )
                    {
                        callback.call( dao, null );
                        return;
                    }

                    // return the quote data
                    callback.call( dao, data[ 0 ] );
                });
            }
        );

        return this;
    }


    getMinQuoteId( callback: ( min_id: number ) => void ): this
    {
        // just in case it's asynchronous later on
        callback.call( this, this.SEQ_QUOTE_ID_DEFAULT );

        return this;
    }


    getMaxQuoteId( callback: ( min_id: number ) => void ): void
    {
        var dao = this;

        this._seqCollection!.find(
            { _id: this.SEQ_QUOTE_ID },
            { limit: <PositiveInteger>1 },
            function( _err, cursor )
            {
                cursor.toArray( function( _err: NullableError, data: any[] )
                {
                    if ( data.length == 0 )
                    {
                        callback.call( dao, 0 );
                        return;
                    }

                    // return the max quote id
                    callback.call( dao, data[ 0 ].val );
                });
            }
        );
    }


    getNextQuoteId( callback: ( quote_id: number ) => void ): this
    {
        var dao = this;

        this._seqCollection!.findAndModify(
            { _id: this.SEQ_QUOTE_ID },
            [ [ 'val', 'descending' ] ],
            { $inc: { val: 1 } },
            { 'new': true },

            function( err, doc )
            {
                if ( err )
                {
                    dao.emit( 'seqError', err );

                    callback.call( dao, 0 );
                    return;
                }

                // return the new id
                callback.call( dao, doc.val );
            }
        );

        return this;
    }


    /**
     * Create a new revision with the provided quote data
     *
     * The revision will contain the whole the quote. If space is a concern, we
     * can (in the future) calculate a delta instead (Mike recommends the Git
     * model of storing the deltas in previous revisions and the whole of the
     * bucket in the most recently created revision).
     */
    createRevision(
        quote:    ServerSideQuote,
        callback: ErrorCallback,
    ): void
    {
        var _self = this,
            qid   = quote.getId(),
            data  = quote.getBucket().getData();

        this._collection!.update( { id: qid },
            { '$push': { revisions: { data: data } } },

            // create record if it does not yet exist
            { upsert: true },

            // on complete
            function( err )
            {
                if ( err )
                {
                    _self.emit( 'mkrevError', err );
                }

                callback( err );
                return;
            }
        );
    }


    getRevision(
        quote:    ServerSideQuote,
        revid:    PositiveInteger,
        callback: ErrorCallback,
    ): void
    {
        revid = <PositiveInteger>+revid;

        // XXX: TODO: Filter out all but the revision we want
        this._collection!.find(
            { id: quote.getId() },
            { limit: <PositiveInteger>1 },
            function( _err, cursor )
            {
                cursor.toArray( function( _err: NullableError, data: any[] )
                {
                    // was the quote found?
                    if ( ( data.length === 0 )
                        || ( data[ 0 ].revisions.length < ( revid + 1 ) )
                    )
                    {
                        callback( null );
                        return;
                    }

                    // return the quote data
                    callback( data[ 0 ].revisions[ revid ] );
                });
            }
        );
    }


    setWorksheets(
        qid:      QuoteId,
        data:     MongoUpdate,
        callback: NodeCallback<void>,
    ): void
    {
        this._collection!.update( { id: qid },
            { '$set': { worksheets: { data: data } } },

            // create record if it does not yet exist
            { upsert: true },

            // on complete
            function( err )
            {
                callback( err );
                return;
            }
        );
    }


    getWorksheet(
        qid:      QuoteId,
        supplier: string,
        index:    PositiveInteger,
        callback: ( data: WorksheetData | null ) => void,
    ): void
    {
        this._collection!.find(
            { id: qid },
            { limit: <PositiveInteger>1 },
            function( _err, cursor )
            {
                cursor.toArray( function( _err: NullableError, data: any[] )
                {
                    // was the quote found?
                    if ( ( data.length === 0 )
                        || ( !data[ 0 ].worksheets )
                        || ( !data[ 0 ].worksheets.data )
                        || ( !data[ 0 ].worksheets.data[ supplier ] )
                    )
                    {
                        callback( null );
                        return;
                    }

                    // return the quote data
                    callback( data[ 0 ].worksheets.data[ supplier ][ index ] );
                } );
            }
        );
    }
};