View | Details | Raw Unified | Return to bug 37905
Collapse All | Expand All

(-)a/Koha/BackgroundJobs.pm (-14 lines)
Lines 72-91 sub filter_by_current { Link Here
72
    );
72
    );
73
}
73
}
74
74
75
=head3 filter_by_last_hour
76
77
    my $current_jobs = $jobs->filter_by_last_hour;
78
79
Returns a new resultset, filtering out jobs that were enqueued more than an hour ago.
80
81
=cut
82
83
sub filter_by_last_hour {
84
    my ($self) = @_;
85
86
    return $self->search( { enqueued_on => { '>', \"NOW() - INTERVAL 1 HOUR" } } );
87
}
88
89
=head2 Internal methods
75
=head2 Internal methods
90
76
91
=head3 _type
77
=head3 _type
(-)a/Koha/REST/V1/BackgroundJobs.pm (-7 / +1 lines)
Lines 38-47 sub list { Link Here
38
38
39
    return try {
39
    return try {
40
40
41
        my $only_current   = $c->param('only_current');
41
        my $only_current = $c->param('only_current');
42
        my $only_last_hour = $c->param('only_last_hour');
43
        $c->req->params->remove('only_current');
42
        $c->req->params->remove('only_current');
44
        $c->req->params->remove('only_last_hour');
45
43
46
        my $bj_rs = Koha::BackgroundJobs->new;
44
        my $bj_rs = Koha::BackgroundJobs->new;
47
45
Lines 49-58 sub list { Link Here
49
            $bj_rs = $bj_rs->filter_by_current;
47
            $bj_rs = $bj_rs->filter_by_current;
50
        }
48
        }
51
49
52
        if ($only_last_hour) {
53
            $bj_rs = $bj_rs->filter_by_last_hour;
54
        }
55
56
        return $c->render(
50
        return $c->render(
57
            status  => 200,
51
            status  => 200,
58
            openapi => $c->objects->search($bj_rs)
52
            openapi => $c->objects->search($bj_rs)
(-)a/api/v1/swagger/paths/jobs.yaml (-5 lines)
Lines 14-24 Link Here
14
        required: false
14
        required: false
15
        type: boolean
15
        type: boolean
16
        description: Only include current jobs
16
        description: Only include current jobs
17
      - name: only_last_hour
18
        in: query
19
        required: false
20
        type: boolean
21
        description: Only include jobs started in the last hour
22
      - $ref: "../swagger.yaml#/parameters/match"
17
      - $ref: "../swagger.yaml#/parameters/match"
23
      - $ref: "../swagger.yaml#/parameters/order_by"
18
      - $ref: "../swagger.yaml#/parameters/order_by"
24
      - $ref: "../swagger.yaml#/parameters/page"
19
      - $ref: "../swagger.yaml#/parameters/page"
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt (-12 / +25 lines)
Lines 259-279 Link Here
259
                $("#job_details").show();
259
                $("#job_details").show();
260
            [% END %]
260
            [% END %]
261
261
262
            let query_filters = function(){
262
            let additional_filters = {
263
                if ( $("#only_current").is(":checked") && !$("#include_last_hour").is(":checked") ) {
263
                enqueued_on: function(){
264
                    return 'only_current=1&only_last_hour=0';
264
                    let now = new Date();
265
                } else if ( $("#include_last_hour").is(":checked") && !$("#only_current").is(":checked")) {
265
                    if ( $("#include_last_hour").is(":checked") ) {
266
                    return 'only_current=0&only_last_hour=1';
266
                        now.setHours(now.getHours() - 1);
267
                } else if ( $("#only_current").is(":checked") && $("#include_last_hour").is(":checked") ) {
267
                        return { ">": now.toISOString() };
268
                    return 'only_current=1&only_last_hour=1';
268
                    } else {
269
                        return { "<": now.toISOString() };
270
                    }
271
                }
272
            };
273
274
            let only_current_filter = function(){
275
                if ( $("#only_current").is(":checked") ) {
276
                    return 'only_current=1';
269
                } else {
277
                } else {
270
                    return 'only_current=0&only_last_hour=0';
278
                    return 'only_current=0';
271
                }
279
                }
272
            }
280
            }
273
281
274
            let jobs_table = $("#table_jobs").kohaTable({
282
            let jobs_table = $("#table_jobs").kohaTable({
275
                "ajax": {
283
                "ajax": {
276
                    "url": "/api/v1/jobs?" + query_filters(),
284
                    "url": "/api/v1/jobs?" + only_current_filter()
277
                },
285
                },
278
                "order": [[ 1, "desc" ]],
286
                "order": [[ 1, "desc" ]],
279
                "columns": [
287
                "columns": [
Lines 342-351 Link Here
342
                        "orderable": false
350
                        "orderable": false
343
                    }
351
                    }
344
                ]
352
                ]
345
            }, null, 1);
353
            }, null, 1, additional_filters);
354
355
            $("#include_last_hour").on("change", function(){
356
                jobs_table.DataTable().draw();
357
                return false;
358
            });
346
359
347
            $("#only_current, #include_last_hour").on("change", function(){
360
            $("#only_current").on("change", function(){
348
                jobs_table.DataTable().ajax.url("/api/v1/jobs?" + query_filters()).load();
361
                jobs_table.DataTable().ajax.url("/api/v1/jobs?" + only_current_filter()).load();
349
                return false;
362
                return false;
350
            });
363
            });
351
        });
364
        });
(-)a/t/db_dependent/Koha/BackgroundJobs.t (-60 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 16;
22
use Test::More tests => 14;
23
use Test::MockModule;
23
use Test::MockModule;
24
24
25
use List::MoreUtils qw(any);
25
use List::MoreUtils qw(any);
Lines 123-186 subtest 'filter_by_current() tests' => sub { Link Here
123
    $schema->storage->txn_rollback;
123
    $schema->storage->txn_rollback;
124
};
124
};
125
125
126
subtest 'filter_by_last_hour() tests' => sub {
127
128
    plan tests => 2;
129
130
    $schema->storage->txn_begin;
131
132
    my $job_now =
133
        $builder->build_object( { class => 'Koha::BackgroundJobs', value => { enqueued_on => dt_from_string } } );
134
    my $job_old = $builder->build_object(
135
        { class => 'Koha::BackgroundJobs', value => { enqueued_on => dt_from_string->subtract( hours => 2 ) } } );
136
    my $rs = Koha::BackgroundJobs->search( { id => [ $job_now->id, $job_old->id ] } );
137
138
    is( $rs->count, 2, '2 jobs in resultset' );
139
140
    $rs = $rs->filter_by_last_hour;
141
142
    is( $rs->count, 1, 'Only 1 job in filtered resultset' );
143
144
    $schema->storage->txn_rollback;
145
};
146
147
subtest 'filter_by_last_hour() and filter_by_current() tests' => sub {
148
149
    plan tests => 3;
150
151
    $schema->storage->txn_begin;
152
153
    my $job_new_now = $builder->build_object(
154
        { class => 'Koha::BackgroundJobs', value => { status => 'new', enqueued_on => dt_from_string } } );
155
    my $job_new_old = $builder->build_object(
156
        {
157
            class => 'Koha::BackgroundJobs',
158
            value => { status => 'new', enqueued_on => dt_from_string->subtract( hours => 2 ) }
159
        }
160
    );
161
    my $job_cancelled = $builder->build_object(
162
        { class => 'Koha::BackgroundJobs', value => { status => 'cancelled', enqueued_on => dt_from_string } } );
163
    my $job_failed = $builder->build_object(
164
        { class => 'Koha::BackgroundJobs', value => { status => 'failed', enqueued_on => dt_from_string } } );
165
    my $job_finished = $builder->build_object(
166
        { class => 'Koha::BackgroundJobs', value => { status => 'finished', enqueued_on => dt_from_string } } );
167
168
    my $rs = Koha::BackgroundJobs->search(
169
        { id => [ $job_new_now->id, $job_new_old->id, $job_cancelled->id, $job_failed->id, $job_finished->id ] } );
170
171
    is( $rs->count, 5, '5 jobs in resultset' );
172
173
    $rs = $rs->filter_by_last_hour;
174
175
    is( $rs->count, 4, '4 jobs in last hour' );
176
177
    $rs = $rs->filter_by_current;
178
179
    is( $rs->count, 1, 'Only 1 current job in last hour' );
180
181
    $schema->storage->txn_rollback;
182
};
183
184
subtest 'search_limited' => sub {
126
subtest 'search_limited' => sub {
185
    plan tests => 3;
127
    plan tests => 3;
186
128
187
- 

Return to bug 37905