Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: c67cde4594791e05c8fd7336a52f0a548d6a450a (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
/**
 * Delta Publisher
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU 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 General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Publish delta message to a queue
 */

import { AmqpPublisher } from './AmqpPublisher';
import { DeltaResult } from '../bucket/delta';
import { EventDispatcher } from './event/EventDispatcher';
import {
    connect as amqpConnect,
    Options,
    Channel
} from 'amqplib';

const avro = require( 'avro-js' );

export interface AmqpConfig extends Options.Connect {
    /** The name of a queue or exchange to publish to */
    exchange: string;
}


export class DeltaPublisher implements AmqpPublisher
{
    /** The path to the avro schema */
    readonly SCHEMA_PATH = __dirname + '/avro/schema.avsc';

    /** A mapping of which delta type translated to which avro event */
    readonly DELTA_MAP: Record<string, string> = {
        data:     'STEP_SAVE',
        ratedata: 'RATE',
    };


    /**
     * Initialize publisher
     *
     * @param _conf    - amqp configuration
     * @param _emitter - event emitter instance
     * @param _ts_ctr  - a timestamp constructor
     */
    constructor(
        private readonly _conf:       AmqpConfig,
        private readonly _dispatcher: EventDispatcher,
        private readonly _ts_ctr    : () => UnixTimestamp,
    ) {}


    /**
     * Publish quote message to exchange post-rating
     *
     * @param delta - The delta to publish
     *
     * @return whether the message was published successfully
    */
    publish( delta: DeltaResult<any> ): Promise<null>
    {
        const exchange = this._conf.exchange;

        return new Promise<null>( ( resolve, reject ) =>
        {
            amqpConnect( this._conf )
            .then( conn =>
            {
                setTimeout( () => conn.close(), 10000 );
                return conn.createChannel();
            } )
            .then( ch =>
            {
                ch.assertExchange( exchange, 'fanout', { durable: true } );

                return this.sendMessage( ch, exchange, delta );
            } )
            .then( sentSuccessfully =>
            {
                console.log('sentSuccessfully', sentSuccessfully);
                if ( sentSuccessfully )
                {
                    this._dispatcher.dispatch(
                        'delta-publish',
                        "Published " + delta.type + " delta with ts '"
                            + delta.timestamp + "' to '" + exchange
                            + '" exchange',
                    );

                    resolve();
                }
                else
                {
                    this._dispatcher.dispatch(
                        'publish-err',
                        "Error publishing " + delta.type + " delta with ts '"
                            + delta.timestamp + "' to '" + exchange
                            + "' exchange",
                    );

                    reject();
                }
            } )
            .catch( e =>
            {
                this._dispatcher.dispatch(
                    'publish-err',
                    "Error publishing " + delta.type + " delta with ts '"
                        + delta.timestamp + '" to "' + exchange + "' exchange '"
                        + e,
                )

                reject();
            } );
        } );
    }


    /**
     * Send message to exchange
     *
     * @param channel  - AMQP channel
     * @param exchange - exchange name
     * @param delta    - The delta to publish
     *
     * @return whether publish was successful
     */
    sendMessage(
        channel:  Channel,
        exchange: string,
        delta:    DeltaResult<any>,
    ): boolean
    {
        const headers = {
            version: 1,
            created: Date.now(),
        };

        // Convert all delta datums to string for avro
        const delta_data = this.avroFormat( delta.data );
        const event_id   = this.DELTA_MAP[ delta.type ];

        const data = {
            event: {
                id:    event_id,
                ts:    this._ts_ctr(),
                actor: 'SERVER',
                step:  null,
            },
            document: {
                id:       123123, // Fix
            },
            session: {
                entity_name: 'Foobar', // Fix
                entity_id:   123123, // Fix
            },
            data: {
                Data: {
                    bucket: delta_data,
                },
            },
            delta: {
                Data: {
                    bucket: delta_data,
                },
            },
            program: {
                Program: {
                    id:      'quote_server',
                    version: 'dadaddwafdwa', // Fix
                },
            },
        };

        const avro_buffer = this.avroEncode( data );

        if ( !avro_buffer )
        {
            return false;
        }

        // we don't use a routing key; fanout exchange
        const routing_key = '';

        return channel.publish(
            exchange,
            routing_key,
            avro_buffer,
            { headers: headers },
        );
    }


    /**
     * Encode the data in an avro buffer
     *
     * @param data - the data to encode
     *
     * @return the avro buffer or null if there is an error
     */
    avroEncode( data: Record<string, any> ): Buffer | null
    {
        let buffer = null;

        try
        {
            const type   = avro.parse( this.SCHEMA_PATH );
                  buffer = type.toBuffer( data );
        }
        catch( e )
        {
            this._dispatcher.dispatch(
                'avro-parse-err',
                'Error encoding data to avro: ' + e,
            );
        }

        return buffer;
    }


    /**
     * Format the data for avro by add type specifications to the data
     *
     * @param data - the data to format
     *
     * @return the formatted data
     */
    avroFormat( data: any, top_level: boolean = true ): any
    {
        let data_formatted: any = {};

        switch( typeof( data ) )
        {
            case 'object': // Typescript treats arrays as objects
                if ( data == null )
                {
                    return null;
                }
                else if ( Array.isArray( data ) )
                {
                    let arr: any[] = [];

                    data.forEach( ( datum ) =>
                    {
                        arr.push( this.avroFormat( datum, false ) );
                    } );

                    data_formatted = ( top_level )
                        ? arr
                        : { 'array': arr };
                }
                else
                {
                    let datum_formatted: any = {};

                    Object.keys( data).forEach( ( key: string ) =>
                    {
                        const datum = this.avroFormat( data[ key ], false );

                        datum_formatted[ key ] = datum;

                    } );

                    data_formatted = ( top_level )
                        ? datum_formatted
                        : { "map": datum_formatted };
                }
                break;

            case 'boolean':
                return { 'boolean': data };

            case 'number':
                return { 'double': data };

            case 'string':
                return { 'string': data };

            case 'undefined':
                return null;
        }

        return data_formatted;
    }
}