-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
159 lines (145 loc) · 4.04 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2024 Tomas Machalek <[email protected]>
// Copyright 2024 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"camus/archiver"
"camus/cncdb"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/czcorpus/cnc-gokit/uniresp"
"github.com/gin-gonic/gin"
)
var (
brokenConcRec1 = regexp.MustCompile(`^get concordance:[^:]+:\s*`)
)
type visitedIds map[string]int
func (v visitedIds) containsCycle() bool {
for _, cnt := range v {
if cnt > 1 {
return true
}
}
return false
}
func (v visitedIds) IDList() []string {
ans := make([]string, 0, len(v))
for k := range v {
ans = append(ans, k)
}
return ans
}
// ------
type Actions struct {
ArchKeeper *archiver.ArchKeeper
}
func (a *Actions) Overview(ctx *gin.Context) {
ans := make(map[string]any)
ans["archiver"] = a.ArchKeeper.GetStats()
var forceTotalsReload bool
if ctx.Query("forceReload") == "1" {
forceTotalsReload = true
}
totals, err := a.ArchKeeper.YearsStats(forceTotalsReload)
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError)
return
}
ans["totals"] = totals
uniresp.WriteJSONResponse(ctx.Writer, ans)
}
func (a *Actions) GetRecord(ctx *gin.Context) {
rec, err := a.ArchKeeper.LoadRecordsByID(ctx.Param("id"))
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError) // TODO
return
}
uniresp.WriteJSONResponse(ctx.Writer, rec)
}
func (a *Actions) Validate(ctx *gin.Context) {
currID := ctx.Param("id")
visitedIDs := make(visitedIds)
for currID != "" {
visitedIDs[currID]++
if visitedIDs.containsCycle() {
uniresp.WriteJSONResponse(
ctx.Writer,
map[string]any{"message": fmt.Sprintf("Possible cycle in %s", currID)},
)
return
}
recs, err := a.ArchKeeper.LoadRecordsByID(currID)
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError) // TODO
return
}
queryVariants := make(map[string]int)
var reprData cncdb.GeneralDataRecord
for _, rec := range recs {
data, err := rec.FetchData()
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError) // TODO
return
}
queryVariants[strings.Join(data.GetQuery(), " ")]++
reprData = data
}
if len(queryVariants) > 1 {
uniresp.WriteJSONResponse(
ctx.Writer,
map[string]any{"message": "Inconsistent query across instances"},
)
return
}
currID = reprData.GetPrevID()
}
uniresp.WriteJSONResponse(
ctx.Writer,
map[string]any{
"ok": true,
"visitedIds": visitedIDs.IDList(),
},
)
}
func (a *Actions) Fix(ctx *gin.Context) {
recs, err := a.ArchKeeper.LoadRecordsByID(ctx.Param("id"))
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError) // TODO
return
}
fixedRecs := make([]cncdb.ArchRecord, len(recs))
for i, rec := range recs {
rec.Data = brokenConcRec1.ReplaceAllString(rec.Data, "")
fixedRecs[i] = rec
}
newRec, err := a.ArchKeeper.DeduplicateInArchive(fixedRecs, fixedRecs[0])
if err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError) // TODO
return
}
ans := make(map[string]any)
ans["numInstances"] = len(recs)
ans["fixed"] = newRec
uniresp.WriteJSONResponse(ctx.Writer, ans)
}
func (a *Actions) DedupReset(ctx *gin.Context) {
if err := a.ArchKeeper.Reset(); err != nil {
uniresp.RespondWithErrorJSON(ctx, err, http.StatusInternalServerError)
return
}
uniresp.WriteJSONResponse(ctx.Writer, map[string]any{"ok": true})
}