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

com.thorstenmarx.webtools.test.MockAnalyticsDB Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * Copyright (C) 2019 Thorsten Marx
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.thorstenmarx.webtools.test;

/*-
 * #%L
 * webtools-actions
 * %%
 * Copyright (C) 2016 - 2019 Thorsten Marx
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * 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
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

/**
 *
 * @author marx
 */
import com.alibaba.fastjson.JSONObject;
import com.thorstenmarx.webtools.api.analytics.AnalyticsDB;
import com.thorstenmarx.webtools.api.analytics.Filter;
import com.thorstenmarx.webtools.api.analytics.query.Aggregator;
import com.thorstenmarx.webtools.api.analytics.query.LimitProvider;
import com.thorstenmarx.webtools.api.analytics.query.Query;
import com.thorstenmarx.webtools.api.analytics.query.ShardDocument;
import com.thorstenmarx.webtools.api.analytics.query.ShardedQuery;
import com.thorstenmarx.webtools.tracking.referrer.ReferrerFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;

/**
 *
 * @author marx
 */
public class MockAnalyticsDB implements AnalyticsDB {

	protected final List documents = new ArrayList<>();

	@Override
	public  R queryAsync(ShardedQuery sq) {
		return ForkJoinPool.commonPool().invoke(new MockAsyncShardQuery<>(this, sq));
	}

	@Override
	public  CompletableFuture query(Query query, Aggregator aggregator) {
		try {
			aggregator.documents(documents);

			return CompletableFuture.supplyAsync(() -> {
				try {
					return aggregator.call();
				} catch (Exception ex) {
					throw new RuntimeException(ex);
				}
			});
//			return pool.submit(aggregator);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	@Override
	public void track(Map> event) {
		final JSONObject jsonEvent = new JSONObject();
		jsonEvent.putAll(event);
		ReferrerFilter.filter(jsonEvent);
		
		documents.add(new ShardDocument("mock", jsonEvent.getJSONObject("data")));
		
	}

	@Override
	public void addFilter(Filter filter) {
		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

	@Override
	public List getFilters() {
		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

	@Override
	public boolean hasFilters() {
		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy