<commit_before>/*
 * Copyright 2012 University of Chicago
 * 
 * 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.
 */

/*
 * Created on Jan 30, 2015
 */
package org.globus.cog.abstraction.coaster.service;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.globus.cog.abstraction.coaster.service.job.manager.AbstractBlockWorkerManager;
import org.globus.cog.abstraction.coaster.service.job.manager.AbstractSettings;
import org.globus.cog.abstraction.coaster.service.job.manager.Block;
import org.globus.cog.abstraction.coaster.service.job.manager.Cpu;
import org.globus.cog.abstraction.coaster.service.job.manager.Job;
import org.globus.cog.abstraction.coaster.service.job.manager.QueueProcessor;
import org.globus.cog.abstraction.coaster.service.job.manager.Time;
import org.globus.cog.util.http.AbstractHTTPServer;
import org.globus.cog.util.json.JSONEncoder;

public class SettingsServer extends AbstractHTTPServer {
    public static final Logger logger = Logger.getLogger(SettingsServer.class);
    
    private static final Set<String> valid = new HashSet<String>();
    static {
        valid.add("/get");
        valid.add("/set");
        valid.add("/list");
        valid.add("/getAll");
        valid.add("/wmInfo");
        valid.add("/index.html");
    }
    
    private AbstractSettings settings;
    private AbstractBlockWorkerManager wm;
    
    public SettingsServer(QueueProcessor queueProcessor, int port) {
        super("Settings", port, null);
        this.settings = queueProcessor.getSettings();
        if (queueProcessor instanceof AbstractBlockWorkerManager) {
            this.wm = (AbstractBlockWorkerManager) queueProcessor;
        }
    }
    
    protected String getAllowedOrigin() {
        return "*";
    }

    @Override
    protected boolean exists(String url) {
        return valid.contains(url);
    }

    @Override
    protected DataLink getDataLink(String url, Map<String, String> params) {
        if (url.equals("/get")) {
            return new DataLink(get(params.get("name")), "text/json");
        }
        else if (url.equals("/set")) {
            return new DataLink(set(params.get("name"), params.get("value")), "text/json");
        }
        else if (url.equals("/list")) {
            return new DataLink(list(), "text/json");
        }
        else if (url.equals("/getAll")) {
            return new DataLink(getAll(), "text/json");
        }
        else if (url.equals("/wmInfo")) {
            return new DataLink(getBlockInfo(), "text/json");
        }
        else {
            return new DataLink(makeIndexPage(), "text/html");
        } 
    }

    private ByteBuffer getBlockInfo() {
        JSONEncoder e = new JSONEncoder();
        e.beginMap();
        if (wm == null) {
            e.writeMapItem("supported", false);
        }
        else {
            e.writeMapItem("supported", true);
            Map<String, Object> wmData = wm.getMonitoringData();
            e.writeMapItem("time", Time.now().getMilliseconds());
            e.writeMapKey("wmdata");
            e.writeMap(wmData);
            List<Block> blocks = wm.getAllBlocks();
            e.writeMapKey("blocks");
            e.beginArray();
            for (Block b : blocks) {
                writeBlock(e, b);
            }
            e.endArray();
        }
        e.endMap();
        return ByteBuffer.wrap(e.toString().getBytes());
    }

    private void writeBlock(JSONEncoder e, Block b) {
        e.beginArrayItem();
        e.beginMap();
        e.writeMapItem("id", b.getId());
        e.writeMapItem("running", b.isRunning());
        e.writeMapItem("workerCount", b.getActiveWorkerCount());
        e.writeMapItem("jobsCompleted", b.getDoneJobCount());
        e.writeMapItem("walltime", b.getWalltime().getMilliseconds());
        writeTime(e, "creationTime", b.getCreationTime());
        writeTime(e, "startTime", b.getStartTime());
        writeTime(e, "terminationTime", b.getTerminationTime());
        writeTime(e, "endTime", b.getEndTime());
        e.writeMapKey("cpus");
        e.beginArray();
        Collection<Cpu> cpus = b.getCpusSnapshot();
        for (Cpu cpu : cpus) {
            writeCpu(e, cpu);
        }
        e.endArray();
        e.endMap();
        e.endArrayItem();
    }

    private void writeCpu(JSONEncoder e, Cpu cpu) {
        e.beginMap();
        e.writeMapItem("id", cpu.getId());
        e.writeMapItem("quality", cpu.getQuality());
        Job job = cpu.getRunning();
        if (job == null) {
            e.writeMapItem("job", null);
        }
        else {
            e.writeMapKey("job");
            writeJob(e, job);
        }
        e.endMap();
    }

    private void writeJob(JSONEncoder e, Job job) {
        e.beginMap();
        e.writeMapItem("id", job.getTask().getIdentity().toString());
        writeTime(e, "startTime", job.getStartTime());<commit_msg>Added block shutdown time (the time when a shutdown is requested) and also add node info to the block info<commit_after>/*
 * Copyright 2012 University of Chicago
 * 
 * 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.
 */

/*
 * Created on Jan 30, 2015
 */
package org.globus.cog.abstraction.coaster.service;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.globus.cog.abstraction.coaster.service.job.manager.AbstractBlockWorkerManager;
import org.globus.cog.abstraction.coaster.service.job.manager.AbstractSettings;
import org.globus.cog.abstraction.coaster.service.job.manager.Block;
import org.globus.cog.abstraction.coaster.service.job.manager.Cpu;
import org.globus.cog.abstraction.coaster.service.job.manager.Job;
import org.globus.cog.abstraction.coaster.service.job.manager.Node;
import org.globus.cog.abstraction.coaster.service.job.manager.QueueProcessor;
import org.globus.cog.abstraction.coaster.service.job.manager.Time;
import org.globus.cog.util.http.AbstractHTTPServer;
import org.globus.cog.util.json.JSONEncoder;

public class SettingsServer extends AbstractHTTPServer {
    public static final Logger logger = Logger.getLogger(SettingsServer.class);
    
    private static final Set<String> valid = new HashSet<String>();
    static {
        valid.add("/get");
        valid.add("/set");
        valid.add("/list");
        valid.add("/getAll");
        valid.add("/wmInfo");
        valid.add("/index.html");
    }
    
    private AbstractSettings settings;
    private AbstractBlockWorkerManager wm;
    
    public SettingsServer(QueueProcessor queueProcessor, int port) {
        super("Settings", port, null);
        this.settings = queueProcessor.getSettings();
        if (queueProcessor instanceof AbstractBlockWorkerManager) {
            this.wm = (AbstractBlockWorkerManager) queueProcessor;
        }
    }
    
    protected String getAllowedOrigin() {
        return "*";
    }

    @Override
    protected boolean exists(String url) {
        return valid.contains(url);
    }

    @Override
    protected DataLink getDataLink(String url, Map<String, String> params) {
        if (url.equals("/get")) {
            return new DataLink(get(params.get("name")), "text/json");
        }
        else if (url.equals("/set")) {
            return new DataLink(set(params.get("name"), params.get("value")), "text/json");
        }
        else if (url.equals("/list")) {
            return new DataLink(list(), "text/json");
        }
        else if (url.equals("/getAll")) {
            return new DataLink(getAll(), "text/json");
        }
        else if (url.equals("/wmInfo")) {
            return new DataLink(getBlockInfo(), "text/json");
        }
        else {
            return new DataLink(makeIndexPage(), "text/html");
        } 
    }

    private ByteBuffer getBlockInfo() {
        JSONEncoder e = new JSONEncoder();
        e.beginMap();
        if (wm == null) {
            e.writeMapItem("supported", false);
        }
        else {
            e.writeMapItem("supported", true);
            Map<String, Object> wmData = wm.getMonitoringData();
            e.writeMapItem("time", Time.now().getMilliseconds());
            e.writeMapKey("wmdata");
            e.writeMap(wmData);
            List<Block> blocks = wm.getAllBlocks();
            e.writeMapKey("blocks");
            e.beginArray();
            for (Block b : blocks) {
                writeBlock(e, b);
            }
            e.endArray();
        }
        e.endMap();
        return ByteBuffer.wrap(e.toString().getBytes());
    }

    private void writeBlock(JSONEncoder e, Block b) {
        e.beginArrayItem();
        e.beginMap();
        e.writeMapItem("id", b.getId());
        e.writeMapItem("running", b.isRunning());
        e.writeMapItem("workerCount", b.getActiveWorkerCount());
        e.writeMapItem("jobsCompleted", b.getDoneJobCount());
        e.writeMapItem("walltime", b.getWalltime().getMilliseconds());
        writeTime(e, "creationTime", b.getCreationTime());
        writeTime(e, "startTime", b.getStartTime());
        writeTime(e, "shutdownTime", b.getShutdownTime());
        writeTime(e, "terminationTime", b.getTerminationTime());
        writeTime(e, "endTime", b.getEndTime());
        e.writeMapKey("nodes");
        e.beginArray();
        Collection<Node> nodes = b.getNodes();
        for (Node node : nodes) {
            writeNode(e, node);
        }
        e.endArray();
        e.writeMapKey("cpus");
        e.beginArray();
        Collection<Cpu> cpus = b.getCpusSnapshot();
        for (Cpu cpu : cpus) {
            writeCpu(e, cpu);
        }
        e.endArray();
        e.endMap();
        e.endArrayItem();
    }

    private void writeNode(JSONEncoder e, Node node) {
        e.beginMap();
        e.writeMapItem("id", node.getId());
        writeTime(e, "startTime", node.getStartTime());
        e.endMap();
    }

    private void writeCpu(JSONEncoder e, Cpu cpu) {
        e.beginMap();
        e.writeMapItem("id", cpu.getId());
        e.writeMapItem("quality", cpu.getQuality());
        Job job = cpu.getRunning();
        if (job == null) {
            e.writeMapItem("job", null);
        }
        else {
            e.writeMapKey("job");
            writeJob(e, job);
        }
        e.endMap();
    }

    private void writeJob(JSONEncoder e, Job job) {
        e.beginMap();
        e.writeMapItem("id", job.getTask().getIdentity().toString());
        writeTime(e, "startTime", job.getStartTime());
{'fraction_non_alphanumeric': 0.1342069201781432, 'fraction_numerical': 0.0025693730729701952, 'mean_word_length': 2.5395574416489843, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941241', 'n_tokens_mistral': 3431, 'n_tokens_neox': 3390, 'n_words': 748}