mapcomplete/Logic/ContributorCount.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-05-10 23:42:01 +02:00
/// Given a feature source, calculates a list of OSM-contributors who mapped the latest versions
import { Store, UIEventSource } from "./UIEventSource"
import { BBox } from "./BBox"
2023-03-28 05:13:48 +02:00
import GeoIndexedStore from "./FeatureSource/Actors/GeoIndexedStore"
2021-05-10 23:42:01 +02:00
export default class ContributorCount {
public readonly Contributors: UIEventSource<Map<string, number>> = new UIEventSource<
Map<string, number>
>(new Map<string, number>())
2023-03-28 05:13:48 +02:00
private readonly perLayer: ReadonlyMap<string, GeoIndexedStore>
2021-11-07 16:34:51 +01:00
private lastUpdate: Date = undefined
2021-05-10 23:42:01 +02:00
constructor(state: {
2023-03-28 05:13:48 +02:00
bounds: Store<BBox>
dataIsLoading: Store<boolean>
perLayer: ReadonlyMap<string, GeoIndexedStore>
}) {
2023-03-28 05:13:48 +02:00
this.perLayer = state.perLayer
const self = this
2023-03-28 05:13:48 +02:00
state.bounds.mapD(
(bbox) => {
self.update(bbox)
},
[state.dataIsLoading]
)
2021-11-07 16:34:51 +01:00
}
private update(bbox: BBox) {
const now = new Date()
if (
this.lastUpdate !== undefined &&
now.getTime() - this.lastUpdate.getTime() < 1000 * 60
) {
return
}
this.lastUpdate = now
2023-03-28 05:13:48 +02:00
const featuresList = [].concat(
Array.from(this.perLayer.values()).map((fs) => fs.GetFeaturesWithin(bbox))
)
const hist = new Map<string, number>()
for (const list of featuresList) {
for (const feature of list) {
const contributor = feature.properties["_last_edit:contributor"]
2021-05-10 23:42:01 +02:00
const count = hist.get(contributor) ?? 0
hist.set(contributor, count + 1)
}
}
this.Contributors.setData(hist)
2021-05-10 23:42:01 +02:00
}
}