mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	wip metrics via db
This commit is contained in:
		
							parent
							
								
									d4cc61f323
								
							
						
					
					
						commit
						ff41f5c425
					
				
							
								
								
									
										34
									
								
								packages/unleash-api/lib/client-metrics-service.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								packages/unleash-api/lib/client-metrics-service.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| 'use strict'; | ||||
| const POLL_INTERVAL = 10000; | ||||
| 
 | ||||
| module.exports = class UnleashClientMetrics { | ||||
|     constructor (metricsDb) { | ||||
|         this.metricsDb = metricsDb; | ||||
|         this.metrics = []; | ||||
|         this.highestIdSeen = 0; | ||||
|         metricsDb.getMetricsLastWeek().then(metrics => { | ||||
|             this.addMetrics(metrics); | ||||
|             this.startPoller(); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     addMetrics (metrics) { | ||||
|         metrics.forEach(m => this.metrics.push(m)); | ||||
|         this.highestIdSeen = this.metrics[this.metrics.length - 1].id; | ||||
|     } | ||||
| 
 | ||||
|     startPoller () { | ||||
|         setInterval(() => { | ||||
|             this.metricsDb.getNewMetrics(this.highestIdSeen) | ||||
|                 .then(metrics => this.addMetrics(metrics)); | ||||
|         }, POLL_INTERVAL).unref(); | ||||
|     } | ||||
| 
 | ||||
|     getMetrics () { | ||||
|         return this.metrics; | ||||
|     } | ||||
| 
 | ||||
|     insert (metrics) { | ||||
|         this.metricsDb.insert(metrics).then(() => console.log('new metrics inserted!')); | ||||
|     } | ||||
| }; | ||||
							
								
								
									
										41
									
								
								packages/unleash-api/lib/db/metrics.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								packages/unleash-api/lib/db/metrics.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,41 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| const METRICS_COLUMNS = ['id', 'created_at', 'metrics']; | ||||
| const TABLE = 'client_metrics'; | ||||
| 
 | ||||
| module.exports = function (db) { | ||||
|     // Insert new client metrics
 | ||||
|     function insert (metrics) { | ||||
|         return db(TABLE).insert({ metrics }); | ||||
|     } | ||||
| 
 | ||||
|     // Used at startup to load all metrics last week into memory!
 | ||||
|     function getMetricsLastWeek () { | ||||
|         return db | ||||
|             .select(METRICS_COLUMNS) | ||||
|             .from(TABLE) | ||||
|             .whereRaw('created_at > now() - interval \'7 day\'') | ||||
|             .orderBy('created_at', 'asc') | ||||
|             .map(mapRow); | ||||
|     } | ||||
| 
 | ||||
|     // Used to poll for new metrics
 | ||||
|     function getNewMetrics (lastKnownId) { | ||||
|         return db | ||||
|             .select(METRICS_COLUMNS) | ||||
|             .from(TABLE) | ||||
|             .where('id', '>', lastKnownId) | ||||
|             .orderBy('created_at', 'asc') | ||||
|             .map(mapRow); | ||||
|     } | ||||
| 
 | ||||
|     function mapRow (row) { | ||||
|         return { | ||||
|             id: row.id, | ||||
|             createdAt: row.created_at, | ||||
|             metrics: row.metrics, | ||||
|         }; | ||||
|     } | ||||
| 
 | ||||
|     return { insert, getMetricsLastWeek, getNewMetrics }; | ||||
| }; | ||||
| @ -2,23 +2,36 @@ | ||||
| 
 | ||||
| const logger = require('../logger'); | ||||
| const ClientMetrics = require('../client-metrics'); | ||||
| const ClientMetricsService = require('../client-metrics-service'); | ||||
| 
 | ||||
| module.exports = function (app) { | ||||
| module.exports = function (app, config) { | ||||
|     const metricsDb = config.metricsDb; | ||||
|     const metrics = new ClientMetrics(); | ||||
|     const service = new ClientMetricsService(metricsDb); | ||||
| 
 | ||||
|     app.get('/metrics', (req, res) => { | ||||
|         res.json(metrics.getState()); | ||||
|         res.json(service.getMetrics()); | ||||
| 
 | ||||
|         // Your stuff:
 | ||||
|         // res.json(metrics.getState());
 | ||||
|     }); | ||||
| 
 | ||||
|     app.post('/metrics', (req, res) => { | ||||
|          | ||||
|         // TODO: validate input and reply with http errorcode
 | ||||
|         try { | ||||
|             const data = JSON.parse(req.body); | ||||
|             metrics.addPayload(data); | ||||
|             // not required with header: Content-Type: application/json
 | ||||
|             // const data = JSON.parse(req.body);
 | ||||
|             // metrics.addPayload(data);
 | ||||
|             service.insert(req.body); | ||||
|         } catch (e) { | ||||
|             logger.error('Error recieving metrics', e); | ||||
|         } | ||||
| 
 | ||||
|         res.end(); | ||||
|     }); | ||||
| 
 | ||||
|     app.get('/metrics', (req, res) => { | ||||
|         res.json(metrics.getState()); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| @ -0,0 +1,3 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| module.exports = require('../scripts/migration-runner').create('008-create-metrics'); | ||||
| @ -0,0 +1,2 @@ | ||||
| --drop new metrics table | ||||
| DROP TABLE client_metrics; | ||||
| @ -0,0 +1,6 @@ | ||||
| --create new metrics table | ||||
| CREATE TABLE client_metrics ( | ||||
|   id serial primary key, | ||||
|   created_at timestamp default now(), | ||||
|   metrics json | ||||
| ); | ||||
| @ -18,6 +18,7 @@ function createApp (options) { | ||||
|     const eventStore = new EventStore(eventDb); | ||||
|     const featureDb = require('./lib/db/feature')(db, eventStore); | ||||
|     const strategyDb = require('./lib/db/strategy')(db, eventStore); | ||||
|     const metricsDb = require('./lib/db/metrics')(db); | ||||
| 
 | ||||
|     const config = { | ||||
|         baseUriPath: options.baseUriPath, | ||||
| @ -28,6 +29,7 @@ function createApp (options) { | ||||
|         eventStore, | ||||
|         featureDb, | ||||
|         strategyDb, | ||||
|         metricsDb, | ||||
|     }; | ||||
| 
 | ||||
|     const app = require('./app')(config); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user