Skip to content

Commit

Permalink
feat: new action to get data from updown
Browse files Browse the repository at this point in the history
  • Loading branch information
dan1M committed Oct 31, 2024
1 parent 59a20bd commit 82d7eea
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .github/workflows/scans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
airtable_api_key: ${{ secrets.AIRTABLE_API_KEY }}
jdma_api_key: ${{ secrets.JDMA_TOKEN }}
updown_api_key: ${{ secrets.UPDOWNIO_API_KEY }}
airtable_base_id: ${{ secrets.AIRTABLE_BASE_ID }}
airtable_procedures_table_name: ${{ secrets.AIRTABLE_TABLE_NAME }}
airtable_editions_table_name: ${{ secrets.AIRTABLE_EDITIONS_TABLE_NAME }}
Expand Down Expand Up @@ -75,6 +76,16 @@ jobs:
output: scans/jdma.json
output2: scans/jdma_3months.json

- name: UpDown
continue-on-error: true
uses: "DISIC/dashlord-observatoire/actions/updown@main"
with:
updownToken: ${{ matrix.sites.updownToken }}
updownApiKey: ${{ secrets.UPDOWNIO_API_KEY }}
startDate: ${{ matrix.sites.startDate }}
endDate: ${{ matrix.sites.endDate }}
output: scans/updown.json

# - name: eco-index
# timeout-minutes: 2
# continue-on-error: true
Expand Down
5 changes: 4 additions & 1 deletion actions/airtable-procedures-urls/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
jdma_api_key:
description: "Clé API JDMA"
required: true
updown_api_key:
description: "Clé API Read-only UpDown"
required: true
airtable_base_id:
description: "Identifiant de la base Airtable"
required: true
Expand Down Expand Up @@ -42,7 +45,7 @@ runs:
shell: bash
run: |
cd ${{ github.action_path }}
JSON_PROCEDURES=$(node index ${{ inputs.airtable_api_key }} ${{ inputs.jdma_api_key }} ${{ inputs.airtable_base_id }} ${{ inputs.airtable_procedures_table_name }} ${{ inputs.airtable_editions_table_name }} | tr '\n' ' ')
JSON_PROCEDURES=$(node index ${{ inputs.airtable_api_key }} ${{ inputs.jdma_api_key }} ${{ inputs.updown_api_key }} ${{ inputs.airtable_base_id }} ${{ inputs.airtable_procedures_table_name }} ${{ inputs.airtable_editions_table_name }} | tr '\n' ' ')
echo $JSON_PROCEDURES > ${{ inputs.output }}
echo "json=$JSON_PROCEDURES" >> $GITHUB_OUTPUT
STRING_URLS=$(cat ${{ inputs.output }} | python3 -c "import sys, json; data = json.load(sys.stdin); print(','.join(list(map(lambda d : d['link'], data))));")
Expand Down
11 changes: 11 additions & 0 deletions actions/airtable-procedures-urls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const repeatRequest = async (url, headers, filters, offset, records = []) => {
const getAirtableUrls = async (
airtable_api_key,
jdma_api_key,
updown_api_key,
base_id,
procedures_table_name,
editions_table_name
Expand Down Expand Up @@ -88,6 +89,14 @@ const getAirtableUrls = async (
}),
});

const updownUrls = await fetch('https://updown.io/api/checks', {
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-KEY": updown_api_key
},
}).then(response => response.json());

console.log(
JSON.stringify(
response
Expand All @@ -96,6 +105,7 @@ const getAirtableUrls = async (
link: record.fields[field_names.link]
? record.fields[field_names.link].replaceAll("\n", "").trim()
: "",
updownToken: updownUrls.find((item) => item.alias === record.fields[field_names.id])?.token || "not_found",
startDate,
endDate,
}))
Expand All @@ -110,6 +120,7 @@ module.exports = { getAirtableUrls };

if (require.main === module) {
getAirtableUrls(
process.argv[process.argv.length - 6],
process.argv[process.argv.length - 5],
process.argv[process.argv.length - 4],
process.argv[process.argv.length - 3],
Expand Down
35 changes: 35 additions & 0 deletions actions/updown/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "UpDown data retrieving"
description: "Récupération des données UpDown pour une démarche"

inputs:
updownToken:
description: "Token UpDown associé à la démarche"
required: true
updownApiKey:
description: "Clé API Read-only UpDown"
required: true
startDate:
description: "Get data from this date"
required: true
endDate:
description: "Get data to this date"
required: false
output:
description: "Path to output file. defaults to updown.json"
default: "updown.json"
required: true

runs:
using: "composite"
steps:
- name: Install
shell: bash
run: |
cd ${{ github.action_path }}
yarn
- name: UpDown retrieve data
shell: bash
run: |
cd ${{ github.action_path }}
node index ${{ inputs.updownToken }} ${{ inputs.updownApiKey }} ${{ inputs.startDate }} ${{ inputs.endDate }} > ${{ github.workspace }}/${{ inputs.output }}
cat ${{ github.workspace }}/${{ inputs.output }}
53 changes: 53 additions & 0 deletions actions/updown/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));

const encodeQueryParams = (params) => {
return Object.keys(params)
.map((key) => {
const value = params[key];
return (
encodeURIComponent(key) +
"=" +
encodeURIComponent(
typeof value === "object" ? JSON.stringify(value) : value
)
);
})
.join("&");
};

const getUpdownData = async (updownToken, updownApiKey, startDate, endDate) => {
const params = {
from: startDate,
to: endDate,
};

const metrics_url = `https://updown.io/api/checks/${updownToken}/metrics?${encodeQueryParams(params)}`;

fetch(metrics_url, {
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-KEY": updownApiKey
},
}).then((response) => {
response.json().then((json) => {
const data = json;

if (data) {
console.log(JSON.stringify(data));
}
});
});
};

module.exports = { getUpdownData };

if (require.main === module) {
getUpdownData(
process.argv[process.argv.length - 4],
process.argv[process.argv.length - 3],
process.argv[process.argv.length - 2],
process.argv[process.argv.length - 1]
);
}
15 changes: 15 additions & 0 deletions actions/updown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "jdma",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"node-fetch": "^3.3.0"
},
"devDependencies": {
"jest": "^29.0.2"
},
"scripts": {
"test": "jest"
}
}

0 comments on commit 82d7eea

Please sign in to comment.