annotate pkg/controllers/importqueue.go @ 5736:55892008ec96 default tip

Fixed a bunch of corner cases in WG import.
author Sascha Wilde <wilde@sha-bang.de>
date Wed, 29 May 2024 19:02:42 +0200
parents 5a0fbdda6e2d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
17 "context"
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
19 "encoding/csv"
1392
0e1d89241cda Imports: An Import (e.g. a sounding result import) can now write a 'summary' of a successful import. This is done if the import switches to to state 'pending'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1359
diff changeset
20 "encoding/json"
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
21 "fmt"
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "net/http"
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "strconv"
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
24 "strings"
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
25 "sync"
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
26 "time"
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
1392
0e1d89241cda Imports: An Import (e.g. a sounding result import) can now write a 'summary' of a successful import. This is done if the import switches to to state 'pending'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1359
diff changeset
28 "github.com/gorilla/mux"
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
29 "github.com/jackc/pgx/pgtype"
1392
0e1d89241cda Imports: An Import (e.g. a sounding result import) can now write a 'summary' of a successful import. This is done if the import switches to to state 'pending'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1359
diff changeset
30
1194
7db850de0952 Added a signer who makes the final decison on an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1193
diff changeset
31 "gemma.intevation.de/gemma/pkg/auth"
4078
80bdcd137a1d Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3776
diff changeset
32 "gemma.intevation.de/gemma/pkg/common"
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
33 "gemma.intevation.de/gemma/pkg/imports"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
34 "gemma.intevation.de/gemma/pkg/log"
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 "gemma.intevation.de/gemma/pkg/models"
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
36
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
37 mw "gemma.intevation.de/gemma/pkg/middleware"
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 )
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 const (
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
41 selectImportsCountSQL = `
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
42 SELECT count(*)
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
43 FROM import.imports
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
44 WHERE
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
45 `
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
46 selectImportsSQL = `
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 SELECT
2627
3a242e6aa56d Import log: Add filter for log entries with warnings only: GET /api/imports?warnings=true
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2179
diff changeset
48 imports.id AS id,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 state::varchar,
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 enqueued,
4748
47922c1a088d Added a 'changed' column to the import.imports table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
51 changed,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 kind,
1194
7db850de0952 Added a signer who makes the final decison on an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1193
diff changeset
53 username,
1392
0e1d89241cda Imports: An Import (e.g. a sounding result import) can now write a 'summary' of a successful import. This is done if the import switches to to state 'pending'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1359
diff changeset
54 signer,
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
55 summary IS NOT NULL AS has_summary,
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
56 EXISTS(SELECT 1 FROM import.import_logs
4196
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
57 WHERE kind = 'error'::log_type and import_id = imports.id) AS has_errors,
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
58 EXISTS(SELECT 1 FROM import.import_logs
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
59 WHERE kind = 'warn'::log_type and import_id = imports.id) AS has_warnings
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
60 FROM import.imports
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
61 WHERE
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
62 `
5563
3be1d79ad3a3 Log export: Limit log lines loading to 'sr' imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5562
diff changeset
63 // XXX: Consider not only supporting 'sr' for log message parsing.
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
64 selectExportSQL = `
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
65 SELECT
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
66 imports.id AS id,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
67 state::varchar,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
68 enqueued,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
69 changed,
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
70 imports.kind,
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
71 username,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
72 (SELECT country FROM users.list_users lu
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
73 WHERE lu.username = import.imports.username) AS country,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
74 signer,
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
75 EXISTS(SELECT 1 FROM import.import_logs
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
76 WHERE kind = 'warn'::log_type and import_id = id) AS has_warnings,
5717
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
77 EXISTS(SELECT 1 FROM import.import_logs
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
78 WHERE kind = 'error'::log_type and import_id = id) AS has_errors,
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
79 data,
5564
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
80 CASE WHEN kind = ANY($1) THEN ARRAY(SELECT msg FROM import.import_logs WHERE import_id = id)
5563
3be1d79ad3a3 Log export: Limit log lines loading to 'sr' imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5562
diff changeset
81 ELSE NULL
3be1d79ad3a3 Log export: Limit log lines loading to 'sr' imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5562
diff changeset
82 END AS msgs
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
83 FROM import.imports
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
84 WHERE
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
85 `
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
86 selectEnqueuedSQL = `
2672
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
87 SELECT enqueued FROM import.imports
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
88 WHERE
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
89 `
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
90 selectImportSummarySQL = `
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
91 SELECT summary, enqueued FROM import.imports WHERE id = $1`
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
92
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
93 selectHasNoRunningImportSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
94 SELECT true FROM import.imports
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
95 WHERE id = $1 AND state <> 'running'::import_state`
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
96
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
97 selectImportLogsSQL = `
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
98 SELECT
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
99 time,
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
100 kind::varchar,
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
101 msg
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
102 FROM import.import_logs
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
103 WHERE import_id = $1
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
104 ORDER BY time`
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
105
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
106 deleteLogsSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
107 DELETE FROM import.import_logs WHERE import_id = $1`
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
108
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
109 deleteImportSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
110 DELETE FROM import.imports WHERE id = $1`
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 )
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
113 type filledStmt struct {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
114 stmt strings.Builder
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
115 args []any
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
116 }
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
117
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
118 func buildFilters(projection string, req *http.Request, args ...any) (
5564
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
119 *filledStmt,
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
120 *filledStmt,
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
121 *filledStmt,
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
122 error,
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
123 ) {
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
124
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
125 var l, a, b filterAnd
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
126
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
127 var noBefore, noAfter bool
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
128
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
129 cond := func(format string, args ...any) {
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
130 term := &filterTerm{format: format, args: args}
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
131 l = append(l, term)
4195
ad13c581de7c Fixed building filters for listing import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4187
diff changeset
132 a = append(a, term)
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
133 b = append(b, term)
2677
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
134 }
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
135
5108
89639e02cff0 Do not show internal review jobs to users
Tom Gottfried <tom@intevation.de>
parents: 5093
diff changeset
136 // Always filter review jobs. They are only for internal use.
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
137 cond(` NOT imports.kind LIKE '%%` + imports.ReviewJobSuffix + `'`)
5108
89639e02cff0 Do not show internal review jobs to users
Tom Gottfried <tom@intevation.de>
parents: 5093
diff changeset
138
2677
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
139 if query := req.FormValue("query"); query != "" {
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
140 query = "%" + query + "%"
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
141 cond(` (imports.kind ILIKE $%d OR username ILIKE $%d OR signer ILIKE $%d OR `+
2677
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
142 `id IN (SELECT import_id FROM import.import_logs WHERE msg ILIKE $%d)) `,
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
143 query, query, query, query)
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
144 }
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
145
3776
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
146 if cc := req.FormValue("cc"); cc != "" {
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
147 codes := sliceToTextArray(splitUpper(cc))
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
148 cond(" username IN "+
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
149 "(SELECT username FROM internal.user_profiles "+
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
150 "WHERE country = ANY($%d)) ",
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
151 codes)
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
152 }
6521c962a7b6 Add 'cc' parameter to search imports for a comma separated list of country codes for the the importing person.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3217
diff changeset
153
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
154 if st := req.FormValue("states"); st != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
155 states := toTextArray(st, imports.ImportStateNames)
2677
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
156 cond(" state = ANY($%d) ", states)
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
157 }
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
158
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
159 if ks := req.FormValue("kinds"); ks != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
160 kinds := toTextArray(ks, imports.ImportKindNames())
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
161 cond(" imports.kind = ANY($%d) ", kinds)
1655
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
162 }
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
163
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
164 if idss := req.FormValue("ids"); idss != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
165 ids := toInt8Array(idss)
2677
fe93c48f76c9 Add /api/imports?query=TXT to search TXT in kind, username, signer or logs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2673
diff changeset
166 cond(" id = ANY($%d) ", ids)
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
167 }
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
168
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
169 if from := req.FormValue("from"); from != "" {
4078
80bdcd137a1d Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3776
diff changeset
170 fromTime, err := common.ParseTime(from)
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
171 if err != nil {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
172 return nil, nil, nil, err
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
173 }
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
174 l = append(l, buildFilterTerm("enqueued >= $%d", fromTime))
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
175 b = append(b, buildFilterTerm("enqueued < $%d", fromTime))
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
176 } else {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
177 noBefore = true
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
178 }
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
179
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
180 if to := req.FormValue("to"); to != "" {
4078
80bdcd137a1d Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3776
diff changeset
181 toTime, err := common.ParseTime(to)
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
182 if err != nil {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
183 return nil, nil, nil, err
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
184 }
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
185 l = append(l, buildFilterTerm("enqueued <= $%d", toTime))
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
186 a = append(a, buildFilterTerm("enqueued > $%d", toTime))
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
187 } else {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
188 noAfter = true
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
189 }
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
190
2665
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
191 switch warn := strings.ToLower(req.FormValue("warnings")); warn {
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
192 case "1", "t", "true":
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
193 cond(` EXISTS(SELECT 1 FROM import.import_logs
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
194 WHERE kind = 'warn'::log_type and import_id = imports.id)`)
2665
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
195 }
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
196
4196
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
197 switch errors := strings.ToLower(req.FormValue("errors")); errors {
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
198 case "1", "t", "true":
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
199 cond(` EXISTS(SELECT 1 FROM import.import_logs
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
200 WHERE kind = 'error'::log_type and import_id = imports.id)`)
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
201 }
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
202
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
203 fl := &filledStmt{}
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
204 fa := &filledStmt{}
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
205 fb := &filledStmt{}
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
206
5564
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
207 fl.args = append(fl.args, args...)
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
208 fa.args = append(fa.args, args...)
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
209 fb.args = append(fb.args, args...)
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
210
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
211 fa.stmt.WriteString(selectEnqueuedSQL)
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
212 fb.stmt.WriteString(selectEnqueuedSQL)
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
213
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
214 var counting bool
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
215
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
216 if projection != "" {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
217 fl.stmt.WriteString(projection)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
218 } else {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
219 switch count := strings.ToLower(req.FormValue("count")); count {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
220 case "1", "t", "true":
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
221 counting = true
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
222 fl.stmt.WriteString(selectImportsCountSQL)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
223 default:
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
224 fl.stmt.WriteString(selectImportsSQL)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
225 }
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
226 }
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
227
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
228 if len(l) == 0 {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
229 fl.stmt.WriteString(" TRUE ")
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
230 } else {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
231 l.serialize(&fl.stmt, &fl.args)
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
232 }
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
233
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
234 if len(b) == 0 {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
235 fb.stmt.WriteString(" TRUE ")
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
236 } else {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
237 b.serialize(&fb.stmt, &fb.args)
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
238 }
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
239
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
240 if len(a) == 0 {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
241 fa.stmt.WriteString(" TRUE ")
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
242 } else {
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
243 a.serialize(&fa.stmt, &fa.args)
1655
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
244 }
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
245
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
246 if !counting {
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
247 fl.stmt.WriteString(" ORDER BY enqueued DESC ")
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
248 fa.stmt.WriteString(" ORDER BY enqueued LIMIT 1")
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
249 fb.stmt.WriteString(" ORDER BY enqueued DESC LIMIT 1")
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
250 }
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
251
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
252 if noBefore {
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
253 fb = nil
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
254 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
255 if noAfter {
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
256 fa = nil
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
257 }
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
258
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
259 return fl, fb, fa, nil
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
260 }
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
261
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
262 func neighbored(ctx context.Context, conn *sql.Conn, fb *filledStmt) *models.ImportTime {
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
263
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
264 var when time.Time
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
265 err := conn.QueryRowContext(ctx, fb.stmt.String(), fb.args...).Scan(&when)
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
266 switch {
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
267 case err == sql.ErrNoRows:
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
268 return nil
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
269 case err != nil:
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
270 log.Warnf("%v\n", err)
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
271 return nil
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
272 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
273 return &models.ImportTime{Time: when.UTC()}
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
274 }
1189
3d50f558870c REST GET call to /imports now has the ability to be filtered by kinds or states.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1037
diff changeset
275
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
276 func exportImports(rw http.ResponseWriter, req *http.Request) {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
277
5564
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
278 type LogLoader interface{ LoadingLogs() bool }
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
279
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
280 var lls []string
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
281
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
282 imports.All(func(k imports.JobKind, jc imports.JobCreator) {
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
283 if ll, ok := jc.(LogLoader); ok && ll.LoadingLogs() {
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
284 lls = append(lls, string(k))
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
285 }
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
286 })
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
287
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
288 var loaders pgtype.TextArray
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
289 if err := loaders.Set(lls); err != nil {
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
290 http.Error(
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
291 rw, fmt.Sprintf("error: %v", err),
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
292 http.StatusInternalServerError)
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
293 return
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
294 }
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
295
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
296 list, _, _, err := buildFilters(selectExportSQL, req, &loaders)
aaa9e658cabd Log export: Added marker interface to JobCreators that log messages should be loaded at export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5563
diff changeset
297
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
298 if err != nil {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
299 http.Error(rw, "error: "+err.Error(), http.StatusBadRequest)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
300 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
301 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
302
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
303 rw.Header().Add("Content-Type", "text/csv")
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
304 out := csv.NewWriter(rw)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
305
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
306 record := []string{
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
307 "#id",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
308 "#kind",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
309 "#enqueued",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
310 "#changed",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
311 "#user",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
312 "#country",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
313 "#signer",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
314 "#state",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
315 "#warnings",
5717
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
316 "#errors",
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
317 "#source",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
318 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
319
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
320 if err := out.Write(record); err != nil {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
321 // Too late for HTTP status message.
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
322 log.Errorf("%v\n", err)
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
323 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
324 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
325
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
326 start := time.Now()
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
327
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
328 conn := mw.GetDBConn(req)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
329 ctx := req.Context()
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
330 var rows *sql.Rows
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
331 if rows, err = conn.QueryContext(ctx, list.stmt.String(), list.args...); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
332 log.Errorf("%v\n", err)
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
333 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
334 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
335 defer rows.Close()
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
336
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
337 // Extract some meta infos from the import.
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
338 type Description interface {
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
339 Description([]string) (string, error)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
340 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
341
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
342 type dataset struct {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
343 id int64
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
344 state string
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
345 enqueued time.Time
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
346 changed time.Time
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
347 kind string
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
348 user string
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
349 country string
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
350 signer sql.NullString
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
351 warnings bool
5717
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
352 errors bool
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
353 data string
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
354 msgs pgtype.TextArray
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
355 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
356
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
357 // Log unsupported description interfaces per kind only once.
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
358 unsupported := make(map[string]bool)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
359
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
360 store := func(ds *dataset) error {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
361
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
362 var description string
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
363
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
364 // Do some introspection on the job to be more verbose.
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
365 if jc := imports.FindJobCreator(imports.JobKind(ds.kind)); jc != nil {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
366 job := jc.Create()
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
367 if err := common.FromJSONString(ds.data, job); err != nil {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
368 log.Errorf("%v\n", err)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
369 } else if desc, ok := job.(Description); ok {
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
370 var ms []string
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
371 if ds.msgs.Status == pgtype.Present {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
372 if err := ds.msgs.AssignTo(&ms); err != nil {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
373 return err
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
374 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
375 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
376 if description, err = desc.Description(ms); err != nil {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
377 return err
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
378 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
379 description = strings.Replace(description, ",", "|", -1)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
380 } else {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
381 if !unsupported[ds.kind] {
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
382 unsupported[ds.kind] = true
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
383 log.Debugf("%s: description not supported\n", ds.kind)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
384 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
385 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
386 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
387
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
388 var signer string
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
389 if ds.signer.Valid {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
390 signer = ds.signer.String
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
391 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
392
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
393 record[0] = strconv.FormatInt(ds.id, 10)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
394 record[1] = ds.kind
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
395 record[2] = ds.enqueued.UTC().Format(common.TimeFormat)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
396 record[3] = ds.changed.UTC().Format(common.TimeFormat)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
397 record[4] = ds.user
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
398 record[5] = ds.country
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
399 record[6] = signer
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
400 record[7] = ds.state
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
401 record[8] = strconv.FormatBool(ds.warnings)
5717
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
402 record[9] = strconv.FormatBool(ds.errors)
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
403 record[10] = description
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
404
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
405 return out.Write(record)
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
406 }
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
407
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
408 for rows.Next() {
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
409 var curr dataset
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
410
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
411 if err := rows.Scan(
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
412 &curr.id,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
413 &curr.state,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
414 &curr.enqueued,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
415 &curr.changed,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
416 &curr.kind,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
417 &curr.user,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
418 &curr.country,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
419 &curr.signer,
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
420 &curr.warnings,
5717
1c3bdd114ca1 Add errors column to CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
421 &curr.errors,
5560
f2204f91d286 Join the log lines of imports to the log exports to recover data from them.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
422 &curr.data,
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
423 &curr.msgs,
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
424 ); err != nil {
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
425 log.Errorf("%v\n", err)
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
426 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
427 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
428
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
429 if err := store(&curr); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
430 log.Errorf("%v\n", err)
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
431 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
432 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
433 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
434
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
435 if err := rows.Err(); err != nil {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
436 log.Errorf("%v\n", err)
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
437 return
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
438 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
439
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
440 out.Flush()
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
441 if err := out.Error(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
442 log.Errorf("%v\n", err)
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
443 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
444
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
445 log.Debugf("Export took: %v\n", time.Since(start))
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
446 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
447
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
448 func listImports(req *http.Request) (jr mw.JSONResult, err error) {
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
449
3194
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
450 var list, before, after *filledStmt
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
451
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
452 if list, before, after, err = buildFilters("", req); err != nil {
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
453 return
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
454 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
455
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
456 ctx := req.Context()
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
457
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
458 conn := mw.JSONConn(req)
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4196
diff changeset
459
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
460 // Fast path for counting
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
461
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
462 switch count := strings.ToLower(req.FormValue("count")); count {
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
463 case "1", "t", "true":
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
464 var count int64
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
465 err = conn.QueryRowContext(ctx, list.stmt.String(), list.args...).Scan(&count)
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
466 switch {
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
467 case err == sql.ErrNoRows:
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
468 count, err = 0, nil
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
469 case err != nil:
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
470 return
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
471 }
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
472 jr = mw.JSONResult{Result: count}
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
473 return
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
474 }
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
475
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
476 // Generate the list
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
477
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
478 var rows *sql.Rows
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
479 if rows, err = conn.QueryContext(ctx, list.stmt.String(), list.args...); err != nil {
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
480 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
481 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
482 defer rows.Close()
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
483
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
484 imports := make([]*models.Import, 0, 20)
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
485
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
486 var signer sql.NullString
1195
486d66a9565c Be aware that the signer is null till the final decision is made about an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1194
diff changeset
487
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
488 for rows.Next() {
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
489 var it models.Import
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
490 var enqueued time.Time
4748
47922c1a088d Added a 'changed' column to the import.imports table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
491 var changed time.Time
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
492 if err = rows.Scan(
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
493 &it.ID,
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
494 &it.State,
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
495 &enqueued,
4748
47922c1a088d Added a 'changed' column to the import.imports table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
496 &changed,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
497 &it.Kind,
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
498 &it.User,
1195
486d66a9565c Be aware that the signer is null till the final decision is made about an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1194
diff changeset
499 &signer,
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
500 &it.Summary,
4196
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
501 &it.Errors,
2179
20d9b71f4125 Import queue listing: Add another column 'warnings' (bool) to listing output which indicates if an import has warnings or not.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1995
diff changeset
502 &it.Warnings,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
503 ); err != nil {
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
504 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
505 }
1195
486d66a9565c Be aware that the signer is null till the final decision is made about an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1194
diff changeset
506 if signer.Valid {
486d66a9565c Be aware that the signer is null till the final decision is made about an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1194
diff changeset
507 it.Signer = signer.String
486d66a9565c Be aware that the signer is null till the final decision is made about an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1194
diff changeset
508 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
509 it.Enqueued = models.ImportTime{Time: enqueued.UTC()}
4748
47922c1a088d Added a 'changed' column to the import.imports table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
510 it.Changed = models.ImportTime{Time: changed.UTC()}
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
511 imports = append(imports, &it)
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
512 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
513
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
514 if err = rows.Err(); err != nil {
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
515 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
516 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
517
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
518 var prev, next *models.ImportTime
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
519
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
520 if before != nil {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
521 prev = neighbored(ctx, conn, before)
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
522 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
523
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
524 if after != nil {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
525 next = neighbored(ctx, conn, after)
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
526 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
527
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
528 jr = mw.JSONResult{
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
529 Result: struct {
2663
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
530 Prev *models.ImportTime `json:"prev,omitempty"`
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
531 Next *models.ImportTime `json:"next,omitempty"`
386ff766dfcd Replaced limit/offset filters with from/to as timestamps (format '2006-01-02T15:04:05.000').The output document also contains 'prev' and 'next' fields (if there is any matching data) from the times before and after 'from' and 'to'.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2662
diff changeset
532 Imports []*models.Import `json:"imports"`
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
533 }{
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
534 Imports: imports,
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
535 Prev: prev,
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
536 Next: next,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
537 },
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
538 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
539 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
540 }
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
541
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
542 func importLogs(req *http.Request) (jr mw.JSONResult, err error) {
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
543
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
544 ctx := req.Context()
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
545
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
546 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
547
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
548 conn := mw.JSONConn(req)
4242
1458c9b0fdaa Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4196
diff changeset
549
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
550 // Check if he have such a import job first.
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
551 var summary sql.NullString
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
552 var enqueued time.Time
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
553 err = conn.QueryRowContext(ctx, selectImportSummarySQL, id).Scan(
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
554 &summary,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
555 &enqueued,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
556 )
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
557 switch {
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
558 case err == sql.ErrNoRows:
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
559 err = mw.JSONError{
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
560 Code: http.StatusNotFound,
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
561 Message: fmt.Sprintf("Cannot find import #%d.", id),
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
562 }
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
563 return
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
564 case err != nil:
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
565 return
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
566 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
567 enqueued = enqueued.UTC()
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
568
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
569 var sum any
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
570 if summary.Valid {
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
571 if err = json.NewDecoder(
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
572 strings.NewReader(summary.String)).Decode(&sum); err != nil {
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
573 return
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
574 }
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
575 }
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
576
1031
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
577 // We have it -> generate log entries.
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
578 var rows *sql.Rows
c0f5dedf5753 Check first if we have a import before generating log import entries list.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1026
diff changeset
579 rows, err = conn.QueryContext(ctx, selectImportLogsSQL, id)
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
580 if err != nil {
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
581 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
582 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
583 defer rows.Close()
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
584
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
585 entries := make([]*models.ImportLogEntry, 0, 10)
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
586
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
587 for rows.Next() {
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
588 var entry models.ImportLogEntry
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
589 var t time.Time
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
590 if err = rows.Scan(&t, &entry.Kind, &entry.Message); err != nil {
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
591 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
592 }
4148
1b1218bbd414 Made 'go vet' happy with controllers again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4084
diff changeset
593 entry.Time = models.ImportTime{Time: t.UTC()}
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
594 entries = append(entries, &entry)
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
595 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
596
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
597 if err = rows.Err(); err != nil {
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
598 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
599 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
600
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
601 jr = mw.JSONResult{
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
602 Result: struct {
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
603 Enqueued models.ImportTime `json:"enqueued"`
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5599
diff changeset
604 Summary any `json:"summary,omitempty"`
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
605 Entries []*models.ImportLogEntry `json:"entries"`
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
606 }{
3089
813309225e35 Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2954
diff changeset
607 Enqueued: models.ImportTime{Time: enqueued},
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
608 Summary: sum,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
609 Entries: entries,
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
610 },
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
611 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
612 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
613 }
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
614
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
615 func deleteImport(req *http.Request) (jr mw.JSONResult, err error) {
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
616
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
617 ctx := req.Context()
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
618 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
619
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
620 var tx *sql.Tx
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
621 tx, err = mw.JSONConn(req).BeginTx(ctx, nil)
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
622 if err != nil {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
623 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
624 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
625 defer tx.Rollback()
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
626
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
627 // Check if he have such a import job first.
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
628 var dummy bool
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
629 err = tx.QueryRowContext(ctx, selectHasNoRunningImportSQL, id).Scan(&dummy)
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
630 switch {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
631 case err == sql.ErrNoRows:
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
632 err = mw.JSONError{
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
633 Code: http.StatusNotFound,
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
634 Message: fmt.Sprintf("Cannot find import #%d.", id),
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
635 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
636 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
637 case err != nil:
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
638 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
639 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
640
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
641 if _, err = tx.ExecContext(ctx, deleteLogsSQL, id); err != nil {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
642 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
643 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
644
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
645 if _, err = tx.ExecContext(ctx, deleteImportSQL, id); err != nil {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
646 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
647 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
648
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
649 if err = tx.Commit(); err != nil {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
650 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
651 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
652
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
653 jr = mw.JSONResult{Code: http.StatusNoContent}
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
654
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
655 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
656 }
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
657
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
658 func reviewImports(req *http.Request) (mw.JSONResult, error) {
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
659
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
660 rs := *mw.JSONInput(req).(*[]models.Review)
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
661
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
662 type reviewResult struct {
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
663 ID int64 `json:"id"`
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
664 Message string `json:"message,omitempty"`
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
665 Error string `json:"error,omitempty"`
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
666 }
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
667
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
668 results := make([]reviewResult, len(rs))
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
669
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
670 for i := range results {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
671 results[i].ID = rs[i].ID
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
672 results[i].Message = fmt.Sprintf("Finalizing import #%d in progress.", rs[i].ID)
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
673 }
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
674
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
675 var wg sync.WaitGroup
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
676 var mu sync.Mutex
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
677
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
678 for i := range rs {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
679 wg.Add(1)
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
680 go func(idx int) {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
681 defer wg.Done()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
682 rev := &rs[idx]
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
683 msg, err := decideImport(req, rev.ID, string(rev.State))
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
684 mu.Lock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
685 if err != nil {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
686 results[idx].Error = err.Error()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
687 }
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
688 results[idx].Message = msg
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
689 mu.Unlock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
690 }(i)
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
691 }
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
692
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
693 done := make(chan struct{})
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
694 go func() {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
695 defer close(done)
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
696 wg.Wait()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
697 }()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
698
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
699 select {
5131
52e3980e3462 review decisions controller: Lowered immediate feedback timeout to 5 secs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5123
diff changeset
700 case <-time.After(5 * time.Second):
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
701 case <-done:
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
702 }
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
703
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
704 out := make([]reviewResult, len(rs))
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
705 mu.Lock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
706 copy(out, results)
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
707 mu.Unlock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
708
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
709 return mw.JSONResult{Result: out}, nil
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
710 }
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
711
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
712 func reviewImport(req *http.Request) (jr mw.JSONResult, err error) {
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
713
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
714 vars := mux.Vars(req)
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
715 id, _ := strconv.ParseInt(vars["id"], 10, 64)
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
716 state := vars["state"]
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
717
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
718 var msg string
5028
d727641911a5 Moved import desision logic to import queue (where it belongs).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4791
diff changeset
719 if msg, err = decideImport(req, id, state); err != nil {
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
720 return
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
721 }
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
722
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
723 result := struct {
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
724 Message string `json:"message"`
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
725 }{
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
726 Message: msg,
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
727 }
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
728
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
729 jr = mw.JSONResult{Result: &result}
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
730 return
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
731 }
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
732
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
733 func decideImport(
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
734 req *http.Request,
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
735 id int64,
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
736 state string,
1469
286a3306f6bf Import bulk review: Cleaned up input JSON document handling. Added proper error handling.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1468
diff changeset
737 ) (message string, err error) {
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
738
5028
d727641911a5 Moved import desision logic to import queue (where it belongs).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4791
diff changeset
739 session, _ := auth.GetSession(req)
d727641911a5 Moved import desision logic to import queue (where it belongs).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4791
diff changeset
740 reviewer := session.User
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
741
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
742 ctx := req.Context()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
743 accepted := state == "accepted"
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
744
5153
adf7b9f1273b Send the info if an import job was re-scheduled to sync callers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5131
diff changeset
745 switch err = imports.DecideImport(ctx, id, accepted, reviewer); {
adf7b9f1273b Send the info if an import job was re-scheduled to sync callers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5131
diff changeset
746 case err == imports.ErrRetrying:
5599
ceecc205011c Fixed error strings (found by staticcheck).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5564
diff changeset
747 return "", fmt.Errorf("finalizing import #%d is re-scheduled", id)
5153
adf7b9f1273b Send the info if an import job was re-scheduled to sync callers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5131
diff changeset
748 case err != nil:
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
749 return "", err
1355
b0ebb77f4ab0 Return nice JSON result if import was accepted or declined.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1351
diff changeset
750 }
b0ebb77f4ab0 Return nice JSON result if import was accepted or declined.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1351
diff changeset
751
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
752 return fmt.Sprintf("Import #%d is %s.", id, state), nil
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
753 }