annotate pkg/controllers/importqueue.go @ 5564:aaa9e658cabd

Log export: Added marker interface to JobCreators that log messages should be loaded at export. Removes the hard coded SR export SQL logic.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 12 Feb 2022 18:56:55 +0100
parents 3be1d79ad3a3
children ceecc205011c
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,
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
77 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
78 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
79 ELSE NULL
3be1d79ad3a3 Log export: Limit log lines loading to 'sr' imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5562
diff changeset
80 END AS msgs
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
81 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
82 WHERE
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
83 `
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
84 selectEnqueuedSQL = `
2672
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
85 SELECT enqueued FROM import.imports
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
86 WHERE
b997e1fd1d3d Fixed warning SQL prefix for selection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2671
diff changeset
87 `
2795
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
88 selectImportSummarySQL = `
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
89 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
90
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
91 selectHasNoRunningImportSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
92 SELECT true FROM import.imports
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
93 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
94
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
95 selectImportLogsSQL = `
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
96 SELECT
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
97 time,
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
98 kind::varchar,
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
99 msg
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
100 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
101 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
102 ORDER BY time`
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
103
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
104 deleteLogsSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
105 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
106
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
107 deleteImportSQL = `
1995
59055c8301df Move import queue to its own database namespace
Tom Gottfried <tom@intevation.de>
parents: 1693
diff changeset
108 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
109 )
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110
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
111 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
112 stmt strings.Builder
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 args []interface{}
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 }
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
115
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
116 func buildFilters(projection string, req *http.Request, args ...interface{}) (
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
117 *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
118 *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
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 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
121 ) {
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
122
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
123 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
124
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
125 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
126
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
127 cond := func(format string, args ...interface{}) {
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
128 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
129 l = append(l, term)
4195
ad13c581de7c Fixed building filters for listing import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4187
diff changeset
130 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
131 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
132 }
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
133
5108
89639e02cff0 Do not show internal review jobs to users
Tom Gottfried <tom@intevation.de>
parents: 5093
diff changeset
134 // 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
135 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
136
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
137 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
138 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
139 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
140 `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
141 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
142 }
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
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
144 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
145 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
146 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
147 "(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
148 "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
149 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
150 }
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
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
152 if st := req.FormValue("states"); st != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
153 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
154 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
155 }
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
156
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
157 if ks := req.FormValue("kinds"); ks != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
158 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
159 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
160 }
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
161
2667
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
162 if idss := req.FormValue("ids"); idss != "" {
5ece2c51d1f0 More filter simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2665
diff changeset
163 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
164 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
165 }
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
166
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 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
168 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
169 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
170 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
171 }
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
172 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
173 b = append(b, buildFilterTerm("enqueued < $%d", fromTime))
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
174 } else {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
175 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
176 }
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
177
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 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
179 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
180 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
181 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
182 }
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
183 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
184 a = append(a, buildFilterTerm("enqueued > $%d", toTime))
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
185 } else {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
186 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
187 }
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
188
2665
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
189 switch warn := strings.ToLower(req.FormValue("warnings")); warn {
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
190 case "1", "t", "true":
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
191 cond(` EXISTS(SELECT 1 FROM import.import_logs
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
192 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
193 }
fea12fc850d2 Simplified warnings filtering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2663
diff changeset
194
4196
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
195 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
196 case "1", "t", "true":
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
197 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
198 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
199 }
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
200
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
201 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
202 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
203 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
204
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
205 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
206 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
207 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
208
4187
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
209 fa.stmt.WriteString(selectEnqueuedSQL)
65a5501dc13d Enable faster access to import logs
Tom Gottfried <tom@intevation.de>
parents: 4148
diff changeset
210 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
211
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
212 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
213
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
214 if projection != "" {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
215 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
216 } else {
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
217 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
218 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
219 counting = true
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
220 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
221 default:
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(selectImportsSQL)
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
223 }
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
224 }
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
225
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
226 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
227 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
228 } 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
229 l.serialize(&fl.stmt, &fl.args)
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
230 }
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
231
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
232 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
233 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
234 } 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
235 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
236 }
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
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 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
239 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
240 } 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
241 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
242 }
34315277f2d6 Added ids filter to importqueue listing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1495
diff changeset
243
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
244 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
245 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
246 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
247 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
248 }
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
249
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
250 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
251 fb = nil
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
252 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
253 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
254 fa = nil
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
255 }
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
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
257 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
258 }
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
259
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
260 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
261
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
262 var when time.Time
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
263 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
264 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
265 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
266 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
267 case err != nil:
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
268 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
269 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
270 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
271 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
272 }
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
273
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
274 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
275
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
276 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
277
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 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
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 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
281 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
282 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
283 }
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 })
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 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
287 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
288 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
289 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
290 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
291 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
292 }
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
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 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
295
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
296 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
297 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
298 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
299 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
300
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
301 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
302 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
303
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
304 record := []string{
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
305 "#id",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
306 "#kind",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
307 "#enqueued",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
308 "#changed",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
309 "#user",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
310 "#country",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
311 "#signer",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
312 "#state",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
313 "#warnings",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
314 "#source",
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
315 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
316
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
317 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
318 // Too late for HTTP status message.
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
319 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
320 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
321 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
322
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
323 start := time.Now()
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
324
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
325 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
326 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
327 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
328 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
329 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
330 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
331 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
332 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
333
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
334 // 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
335 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
336 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
337 }
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
338
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 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
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 warnings 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
349 data string
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
350 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
351 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
352
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 // 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
354 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
355
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
356 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
357
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 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
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 // 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
361 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
362 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
363 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
364 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
365 } 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
366 var ms []string
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
367 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
368 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
369 return err
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
370 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
371 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
372 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
373 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
374 }
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
375 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
376 } 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
377 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
378 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
379 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
380 }
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 }
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 }
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
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
384 var signer string
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
385 if ds.signer.Valid {
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
386 signer = ds.signer.String
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
387 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
388
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
389 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
390 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
391 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
392 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
393 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
394 record[5] = ds.country
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
395 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
396 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
397 record[8] = strconv.FormatBool(ds.warnings)
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[9] = description
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
399
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 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
401 }
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
402
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
403 for rows.Next() {
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
404 var curr dataset
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
405
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
406 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
407 &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
408 &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
409 &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
410 &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
411 &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
412 &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
413 &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
414 &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
415 &curr.warnings,
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.data,
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
417 &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
418 ); err != nil {
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
419 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
420 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
421 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
422
5561
b91716d2acc6 Fixed log message analyzing in log export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5560
diff changeset
423 if err := store(&curr); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
424 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
425 return
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
426 }
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
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
429 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
430 log.Errorf("%v\n", err)
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
431 return
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
432 }
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
433
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
434 out.Flush()
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
435 if err := out.Error(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5153
diff changeset
436 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
437 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
438
5562
5152b4db40cc Simplified code of log export a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5561
diff changeset
439 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
440 }
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
441
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
442 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
443
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
444 var list, before, after *filledStmt
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
445
4791
1fef4679b07a Added an endpoint GET api/imports/export to export imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4748
diff changeset
446 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
447 return
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
448 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
449
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
450 ctx := req.Context()
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
451
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
452 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
453
2682
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
454 // 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
455
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
456 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
457 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
458 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
459 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
460 switch {
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
461 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
462 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
463 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
464 return
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
465 }
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
466 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
467 return
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
468 }
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
469
9a493d27bf3f Added /api/imports?count=true to only count the results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2681
diff changeset
470 // 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
471
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
472 var rows *sql.Rows
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
473 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
474 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
475 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
476 defer rows.Close()
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
477
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
478 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
479
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
480 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
481
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
482 for rows.Next() {
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
483 var it models.Import
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
484 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
485 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
486 if err = rows.Scan(
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
487 &it.ID,
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
488 &it.State,
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
489 &enqueued,
4748
47922c1a088d Added a 'changed' column to the import.imports table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4244
diff changeset
490 &changed,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
491 &it.Kind,
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
492 &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
493 &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
494 &it.Summary,
4196
948e312e87bc Add flag and filter for imports having errors
Tom Gottfried <tom@intevation.de>
parents: 4195
diff changeset
495 &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
496 &it.Warnings,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
497 ); err != nil {
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
498 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
499 }
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
500 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
501 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
502 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
503 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
504 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
505 imports = append(imports, &it)
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
506 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
507
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
508 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
509 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
510 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
511
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
512 var prev, next *models.ImportTime
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
513
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
514 if before != nil {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
515 prev = neighbored(ctx, conn, before)
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
516 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
517
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
518 if after != nil {
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
519 next = neighbored(ctx, conn, after)
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
520 }
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
521
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
522 jr = mw.JSONResult{
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
523 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
524 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
525 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
526 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
527 }{
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
528 Imports: imports,
2671
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
529 Prev: prev,
8f3facf902dd Filter prev/next, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2670
diff changeset
530 Next: next,
1023
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
531 },
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
532 }
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
533 return
337a7f4c8a16 Add endpoint to list all or some import jobs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
534 }
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
535
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
536 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
537
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
538 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
539
1026
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
540 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
541
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
542 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
543
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
544 // 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
545 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
546 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
547 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
548 &summary,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
549 &enqueued,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
550 )
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
551 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
552 case err == sql.ErrNoRows:
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
553 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
554 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
555 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
556 }
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 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
558 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
559 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
560 }
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
561 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
562
2662
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
563 var sum interface{}
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
564 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
565 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
566 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
567 return
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
568 }
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
569 }
f90557a8c960 Moved summary from /api/imports overview to /api/imports/{id} details.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2627
diff changeset
570
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
571 // 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
572 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
573 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
574 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
575 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
576 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
577 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
578
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
579 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
580
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
581 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
582 var entry models.ImportLogEntry
4084
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
583 var t time.Time
350a24c92848 Deliver times from import queue in UTC.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
584 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
585 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
586 }
4148
1b1218bbd414 Made 'go vet' happy with controllers again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4084
diff changeset
587 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
588 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
589 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
590
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
591 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
592 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
593 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
594
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
595 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
596 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
597 Enqueued models.ImportTime `json:"enqueued"`
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
598 Summary interface{} `json:"summary,omitempty"`
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
599 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
600 }{
3089
813309225e35 Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2954
diff changeset
601 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
602 Summary: sum,
241e7f05a538 /api/imports/{id} result now also contains the 'enqueued' time.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2772
diff changeset
603 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
604 },
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
605 }
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
606 return
3de54d7b7d30 Added endpoint to show the detail logs of an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1023
diff changeset
607 }
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
608
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
609 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
610
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
611 ctx := req.Context()
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
612 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
613
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
614 var tx *sql.Tx
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
615 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
616 if err != nil {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
617 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
618 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
619 defer tx.Rollback()
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
620
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
621 // 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
622 var dummy bool
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
623 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
624 switch {
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
625 case err == sql.ErrNoRows:
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
626 err = mw.JSONError{
1037
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
627 Code: http.StatusNotFound,
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
628 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
629 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
630 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
631 case err != nil:
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
632 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
633 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
634
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
635 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
636 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
637 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
638
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
639 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
640 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
641 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
642
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
643 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
644 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
645 }
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
646
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
647 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
648
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
649 return
a04126989d91 Added endpoint to delete job from import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1031
diff changeset
650 }
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
651
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
652 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
653
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
654 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
655
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
656 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
657 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
658 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
659 Error string `json:"error,omitempty"`
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
660 }
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 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
663
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
664 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
665 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
666 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
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
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
669 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
670 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
671
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
672 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
673 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
674 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
675 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
676 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
677 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
678 mu.Lock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
679 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
680 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
681 }
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
682 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
683 mu.Unlock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
684 }(i)
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
685 }
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
686
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
687 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
688 go func() {
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
689 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
690 wg.Wait()
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 select {
5131
52e3980e3462 review decisions controller: Lowered immediate feedback timeout to 5 secs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5123
diff changeset
694 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
695 case <-done:
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
696 }
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 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
699 mu.Lock()
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
700 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
701 mu.Unlock()
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 return mw.JSONResult{Result: out}, nil
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
704 }
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
705
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
706 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
707
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
708 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
709 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
710 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
711
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
712 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
713 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
714 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
715 }
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
716
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 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
718 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
719 }{
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 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
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
4244
4394daeea96a Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4243
diff changeset
723 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
724 return
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
725 }
1192
3afa71405b87 Added REST endpoint to accept or decline an import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1189
diff changeset
726
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
727 func decideImport(
1468
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
728 req *http.Request,
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
729 id int64,
5e1218b5a123 proof of concept
Thomas Junk <thomas.junk@intevation.de>
parents: 1392
diff changeset
730 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
731 ) (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
732
5028
d727641911a5 Moved import desision logic to import queue (where it belongs).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4791
diff changeset
733 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
734 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
735
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
736 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
737 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
738
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
739 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
740 case err == imports.ErrRetrying:
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
741 return "", fmt.Errorf("Finalizing import #%d is re-scheduled.", id)
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
742 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
743 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
744 }
b0ebb77f4ab0 Return nice JSON result if import was accepted or declined.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1351
diff changeset
745
5123
eeb45e3e0a5a Added mechanism to have sync import jobs on import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5109
diff changeset
746 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
747 }