| 
									
										
										
										
											2021-05-10 23:42:01 +02:00
										 |  |  | /// Given a feature source, calculates a list of OSM-contributors who mapped the latest versions
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { Store, UIEventSource } from "./UIEventSource" | 
					
						
							|  |  |  | import FeaturePipeline from "./FeatureSource/FeaturePipeline" | 
					
						
							|  |  |  | import Loc from "../Models/Loc" | 
					
						
							|  |  |  | import { BBox } from "./BBox" | 
					
						
							| 
									
										
										
										
											2021-05-10 23:42:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class ContributorCount { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public readonly Contributors: UIEventSource<Map<string, number>> = new UIEventSource< | 
					
						
							|  |  |  |         Map<string, number> | 
					
						
							|  |  |  |     >(new Map<string, number>()) | 
					
						
							|  |  |  |     private readonly state: { | 
					
						
							|  |  |  |         featurePipeline: FeaturePipeline | 
					
						
							|  |  |  |         currentBounds: Store<BBox> | 
					
						
							|  |  |  |         locationControl: Store<Loc> | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     private lastUpdate: Date = undefined | 
					
						
							| 
									
										
										
										
											2021-05-10 23:42:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor(state: { | 
					
						
							|  |  |  |         featurePipeline: FeaturePipeline | 
					
						
							|  |  |  |         currentBounds: Store<BBox> | 
					
						
							|  |  |  |         locationControl: Store<Loc> | 
					
						
							|  |  |  |     }) { | 
					
						
							|  |  |  |         this.state = state | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  |         state.currentBounds.map((bbox) => { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |             self.update(bbox) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         state.featurePipeline.runningQuery.addCallbackAndRun((_) => | 
					
						
							|  |  |  |             self.update(state.currentBounds.data) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private update(bbox: BBox) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (bbox === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const now = new Date() | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             this.lastUpdate !== undefined && | 
					
						
							|  |  |  |             now.getTime() - this.lastUpdate.getTime() < 1000 * 60 | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.lastUpdate = now | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         const featuresList = this.state.featurePipeline.GetAllFeaturesWithin(bbox) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const hist = new Map<string, number>() | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         for (const list of featuresList) { | 
					
						
							|  |  |  |             for (const feature of list) { | 
					
						
							|  |  |  |                 const contributor = feature.properties["_last_edit:contributor"] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const count = hist.get(contributor) ?? 0 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:42:01 +02:00
										 |  |  |                 hist.set(contributor, count + 1) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         this.Contributors.setData(hist) | 
					
						
							| 
									
										
										
										
											2021-05-10 23:42:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |