Skip to content

Commit 09524b3

Browse files
authored
Merge pull request #17 from laravel-workflow/mongodb
MongoDB
2 parents 820dd97 + d556786 commit 09524b3

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

app/Http/Controllers/DashboardStatsController.php

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,74 @@ public function index() {
2424
->orderBy('updated_at')
2525
->first();
2626

27-
$maxDurationWorkflow = config('workflows.stored_workflow_model', StoredWorkflow::class)::select('*')
28-
->when(config('database.default') === 'mysql', function ($q) {
29-
return $q->addSelect(DB::raw('TIMEDIFF(created_at, updated_at) as duration'));
30-
})
31-
->when(config('database.default') === 'pgsql', function ($q) {
32-
return $q->addSelect(DB::raw('(EXTRACT(EPOCH FROM created_at - updated_at)) as duration'));
33-
})
34-
->where('status', '!=', 'pending')
35-
->orderBy('duration')
36-
->first();
27+
if (config('database.default') === 'mongodb' && $maxWaitTimeWorkflow && $maxWaitTimeWorkflow->_id) {
28+
$maxWaitTimeWorkflow->id = $maxWaitTimeWorkflow->_id;
29+
}
3730

38-
$maxExceptionsWorkflow = config('workflows.stored_workflow_model', StoredWorkflow::class)::withCount('exceptions')
39-
->orderByDesc('exceptions_count')
40-
->orderByDesc('updated_at')
41-
->first();
31+
if (config('database.default') === 'mongodb') {
32+
$maxDurationWorkflow = config('workflows.stored_workflow_model', StoredWorkflow::class)::select('*')
33+
->raw(function ($collection) {
34+
return $collection->aggregate([
35+
[
36+
'$match' => [
37+
'status' => [ '$ne' => 'pending' ]
38+
]
39+
],
40+
[
41+
'$addFields' => [
42+
'duration' => [
43+
'$subtract' => [
44+
['$toDate' => '$updated_at'],
45+
['$toDate' => '$created_at']
46+
]
47+
]
48+
]
49+
],
50+
[
51+
'$sort' => ['duration' => -1]
52+
],
53+
[
54+
'$limit' => 1
55+
]
56+
]);
57+
})
58+
->first();
59+
$maxDurationWorkflow->id = $maxDurationWorkflow->_id;
60+
} else {
61+
$maxDurationWorkflow = config('workflows.stored_workflow_model', StoredWorkflow::class)::select('*')
62+
->when(config('database.default') === 'mysql', function ($q) {
63+
return $q->addSelect(DB::raw('TIMEDIFF(created_at, updated_at) as duration'));
64+
})
65+
->when(config('database.default') === 'pgsql', function ($q) {
66+
return $q->addSelect(DB::raw('(EXTRACT(EPOCH FROM created_at - updated_at)) as duration'));
67+
})
68+
->where('status', '!=', 'pending')
69+
->orderBy('duration')
70+
->first();
71+
}
72+
73+
if (config('database.default') === 'mongodb') {
74+
$maxExceptionsWorkflow = null;
75+
76+
$mostExceptionWorkflowId = StoredWorkflowException::raw(function ($collection) {
77+
return $collection->aggregate([
78+
['$group' => ['_id' => '$stored_workflow_id', 'count' => ['$sum' => 1]]],
79+
['$sort' => ['count' => -1]],
80+
['$limit' => 1]
81+
]);
82+
})->first()['_id'];
83+
84+
$maxExceptionsWorkflow = StoredWorkflow::where('_id', $mostExceptionWorkflowId)->first();
85+
86+
$maxExceptionsWorkflow->exceptions_count = StoredWorkflowException::where('stored_workflow_id', $mostExceptionWorkflowId)->count();
87+
88+
$maxExceptionsWorkflow->id = $maxExceptionsWorkflow->_id;
89+
} else {
90+
$maxExceptionsWorkflow = config('workflows.stored_workflow_model', StoredWorkflow::class)::withCount('exceptions')
91+
->orderByDesc('exceptions_count')
92+
->orderByDesc('updated_at')
93+
->first();
94+
}
4295

4396
return response()->json([
4497
'flows' => config('workflows.stored_workflow_model', StoredWorkflow::class)::count(),

app/Http/Controllers/WorkflowsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function running() {
3232
}
3333

3434
public function show($id) {
35-
$flow = config('workflows.stored_workflow_model', StoredWorkflow::class)::whereId($id)->with(['exceptions', 'logs'])->first();
35+
$flow = config('workflows.stored_workflow_model', StoredWorkflow::class)::find($id)->with(['exceptions', 'logs'])->first();
3636

3737
$flow->arguments = serialize(Y::unserialize($flow->arguments));
3838

0 commit comments

Comments
 (0)