All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog.plugins.sidecar.services.ImportService Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog.plugins.sidecar.services;

import org.graylog.plugins.sidecar.rest.models.CollectorUpload;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoConnection;
import org.graylog2.database.PaginatedDbService;
import org.graylog2.database.PaginatedList;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Period;
import org.mongojack.DBQuery;
import org.mongojack.DBSort;

import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ImportService extends PaginatedDbService {
    private static final String COLLECTION_NAME = "collector_uploads";

    @Inject
    public ImportService(MongoConnection mongoConnection,
                         MongoJackObjectMapperProvider mapper){
        super(mongoConnection, mapper, CollectorUpload.class, COLLECTION_NAME);
    }

    public PaginatedList findPaginated(int page, int perPage, String sortField, String order) {
        final DBSort.SortBuilder sortBuilder = getSortBuilder(order, sortField);
        return findPaginatedWithQueryAndSort(DBQuery.empty(), sortBuilder, page, perPage);
    }


    public List all() {
        try (final Stream collectorUploadStream = streamAll()) {
            return collectorUploadStream.collect(Collectors.toList());
        }
    }

    public long count() {
        return db.count();
    }

    public int destroyExpired(Period period) {
        final DateTime threshold = DateTime.now(DateTimeZone.UTC).minus(period);
        int count;

        try (final Stream uploadStream = streamAll()) {
            count = uploadStream
                    .mapToInt(upload -> {
                        if (upload.created().isBefore(threshold)) {
                            return delete(upload.id());
                        }
                        return 0;
                    })
                    .sum();
        }

        return count;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy