
com.threewks.thundr.bigmetrics.controller.EventQueueController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thundr-bigmetrics Show documentation
Show all versions of thundr-bigmetrics Show documentation
A thundr module for supporting metrics in big query
The newest version!
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://www.3wks.com.au/thundr
* Copyright (C) 2014 3wks,
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.threewks.thundr.bigmetrics.controller;
import java.util.Map;
import com.threewks.thundr.bigmetrics.BigMetricsService;
import com.threewks.thundr.bigmetrics.bigquery.BigQueryService;
import com.threewks.thundr.http.StatusCode;
import com.threewks.thundr.logger.Logger;
import com.threewks.thundr.view.string.StringView;
/**
* Controller handling endpoint events for the queues
*/
public class EventQueueController {
private BigMetricsService bigMetricsService;
private BigQueryService bigQueryService;
public EventQueueController(BigMetricsService bigMetricsService, BigQueryService bigQueryService) {
this.bigMetricsService = bigMetricsService;
this.bigQueryService = bigQueryService;
}
/**
* The endpoint for the queue that handles the insertion of the event into the big query datastore.
*
* @param xAppengineTaskname the header X-AppEngine-TaskName
added by GAE - used as the unique key for the big query
* insert request. Note: Thundr converts the header into the name xAppengineTaskname
* @param tableId the table id to insert the event record for
* @param data the data for the event - provided as a key -> value map
* @return an OK string view if all went well
*/
public StringView addEvent(String xAppengineTaskname, String tableId, Map data) {
try {
bigQueryService.insert(xAppengineTaskname, tableId, data);
return new StringView("OK").withStatusCode(StatusCode.OK);
} catch (Exception e) {
String errorMsg = "Failed to process event for tableId: %s - Error Message: %s";
Logger.error(errorMsg, tableId, e.getMessage());
return new StringView(errorMsg, tableId, e.getMessage()).withStatusCode(StatusCode.InternalServerError);
}
}
/**
* Ensures that the views in bigquery are up to date with all event tables.
*
* @return a string view with status code representing the outcome
*/
public StringView ensureViews() {
try {
bigMetricsService.ensureViewsExist();
return new StringView("OK");
} catch (Exception e) {
Logger.error("Failed to ensure big metrics views are up to date: %s", e.getMessage());
return new StringView(String.format("Failed to ensure big metrics views are up to date: %s", e.getMessage())).withStatusCode(StatusCode.InternalServerError);
}
}
/**
* Ensures that the tables in bigquery are up to date with all event tables.
*
* @return a string view with status code representing the outcome
*/
public StringView ensureTables() {
try {
bigMetricsService.ensureTablesExist();
return new StringView("OK");
} catch (Exception e) {
Logger.error("Failed to ensure big metrics tables are up to date: %s", e.getMessage());
return new StringView(String.format("Failed to ensure big metrics tables are up to date: %s", e.getMessage())).withStatusCode(StatusCode.InternalServerError);
}
}
}