@@ -2,15 +2,21 @@ import { Request, Response } from "express";
2
2
3
3
import Core from "../Core.js" ;
4
4
import { ERROR_GENERIC } from "../util/Errors.js" ;
5
+ import axios from "axios" ;
6
+ import { toOverpassPolygon } from "../util/Coordinates.js" ;
5
7
6
8
class AdminController {
7
9
private core : Core ;
10
+ private progress ;
8
11
9
12
constructor ( core : Core ) {
10
13
this . core = core ;
14
+ this . progress = {
15
+ buildings : { done : 0 , total : 0 } ,
16
+ adresses : { done : 0 , total : 0 } ,
17
+ } ;
11
18
}
12
19
13
- // CRON
14
20
public getCronJobs ( req : Request , res : Response ) {
15
21
const jobs = this . core . getCron ( ) . getAll ( ) ;
16
22
@@ -27,6 +33,68 @@ class AdminController {
27
33
} )
28
34
) ;
29
35
}
36
+
37
+ public getProgress ( req : Request , res : Response ) {
38
+ res . send ( this . progress ) ;
39
+ }
40
+
41
+ public async getClaimBuildingCounts ( req : Request , res : Response ) {
42
+ if ( this . progress . buildings > 0 ) {
43
+ return ERROR_GENERIC ( res , 409 , "Recalculations are already ongoing." ) ;
44
+ }
45
+
46
+ const claims = await this . core . getPrisma ( ) . claim . findMany ( {
47
+ where : {
48
+ center : { not : null } ,
49
+ buildings :
50
+ req . query . skipExisting === "true"
51
+ ? 1
52
+ : {
53
+ gte : req . query . take ? parseInt ( req . query . gte as string ) : 0 ,
54
+ } ,
55
+ } ,
56
+ take : req . query . take && parseInt ( req . query . take as string ) ,
57
+ skip : req . query . skip ? parseInt ( req . query . skip as string ) : 0 ,
58
+ select : { buildings : true , id : true , area : true } ,
59
+ } ) ;
60
+
61
+ res . send ( { progress : 0 , count : claims . length } ) ;
62
+ this . progress . buildings . total = claims . length ;
63
+
64
+ for ( const [ i , claim ] of claims . entries ( ) ) {
65
+ const polygon = toOverpassPolygon ( claim . area ) ;
66
+
67
+ const overpassQuery = `[out:json][timeout:25];
68
+ (
69
+ node["building"]["building"!~"grandstand"]["building"!~"roof"]["building"!~"garage"]["building"!~"hut"]["building"!~"shed"](poly: "${ polygon } ");
70
+ way["building"]["building"!~"grandstand"]["building"!~"roof"]["building"!~"garage"]["building"!~"hut"]["building"!~"shed"](poly: "${ polygon } ");
71
+ relation["building"]["building"!~"grandstand"]["building"!~"roof"]["building"!~"garage"]["building"!~"hut"]["building"!~"shed"](poly: "${ polygon } ");
72
+ );
73
+ out count;` ;
74
+
75
+ const { data } = await axios . get (
76
+ `https://overpass.kumi.systems/api/interpreter?data=${ overpassQuery . replace (
77
+ "\n" ,
78
+ ""
79
+ ) } `
80
+ ) ;
81
+ this . core
82
+ . getLogger ( )
83
+ . debug (
84
+ "Getting buildings for claim " +
85
+ claim . id +
86
+ ` (${ i + 1 } /${ claims . length } )`
87
+ ) ;
88
+
89
+ const updatedClaim = await this . core . getPrisma ( ) . claim . update ( {
90
+ where : { id : claim . id } ,
91
+ data : { buildings : parseInt ( data ?. elements [ 0 ] ?. tags ?. total ) || 0 } ,
92
+ } ) ;
93
+
94
+ this . progress . buildings . done = i + 1 ;
95
+ }
96
+ this . progress . buildings = { done : 0 , total : 0 } ;
97
+ }
30
98
}
31
99
32
100
export default AdminController ;
0 commit comments