package org.influxdb.inflow.examples;

import java.util.concurrent.TimeUnit;
import org.influxdb.dto.Point;
import org.influxdb.inflow.Client;
import org.influxdb.inflow.Database;
import org.influxdb.inflow.InflowException;

public class ExamplesApp {

  public static void main(String[] args) throws InflowException {
    
    // Initializeation examples
    exampleInitializeClient();
   
    // Reading Data Examples
    exampleReadData();
    
    // Writing Data Examples
    exampleWriteData();
  }
  
  public static void exampleInitializeClient() throws InflowException {
    // Initialize a new client with hostname and port
    Client client = new Client("influxdb.local", 8086, "inflowexample", "inflow011");
    
    // You can also create a client from a URI describing the server
    Client clientFromURI = Client.fromURI("http://inflowexample:inflow011@bludgeon:8086");
    
    // if you use Database.fromURI(), it will be setup with the database name in the URI
    Database databaseFromURI = Database.fromURI("http://inflowexample:inflow011@bludgeon:8086/inflow1");
  }
  
  public static void exampleWriteData() throws InflowException {
    Client client = Client.fromURI("http://inflowexample:inflow011@bludgeon:8086");

    // specify to use inflow_test database
    Database database = client.selectDB("inflow_test");

    // create some points to write.
    // The name of a measurement and the value are mandatory. Additional fields, tags and a timestamp are optional.
    Point point1 = Point
            .measurement("cpu_load_short")
            .field("value", 0.64)
            .tag("host", "server01")
            .tag("region", "us-west")
            .field("cpucount", 10)
            .time(1435222310, TimeUnit.SECONDS) // 2015-06-25 08:51:50 GMT
            .build();
    // Note: It's possible to add multiple fields (see https://influxdb.com/docs/v0.9/concepts/key_concepts.html) when writing measurements to InfluxDB.

    Point point2 = Point
            .measurement("cpu_load_short")
            .field("value", 0.84)
            .build();
    // Note: No .time() - InfluxDB uses the current time as the default timestamp.

    Point[] points = new Point[]{point1, point2};

    database.writePoints(points);
  }

}

@@ -202,24 +202,32 @@
 nflow.Inflow
+Database
 Exception;%0A%0A
@@ -229,35 +229,222 @@
 on;%0A
-%0Apublic class ExamplesApp %7B
+import org.influxdb.inflow.InflowException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class ExamplesApp %7B%0A  %0A  private final static Logger logger = LoggerFactory.getLogger(ExamplesApp.class);
 %0A%0A  
@@ -500,24 +500,49 @@
 lowException
+, InflowDatabaseException
  %7B%0A    %0A    
@@ -1370,192 +1370,496 @@
 tion
- %7B%0A    Client client = Client.fromURI(%22http://inflowexample:inflow011@bludgeon:8086%22);%0A%0A    // specify to use inflow_test database%0A    Database database = client.selectDB(%22inflow_test%22
+, InflowDatabaseException %7B%0A    String influxdbURI = %22http://inflowexample:inflow011@bludgeon:8086%22;%0A    %0A    logger.info(%22Creating client from URI %22 + influxdbURI);%0A    Client client = Client.fromURI(influxdbURI);%0A%0A    // specify to use inflow_test database%0A    Database database = client.selectDB(%22inflow_test%22);%0A    %0A    // make sure database exists before trying to write to it%0A    logger.info(%22Creating database %22 + database.getName() + %22 before writing to it%22);%0A    database.create(
 );%0A%0A
@@ -2732,16 +2732,107 @@
 int2%7D;%0A%0A
+    logger.info(%22Writing %22 + points.length + %22 points to database %22 + database.getName());%0A
     data

create database before writing to it announce what we are doing with slf4j logger