diff --git a/.github/workflows/scans.yml b/.github/workflows/scans.yml index 82f761cc5f..15fe188908 100644 --- a/.github/workflows/scans.yml +++ b/.github/workflows/scans.yml @@ -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 }} @@ -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 diff --git a/actions/airtable-procedures-urls/action.yml b/actions/airtable-procedures-urls/action.yml index 793a77610e..4ed9a04dd8 100644 --- a/actions/airtable-procedures-urls/action.yml +++ b/actions/airtable-procedures-urls/action.yml @@ -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 @@ -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))));") diff --git a/actions/airtable-procedures-urls/index.js b/actions/airtable-procedures-urls/index.js index 060e9a80e7..6915a32005 100644 --- a/actions/airtable-procedures-urls/index.js +++ b/actions/airtable-procedures-urls/index.js @@ -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 @@ -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 @@ -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, })) @@ -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], diff --git a/actions/updown/action.yml b/actions/updown/action.yml new file mode 100644 index 0000000000..e8de914511 --- /dev/null +++ b/actions/updown/action.yml @@ -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 }} diff --git a/actions/updown/index.js b/actions/updown/index.js new file mode 100644 index 0000000000..e7aa25efc5 --- /dev/null +++ b/actions/updown/index.js @@ -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] + ); +} diff --git a/actions/updown/package.json b/actions/updown/package.json new file mode 100644 index 0000000000..1c1e71179c --- /dev/null +++ b/actions/updown/package.json @@ -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" + } +}