/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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 quarks.samples.topology;

import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.google.gson.JsonObject;

import quarks.execution.Job;
import quarks.execution.services.job.JobRegistryService;
import quarks.providers.direct.DirectProvider;
import quarks.runtime.jobregistry.JobEvents;
import quarks.runtime.jobregistry.JobRegistry;
import quarks.topology.TStream;
import quarks.topology.Topology;
import quarks.topology.plumbing.PlumbingStreams;

/**
 * Demonstrates job monitoring using the {@link JobRegistryService} service.
 * <p>
 * The example starts a system monitoring application, then concurrently 
 * submits two jobs.
 * The job monitoring application generates job event tuples when jobs 
 * are added or removed from registry, or when a job gets updated. 
 * Tuples are pushed to a sink, which prints them onto the system output.</p>
 * <p>
 * Note that the original job events stream processing is executed by the
 * JobRegistryService event {@linkplain 
 * JobRegistryService#addListener(quarks.function.BiConsumer) listeners}
 * invoker thread. 
 * It is considered good practice to isolate the event source from the rest 
 * of the graph, in order for the processing of tuples to be executed by a
 * different thread.</p>
 */
public class JobEventsSample {
    private final DirectProvider dp;

    public static void main(String[] args) throws Exception {
        
        JobEventsSample sample = new JobEventsSample();
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

        // Monitoring app
        sample.startMonitorApp();

        // Asynchronously start two applications
        executor.schedule(sample.runMonitoredApp("MonitoredApp1"), 300, TimeUnit.MILLISECONDS);
        executor.schedule(sample.runMonitoredApp("MonitoredApp2"), 3000, TimeUnit.MILLISECONDS);
    }

    JobEventsSample() throws Exception {
        this.dp = new DirectProvider();
        JobRegistry.createAndRegister(dp.getServices());
    }

    /**
     * Declares and submits a monitored application.
     * <p>
     * Note that inline sleeps are introduced to simulate the timing 
     * of a real-life application lifecycle.
     * 
     * @param name application name
     */
    void monitored(String name) throws InterruptedException, ExecutionException {
        // Declare topology
        Topology t = dp.newTopology(name);
        
        Random r = new Random();
        TStream<Double> d  = t.poll(() -> r.nextGaussian(), 100, TimeUnit.MILLISECONDS);
        d.sink(tuple -> System.out.print("."));

        // Submit job after 2 seconds
        Thread.sleep(2000);
        Future<Job> f = dp.submit(t);
        Job job = f.get();
        
        // Run for 5 seconds, then close job
        Thread.sleep(5000);
        job.stateChange(Job.Action.CLOSE);

        // Unregister job after 2 seconds
        Thread.sleep(2000);
        provider().getServices().getService(JobRegistryService.class).removeJob(job.getId());
    }

    /**
     * Monitoring application generates tuples on job registrations, removals, 
     * and on registered job updates.
     */
    Job startMonitorApp() throws InterruptedException, ExecutionException {
        Topology topology = dp.newTopology("MonitorApp");

        TStream<JsonObject> jobEvents = JobEvents.source(
                topology, 
                (evType, job) -> { return JobEventsSample.wrap(evType, job); });
        
        TStream<JsonObject> isolated = PlumbingStreams.isolate(jobEvents, true);
        isolated.sink(tuple -> {
                System.err.println(tuple.toString());
            });

        Future<Job> f = dp.submit(topology);
        return f.get();
    }
    
    /**
     * Creates a JsonObject wrapping a JobRegistryService event type and 
     * Job info.
     * 
     * @param evType the event type
     * @param job the job
     * @return the wrapped data
     */
    static JsonObject wrap(JobRegistryService.EventType evType, Job job) {
        JsonObject jo = new JsonObject();
        jo.addProperty("time", (Number)System.currentTimeMillis());
        jo.addProperty("event", evType.toString());
        jo.addProperty("jobId", job.getId());
        jo.addProperty("jobName", job.getName());
        jo.addProperty("jobState", job.getCurrentState().toString());
        jo.addProperty("jobNextState", job.getNextState().toString());
        return jo;
    }

    private DirectProvider provider() {
        return dp;
    }
    
    private Runnable runMonitoredApp(String name) {
        return new Runnable() {

            @Override
            public void run() {
                try {
                    monitored(name);
                } catch (Throwable t) {
                    throw new RuntimeException(t);
                }
            }
        };
    }
}

@@ -4947,18 +4947,21 @@
 nObject 
-jo
+value
  = new J
@@ -4973,34 +4973,37 @@
 ject();%0A        
-jo
+value
 .addProperty(%22ti
@@ -5044,34 +5044,37 @@
 lis());%0A        
-jo
+value
 .addProperty(%22ev
@@ -5099,34 +5099,78 @@
 ing());%0A        
+JsonObject obj = new JsonObject();%0A        ob
 j
-o
 .addProperty(%22jo
@@ -5171,12 +5171,9 @@
 ty(%22
-jobI
+i
 d%22, 
@@ -5186,34 +5186,35 @@
 tId());%0A        
-j
 o
+bj
 .addProperty(%22jo
@@ -5211,20 +5211,17 @@
 operty(%22
-jobN
+n
 ame%22, jo
@@ -5234,34 +5234,35 @@
 ame());%0A        
-j
 o
+bj
 .addProperty(%22jo
@@ -5263,12 +5263,9 @@
 ty(%22
-jobS
+s
 tate
@@ -5310,18 +5310,19 @@
         
-j
 o
+bj
 .addProp
@@ -5331,12 +5331,9 @@
 ty(%22
-jobN
+n
 extS
@@ -5383,17 +5383,172 @@
     
-return jo
+obj.addProperty(%22health%22, job.getHealth().toString());%0A        obj.addProperty(%22lastError%22, job.getLastError());%0A        value.add(%22job%22, obj);%0A        return value
 ;%0A  

Update JobEventsSample with job health