Skip to content

Commit

Permalink
Made endpoints dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Oct 17, 2024
1 parent 7e458fe commit 65f1828
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/test_carbondb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ jobs:
uses: ./
with:
task: start-measurement
company-uuid: "20b269ce-cd67-4788-8614-030eaf5a0b47"
project-uuid: "00000000-E63D-43E9-9714-A4DE9464F3D8"
machine-uuid: "00000001-E63D-43E9-9714-A4DE9464F3D8"
# type should be auto set to machine.ci
company: "My cool company"
project: "CI Testing"
machine: "ubuntu-latest"
tags: '["That-os-one","That-is-number-two"]'
api-endpoint: "http://api-478gtuzwbe72.green-coding.io:9142/v2/ci/measurement/add"

- name: Sleep step
run: sleep 2
Expand Down
32 changes: 22 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,29 @@ inputs:
description: 'Base URL of the Github API to send data to (including schema). Default is "github.api_url" (which resolves to https://api.github.com in free and many enterprise setups), but can be changed to your hostname if you have a custom hosted Github Enterprise'
default: ${{ github.api_url }}
required: false
company-uuid:
description: 'If you want to add data to the CarbonDB you can set this to your company UUID'
default: '-'
type:
description: 'If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a type for drill-down later. Defaults to "machine.ci"'
default: 'machine.ci'
required: false
project-uuid:
description: 'If you want to add data to the CarbonDB you can set this to your project UUID'
default: '-'
project:
description: 'If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a project for drill-down later. Defaults to "CI/CD"'
default: 'CI/CD'
required: false
machine-uuid:
description: 'If you want to add data to the CarbonDB you can set this to your machine UUID.'
default: '-'
machine:
description: 'If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set a machine for drill-down later. Defaults to ubuntu-latest'
default: 'ubuntu-latest'
required: false
tags:
description: 'If you want filter data in the GMT Dashboard or in CarbonDB you can here manually set tags for drill-down later. Please supply as JSON list of strings. Ex ["a", "b", "c"]. Defaults to "[]"'
default: '[]'
required: false
api-endpoint-add:
description: 'When using the GMT Dasboard and / or CarbonDB specify the endpoint URL to send to. Defaults to "https://api.green-coding.io/v2/ci/measurement/add"'
default: 'https://api.green-coding.io/v2/ci/measurement/add'
required: false
api-endpoint-badge-get:
description: 'When using the GMT Dasboard and / or CarbonDB specify the endpoint URL to get the badge from to. Defaults to "https://api.green-coding.io//v1/ci/badge/get'
default: 'https://api.green-coding.io/v1/ci/badge/get'
required: false

outputs:
Expand Down Expand Up @@ -90,7 +102,7 @@ runs:
workflow_id=${workflow_id:-"not_set"}
workflow_name=${workflow_name:-"not_set"}
${{github.action_path}}/scripts/setup.sh start_measurement "${{inputs.machine-power-data}}" "${{ github.run_id }}" "${{inputs.branch}}" "${{ github.repository }}" "$workflow_id" "$workflow_name" "${{ github.sha }}" "github" "${{ inputs.send-data }}" "${{ inputs.company-uuid }}" "${{ inputs.project-uuid }}" "${{ inputs.machine-uuid }}" "${{ inputs.calculate-co2 }}" "${{ inputs.json-output }}"
${{github.action_path}}/scripts/setup.sh start_measurement "${{inputs.machine-power-data}}" "${{ github.run_id }}" "${{inputs.branch}}" "${{ github.repository }}" "$workflow_id" "$workflow_name" "${{ github.sha }}" "github" "${{ inputs.send-data }}" "${{ inputs.type }}" "${{ inputs.project }}" "${{ inputs.machine }}" "${{ inputs.tags }}" "${{ inputs.calculate-co2 }}" "${{ inputs.json-output }}" "${{ inputs.api-endpoint-add }}" "${{ inputs.api-endpoint-badge-get }}"
- if: inputs.task == 'get-measurement'
id: run-lap-model
Expand Down
5 changes: 2 additions & 3 deletions scripts/display_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function display_results {
MEASUREMENT_RAN=${MEASUREMENT_RAN:-}
MEASUREMENT_COUNT=${MEASUREMENT_COUNT:-}
WORKFLOW_ID=${WORKFLOW_ID:-}
DASHBOARD_API_BASE=${DASHBOARD_API_BASE:-}
API_ENDPOINT_BADGE_GET=${API_ENDPOINT_BADGE_GET:-}
JSON_OUTPUT=${JSON_OUTPUT:-}
GITHUB_STEP_SUMMARY=${GITHUB_STEP_SUMMARY:-}

Expand Down Expand Up @@ -105,12 +105,11 @@ function display_results {
if [[ ${SEND_DATA} == 'true' && ${display_badge} == 'true' ]]; then
repo_enc=$( echo ${REPOSITORY} | jq -Rr @uri)
branch_enc=$( echo ${BRANCH} | jq -Rr @uri)
get_endpoint=$DASHBOARD_API_BASE"/v1/ci/badge/get"
metrics_url="https://metrics.green-coding.io"

echo "Badge for your README.md:" >> $output
echo ' ```' >> $output
echo "[![Energy Used](${get_endpoint}?repo=${repo_enc}&branch=${branch_enc}&workflow=$WORKFLOW_ID)](${metrics_url}/ci.html?repo=${repo_enc}&branch=${branch_enc}&workflow=$WORKFLOW_ID)" >> $output
echo "[![Energy Used](${API_ENDPOINT_BADGE_GET}?repo=${repo_enc}&branch=${branch_enc}&workflow=$WORKFLOW_ID)](${metrics_url}/ci.html?repo=${repo_enc}&branch=${branch_enc}&workflow=$WORKFLOW_ID)" >> $output
echo ' ```' >> $output

echo "See energy runs here:" >> $output
Expand Down
12 changes: 6 additions & 6 deletions scripts/make_measurement.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function make_measurement() {
MODEL_NAME=${MODEL_NAME:-}
MEASUREMENT_COUNT=${MEASUREMENT_COUNT:-}
WORKFLOW_ID=${WORKFLOW_ID:-}
DASHBOARD_API_BASE=${DASHBOARD_API_BASE:-}
API_ENDPOINT_ADD=${API_ENDPOINT_ADD:-}

# capture time
step_time=$(($(date +%s) - $(cat /tmp/eco-ci/timer-step.txt)))
Expand Down Expand Up @@ -91,12 +91,11 @@ function make_measurement() {
CO2EQ=$(echo "$CO2EQ_EMBODIED $CO2EQ_ENERGY" | awk '{printf "%.9f", $1 + $2}')
fi

add_endpoint=$DASHBOARD_API_BASE"/v1/ci/measurement/add"
value_mJ=$(echo "$step_energy 1000" | awk '{printf "%.9f", $1 * $2}' | cut -d '.' -f 1)
unit="mJ"
model_name_uri=$(echo $MODEL_NAME | jq -Rr @uri)

curl -X POST "${add_endpoint}" -H 'Content-Type: application/json' -d "{
curl -X POST "${API_ENDPOINT_ADD}" -H 'Content-Type: application/json' -d "{
\"energy_value\":\"${value_mJ}\",
\"energy_unit\":\"${unit}\",
\"cpu\":\"${model_name_uri}\",
Expand All @@ -111,9 +110,10 @@ function make_measurement() {
\"cpu_util_avg\":\"${cpu_avg}\",
\"duration\":\"${step_time}\",
\"workflow_name\":\"${WORKFLOW_NAME}\",
\"cb_company_uuid\":\"${CB_COMPANY_UUID}\",
\"cb_project_uuid\":\"${CB_PROJECT_UUID}\",
\"cb_machine_uuid\":\"${CB_MACHINE_UUID}\",
\"filter_type\":\"${CB_COMPANY_UUID}\",
\"filter_project\":\"${CB_PROJECT_UUID}\",
\"filter_machine\":\"${CB_MACHINE_UUID}\",
\"filter_tags\":\"${CB_MACHINE_UUID}\",
\"lat\":\"${LAT:-""}\",
\"lon\":\"${LON:-""}\",
\"city\":\"${CITY:-""}\",
Expand Down
28 changes: 9 additions & 19 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function start_measurement {
# call init_variables
add_var "MACHINE_POWER_DATA" "$1"
cpu_vars "$1"
add_var DASHBOARD_API_BASE "https://api.green-coding.io"

add_var RUN_ID "$2"
add_var BRANCH "$3"
Expand All @@ -30,23 +29,14 @@ function start_measurement {
add_var COMMIT_HASH "$7"
add_var SOURCE "$8"
add_var SEND_DATA "$9"
if [[ "${10}" == '-' ]]; then
add_var CB_COMPANY_UUID ""
else
add_var CB_COMPANY_UUID "${10}"
fi
if [[ "${11}" == '-' ]]; then
add_var CB_PROJECT_UUID ""
else
add_var CB_PROJECT_UUID "${11}"
fi
if [[ "${12}" == '-' ]]; then
add_var CB_MACHINE_UUID ""
else
add_var CB_MACHINE_UUID "${12}"
fi
add_var CALCULATE_CO2 "${13}"
add_var JSON_OUTPUT "${14}"
add_var FILTER_TYPE "${10}"
add_var FILTER_PROJECT "${11}"
add_var FILTER_MACHINE "${12}"
add_var FILTER_TAGS "${13}"
add_var CALCULATE_CO2 "${14}"
add_var JSON_OUTPUT "${15}"
add_var API_ENDPOINT_ADD ${16}
add_var API_ENDPOINT_BADGE_GET ${17}

touch /tmp/eco-ci/cpu-util-step.txt
touch /tmp/eco-ci/cpu-util-total.txt
Expand Down Expand Up @@ -92,7 +82,7 @@ function end_measurement {
option="$1"
case $option in
start_measurement)
start_measurement "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}"
start_measurement "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}"
;;
lap_measurement)
lap_measurement
Expand Down

0 comments on commit 65f1828

Please sign in to comment.