Mercurial > gemma
comparison pkg/controllers/diff.go @ 2570:c4242b9d59fe
Morphological differences: Added endpoint stub POST /api/diff
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 11 Mar 2019 13:15:28 +0100 |
parents | |
children | 7686c7c23506 |
comparison
equal
deleted
inserted
replaced
2569:2d18b8d5b9db | 2570:c4242b9d59fe |
---|---|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 | |
2 // without warranty, see README.md and license for details. | |
3 // | |
4 // SPDX-License-Identifier: AGPL-3.0-or-later | |
5 // License-Filename: LICENSES/AGPL-3.0.txt | |
6 // | |
7 // Copyright (C) 2018, 2019 by via donau | |
8 // – Österreichische Wasserstraßen-Gesellschaft mbH | |
9 // Software engineering by Intevation GmbH | |
10 // | |
11 // Author(s): | |
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | |
13 | |
14 package controllers | |
15 | |
16 import ( | |
17 "database/sql" | |
18 "fmt" | |
19 "log" | |
20 "net/http" | |
21 "time" | |
22 | |
23 "golang.org/x/sync/semaphore" | |
24 | |
25 "gemma.intevation.de/gemma/pkg/common" | |
26 "gemma.intevation.de/gemma/pkg/models" | |
27 "gemma.intevation.de/gemma/pkg/octree" | |
28 ) | |
29 | |
30 // Only allow three diffence calculation at once. | |
31 // TODO: Make this configurable? | |
32 var diffCalculationSemaphore = semaphore.NewWeighted(int64(3)) | |
33 | |
34 func diffCalculation( | |
35 input interface{}, | |
36 req *http.Request, | |
37 conn *sql.Conn, | |
38 ) (jr JSONResult, err error) { | |
39 | |
40 start := time.Now() | |
41 | |
42 ctx := req.Context() | |
43 | |
44 // DoS counter measure. | |
45 if err = diffCalculationSemaphore.Acquire(ctx, 1); err != nil { | |
46 return | |
47 } | |
48 defer diffCalculationSemaphore.Release(1) | |
49 | |
50 dci := input.(models.DiffCalculationInput) | |
51 | |
52 minuendTree, err := octree.FromCache( | |
53 ctx, conn, | |
54 dci.Bottleneck, dci.Minuend.Time) | |
55 | |
56 log.Printf("info: loading minuend octree took %s\n", time.Since(start)) | |
57 if err != nil { | |
58 return | |
59 } | |
60 | |
61 if minuendTree == nil { | |
62 err = JSONError{ | |
63 Code: http.StatusNotFound, | |
64 Message: fmt.Sprintf("Cannot find survey for %s/%s.", | |
65 dci.Bottleneck, | |
66 dci.Minuend.Format(common.DateFormat)), | |
67 } | |
68 return | |
69 } | |
70 | |
71 start = time.Now() | |
72 | |
73 subtrahendTree, err := octree.FromCache( | |
74 ctx, conn, | |
75 dci.Bottleneck, dci.Subtrahend.Time) | |
76 | |
77 log.Printf("info: loading subtrahend octree took %s\n", time.Since(start)) | |
78 if err != nil { | |
79 return | |
80 } | |
81 | |
82 if subtrahendTree == nil { | |
83 err = JSONError{ | |
84 Code: http.StatusNotFound, | |
85 Message: fmt.Sprintf("Cannot find survey for %s/%s.", | |
86 dci.Bottleneck, | |
87 dci.Subtrahend.Format(common.DateFormat)), | |
88 } | |
89 return | |
90 } | |
91 | |
92 // TODO: Implement me! | |
93 | |
94 return | |
95 } |