-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWikiEntityImportController.php
More file actions
117 lines (102 loc) · 4.21 KB
/
WikiEntityImportController.php
File metadata and controls
117 lines (102 loc) · 4.21 KB
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
<?php
namespace App\Http\Controllers;
use App\WikiEntityImportStatus;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\WikiEntityImport;
use App\Jobs\WikiEntityImportJob;
use Carbon\Carbon;
use Prometheus\CollectorRegistry;
use Prometheus\Counter;
class WikiEntityImportController extends Controller
{
private Counter $successfulCounter;
private Counter $failedCounter;
public function __construct(CollectorRegistry $registry)
{
$this->successfulCounter = $registry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
'The number of successful Entity import records.',
);
$this->failedCounter = $registry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_failed',
'The number of failed Entity import records.',
);
}
public function get(Request $request): \Illuminate\Http\JsonResponse
{
$wiki = $request->attributes->get('wiki');
$imports = $wiki->wikiEntityImports()->get();
return response()->json(['data' => $imports]);
}
public function create(Request $request): \Illuminate\Http\JsonResponse
{
$validatedInput = $request->validate([
'source_wiki_url' => ['required', 'url'],
'entity_ids' => ['required', 'string', function (string $attr, mixed $value, \Closure $fail) {
$chunks = explode(',', $value);
foreach ($chunks as $chunk) {
if (!preg_match("/^[A-Z]\d+(@\d+)?$/", $chunk)) {
$fail("Received unexpected input '{$chunk}' cannot continue.");
}
}
}],
]);
$wiki = $request->attributes->get('wiki');
$imports = $wiki->wikiEntityImports()->get();
foreach ($imports as $import) {
if ($import->status === WikiEntityImportStatus::Success) {
return response()
->json(['error' => 'Wiki "'.$wiki->domain.'" already has performed a successful entity import'])
->setStatusCode(400);
}
if ($import->status === WikiEntityImportStatus::Pending) {
return response()
->json(['error' => 'Wiki "'.$wiki->domain.'" currently has a pending entity import'])
->setStatusCode(400);
}
}
$import = $wiki->wikiEntityImports()->create([
'status' => WikiEntityImportStatus::Pending,
'started_at' => Carbon::now(),
'payload' => $validatedInput,
]);
dispatch(new WikiEntityImportJob(
wikiId: $wiki->id,
sourceWikiUrl: $validatedInput['source_wiki_url'],
importId: $import->id,
entityIds: explode(',', $validatedInput['entity_ids']),
));
return response()->json(['data' => $import]);
}
public function update(Request $request): \Illuminate\Http\JsonResponse
{
// This route is not supposed have ACL middlewares in front as it is expected
// to be called from backend services that are implicitly allowed
// access right.
$validatedInput = $request->validate([
'wiki_entity_import' => ['required', 'integer'],
'status' => ['required', Rule::in([WikiEntityImportStatus::Failed->value, WikiEntityImportStatus::Success->value])],
]);
$import = WikiEntityImport::find($validatedInput['wiki_entity_import']);
if (!$import) {
abort(404, 'No such import');
}
if ($import->status !== WikiEntityImportStatus::Pending) {
abort(400, 'Import has to be pending if updated');
}
if ($validatedInput['status'] === WikiEntityImportStatus::Failed->value) {
$this->failedCounter->inc();
}
if ($validatedInput['status'] === WikiEntityImportStatus::Success->value) {
$this->successfulCounter->inc();
}
$import->update([
'status' => $validatedInput['status'],
'finished_at' => Carbon::now(),
]);
return response()->json(['data' => $import]);
}
}