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

Posted

技术标签:

【中文标题】在 Google Apps 脚本中使用 google_services 状态页面 JSONP【英文标题】:Consuming google_services status page JSONP in Google Apps Script 【发布时间】:2021-07-02 07:45:59 【问题描述】:

我希望获取所有响应代码的服务状态,并获取有多少服务已启动并运行良好,例如,

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


  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;
 

【问题讨论】:

欢迎来到 ***,请借此机会参加 tour 并学习如何使用 How to Ask、format code、minimal reproducible example 和 Learn More 【参考方案1】:

我在 Apps Script 上搜索了任何与 JSONP 相关的资源,并找到了 Serving JSONP in web pages。但是,链接示例非常有限。

推荐:

话虽如此,我已经尝试从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:

当部分节点展开时:

【讨论】:

计算旧事件是错误的,我只查看服务的当前状态,无论如何代码工作正常我已根据我的要求重新修改,干得好

以上是关于在 Google Apps 脚本中使用 google_services 状态页面 JSONP的主要内容,如果未能解决你的问题,请参考以下文章

Google 表单 - 使用 Google Apps 脚本在项目中添加自定义按钮“更多信息”

是否可以使用 Apps 脚本运行 Google 表格插件?

使用 Google Apps 脚本将文件上传到我的 google 驱动器(在 GOOGLE 中没有表格)

在 Google Apps 脚本中使用 Mandrill API

在 Google Apps 脚本中使用 Google People API 删除联系人返回 404 错误

使用Google Apps脚本删除Google表格中的行