Javascript 正在使用google应用程序脚本中的google_服务状态页JSONP
我希望获得所有响应代码的服务状态,以及有多少服务正常运行 如果有10项服务,则有9项服务向上,1项服务向下。结果应以Javascript 正在使用google应用程序脚本中的google_服务状态页JSONP,javascript,google-apps-script,jsonp,Javascript,Google Apps Script,Jsonp,我希望获得所有响应代码的服务状态,以及有多少服务正常运行 如果有10项服务,则有9项服务向上,1项服务向下。结果应以 { 9/10 working, dash service DOWN } 或任何其他形式来获取状态 我正在尝试从中检索数据 我不知道如何从appscript处理JSONP 我试过使用 var result = UrlFetchApp.fetch('https://www.google.com/appsstatus/json/en' + uri, options).get
{
9/10 working,
dash service DOWN
}
或任何其他形式来获取状态
我正在尝试从中检索数据
我不知道如何从appscript处理JSONP
我试过使用
var result = UrlFetchApp.fetch('https://www.google.com/appsstatus/json/en' + uri, options).getContentText();
function dashboard.jsonp(data){
return data;
}
我已经在Apps脚本上搜索了与JSONP相关的任何资源,并找到了。然而,链接示例非常有限 推荐: 话虽如此,我已经尝试从中删除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;
}
/**
*@自定义代码google_服务状态页JSONP
*/
函数checkServices(){//要在应用程序脚本编辑器中运行的主函数
Logger.log(“停止服务的总数:+countDownServices(getStatusOfServices()).length);
log(“总服务:+countAllServices());
Logger.log(countAllServices()-countDownServices(getStatusOfServices()).length+“/”+countAllServices()+“正在工作\n关闭服务\n”+countDownServices(getStatusOfServices());
}
函数parseDashboardJSONP(){//从JSONP获取JSON值,以便在其他函数中轻松解析
var jsonData=”https://www.google.com/appsstatus/json/en"
var jsonFile=UrlFetchApp.fetch(jsonData.getContentText();
var text=jsonFile.replace(“dashboard.jsonp(“,”);
返回文本。替换(“);”,“”);
}
函数getServicesData(){//从JSONP链接获取所有当前服务及其ID
var containerData=[];
var parsethedata=JSON.parse(parseDashboardJSONP());//解析JSON值
试一试{
对于(var x=0;x=>0;x++){
var getServiceName=parsethedata.services[x].name;//从JSON值解析服务名称
var getServiceID=parsethedata.services[x].id;//从JSON值解析服务id
containerData[getServiceID]=getServiceName;//将所有解析数据放入一个数组中
}
}捕获(e){
//Logger.log(“行尾”);
}
返回集装箱数据;
}
函数getStatusOfServices(){//获取所有关闭的服务名称
var containerStatus=[];
var parsethedata=JSON.parse(parseDashboardJSONP());//解析JSON值
试一试{
对于(var x=0;x=>0;x++){
var getID=parsethedata.messages[x].service;//从JSON值的“messages”节点获取服务ID
containerStatus[x]=getServicesData()[getID];//从“服务”节点获取与“消息”节点的服务ID匹配的服务名称
}
}捕获(e){
//Logger.log(“行尾”);
}
返回集装箱状态;
}
函数countAllServices(){//Count JSON数据的“services”节点中的所有服务
var计数=0;
对于(var x=0;xb.toLowerCase()?1:-1;//对不“查看”字母大小写的函数进行排序
}
outArray.push(数组[0]);
for(数组中的变量n){
if(outArray[outArray.length-1].toLowerCase()!=array[n].toLowerCase()){
push(数组[n]);
}
}
返回数组;
}
运行上述代码后,将在执行日志上显示以下结果:
下面是代码parseDashboardJSONP()
使用的格式化JSON数据的概述。我使用了:
展开某些节点时:
欢迎使用StackOverFlow请借此机会学习如何使用,计数旧事件是错误的,我只查看服务的当前状态,不管怎样代码工作正常我已根据我的要求重新修改,做得好