在 Google Apps 脚本中使用 google_services 状态页面 JSONP

Consuming google_services status page JSONP in Google Apps Script

我正在寻找所有响应代码的服务状态以及有多少服务正在运行 运行 很好,

如果有 10 个服务,则有 9 个服务是 UP,一个 down.Result 应该是

的形式
{
  9/10 working,
  dash service DOWN
}

或任何其他形式获取状态 我正在尝试从

检索数据

https://www.google.com/appsstatus/json/en

我不知道如何从 appscript 处理 JSONP

我试过使用

var result = UrlFetchApp.fetch('https://www.google.com/appsstatus/json/en' + uri, options).getContentText();
    
 function dashboard.jsonp(data){

       return data;
 }

我在 Apps 脚本上搜索了与 JSONP 相关的任何资源,并找到了 Serving JSONP in web pages。但是,link 样本非常有限。

建议:

话虽如此,我已经尝试从 https://www.google.com/appsstatus/json/en 中抓取 JSONP,您可以查看下面的代码作为参考:

/**
 * @Custom code google_services status page JSONP
 */

function checkServices(){ //Main function to run in the Apps Script editor
  Logger.log("Total number of down services: "+countDownServices(getStatusOfServices()).length);
  Logger.log("Total services:" + countAllServices());
  Logger.log(countAllServices() - countDownServices(getStatusOfServices()).length +"/"+countAllServices()+" Working\nDown Service(s)\n"+countDownServices(getStatusOfServices()));

}

function parseDashboardJSONP(){ //Get the JSON values from JSONP to be easily parsed in the other functions
  var jsonData = "https://www.google.com/appsstatus/json/en"
  var jsonFile = UrlFetchApp.fetch(jsonData).getContentText();
  var text = jsonFile.replace("dashboard.jsonp(","");
  return text.replace(");","");
}

function getServicesData(){ //Get all current services and their IDs from the JSONP link
  var containerData = [];
  var parsethedata = JSON.parse(parseDashboardJSONP()); //Parse the JSON value
  try{
      for(var x = 0; x=>0; x++){
      var getServiceName = parsethedata.services[x].name; //Parse the service names from the JSON values
      var getServiceID = parsethedata.services[x].id; //Parse the service id from the JSON values
      containerData[getServiceID] = getServiceName; //Put all parse data into an array
      }
  }catch(e){
    //Logger.log("End of the line");
  }
  return containerData;
}

function getStatusOfServices(){ //Get all service names that were down
  var containerStatus = [];
  var parsethedata = JSON.parse(parseDashboardJSONP()); //Parse the JSON value
  try{
    for(var x = 0; x=>0; x++){
      var getID = parsethedata.messages[x].service; //Get the service ID from the "messages" node of the JSON value
      containerStatus[x] = getServicesData()[getID]; //Get the name of the service from "services" node that matches with the service ID from "messages" node
    }
  }catch(e){
    //Logger.log("End of the line");
  }
  return containerStatus;
}

function countAllServices(){ //Count all services from the "services" node of the JSON data
  var count = 0;
  for(var x=0; x<getServicesData().length;x++){
    if(getServicesData()[x]!=null){
      count++;
    }
  }
  return count;
}

function countDownServices(array) { //Remove the duplicate services that are on the "messages" node of the JSON data and count all of the down services unique names
  var outArray = [];
  array.sort(lowerCase);
  function lowerCase(a,b){
    return a.toLowerCase()>b.toLowerCase() ? 1 : -1;// sort function that does not "see" letter case
  }
  outArray.push(array[0]);
  for(var n in array){
    if(outArray[outArray.length-1].toLowerCase()!=array[n].toLowerCase()){
      outArray.push(array[n]);
    }
  }
  return outArray;
}

在 运行 上面的代码之后,这将是执行日志中的结果:

下面是代码 parseDashboardJSONP() 正在使用的格式化 JSON 数据的概述。我用过 JSON Formatter:

展开部分节点时: