Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move to production #3885

Merged
merged 11 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion k8s/analytics/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ images:
celeryWorker: eu.gcr.io/airqo-250220/airqo-analytics-celery-worker
reportJob: eu.gcr.io/airqo-250220/airqo-analytics-report-job
devicesSummaryJob: eu.gcr.io/airqo-250220/airqo-analytics-devices-summary-job
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
api:
name: airqo-analytics-api
label: analytics-api
Expand Down
2 changes: 1 addition & 1 deletion k8s/auth-service/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-auth-api
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/device-registry/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-device-registry-api
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/device-registry/values-stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 2
image:
repository: eu.gcr.io/airqo-250220/airqo-stage-device-registry-api
tag: stage-a90d08d3-1732130453
tag: stage-ab9bd25f-1732443907
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-airqo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/airqo-exceedance-job
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-kcca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/kcca-exceedance-job
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/locate/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-locate-api
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/predict/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ images:
predictJob: eu.gcr.io/airqo-250220/airqo-predict-job
trainJob: eu.gcr.io/airqo-250220/airqo-train-job
predictPlaces: eu.gcr.io/airqo-250220/airqo-predict-places-air-quality
tag: prod-27d26f5a-1732130489
tag: prod-1cae31a2-1732444009
api:
name: airqo-prediction-api
label: prediction-api
Expand Down
24 changes: 24 additions & 0 deletions src/device-registry/models/Reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ const HealthTipsSchema = new Schema(
{ _id: false }
);

const categorySchema = new Schema(
{
area_name: { type: String },
category: { type: String },
highway: { type: String },
landuse: { type: String },
latitude: { type: Number },
longitude: { type: Number },
natural: { type: String },
search_radius: { type: Number },
waterway: { type: String },
tags: [
{
type: String,
trim: true,
},
],
},
{
_id: false,
}
);

const SiteDetailsSchema = new Schema(
{
_id: Schema.Types.ObjectId,
Expand All @@ -42,6 +65,7 @@ const SiteDetailsSchema = new Schema(
bearing_in_radians: Number,
description: String,
data_provider: String,
site_category: { type: categorySchema },
},
{ _id: false }
);
Expand Down
149 changes: 149 additions & 0 deletions src/device-registry/utils/scripts/api-response-transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Transforms API response data into the required format for the second API
* @param {Object} inputData - The response from the first API
* @returns {Object} Formatted data for the second API
*
*/
const axios = require("axios");
const isEmpty = require("is-empty");

function transformApiResponse(inputData) {
// Extract site-category data for direct mapping
const siteCategory = inputData.site["site-category"];

// Initialize the base structure with direct mappings
const result = {
area_name: siteCategory.area_name,
category: siteCategory.category,
highway: siteCategory.highway,
landuse: siteCategory.landuse,
latitude: siteCategory.latitude,
longitude: siteCategory.longitude,
natural: siteCategory.natural,
search_radius: siteCategory.search_radius,
waterway: siteCategory.waterway,
tags: [],
};

// Process OSM_info array to extract unique values
const osmInfoArray = inputData.site.OSM_info;
const uniqueValues = new Map();

// Helper function to clean and extract value from OSM info line
const extractValue = (line) => {
const [key, value] = line.trim().split(": ");
return { key: key.trim(), value: value.trim() };
};

// Process each entry in OSM_info
for (let i = 0; i < osmInfoArray.length; i++) {
const line = osmInfoArray[i];

// Skip the "Found OSM data:" lines
if (line.includes("Found OSM data:")) continue;

const { key, value } = extractValue(line);

// Skip empty or "unknown" values
if (!value || value === "unknown") continue;

// For Location, format it as a string
if (key === "Location") {
const locationStr = value.replace(/[()]/g, "");
uniqueValues.set(key, locationStr);
}
// For other keys, store if not already present or if current value is more specific
else if (
!uniqueValues.has(key) ||
(uniqueValues.get(key) === "unknown" && value !== "unknown")
) {
uniqueValues.set(key, value);
}
}

// Convert unique values to tags array
uniqueValues.forEach((value, key) => {
// Format the tag as "key: value"
const tag = `${key}: ${value}`;
result.tags.push(tag);
});

return result;
}

// Example usage:
const createRequestBody = (apiResponse) => {
try {
return transformApiResponse(apiResponse);
} catch (error) {
console.error("Error transforming API response:", error);
throw error;
}
};

// Make a GET request
const url = "http://localhost:3000/api/v2/devices/sites/summary";
const config = {
headers: {
Authorization: "",
},
};
axios
.get(url, config)
.then((response) => {
console.log("GET response site name" + ": ");
console.dir(response.data);
for (let i = 0; i < response.data.sites.length; i += 10) {
const batch = response.data.sites.slice(i, i + 10);
// Process batch of 10 items
batch.forEach(async (site) => {
// console.log("the site _id", site._id);
const url = `http://localhost:3000/api/v2/spatial/categorize_site?latitude=${site.latitude}&longitude=${site.longitude}`;

//***MAKE THE GET REQUEST */
axios
.get(url, config)
.then((response) => {
// console.log("PUT response:", response.data);
const apiResponse = response.data;
const requestBody = createRequestBody(apiResponse);
// console.log("requestBody", requestBody);
const updateURL = `http://localhost:3000/api/v2/devices/sites?id=${site._id}`;
const data = {
site_category: requestBody,
};

// Make a PUT request
axios
.put(updateURL, data, config)
.then((response) => {
console.log("PUT response:", response.data);
})
.catch((error) => {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
});
})
.catch((error) => {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
});
});
}
})
.catch((error) => {
console.error("GET error:", error);
});

module.exports = {
createRequestBody,
transformApiResponse,
};
20 changes: 17 additions & 3 deletions src/device-registry/utils/scripts/bulk-script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const axios = require("axios");
const isEmpty = require("is-empty");

function extractBeforeV2IncludingSlash(url) {
const v2Index = url.indexOf("v2/");

if (v2Index !== -1) {
// Found "v2/" in the string
return url.substring(0, v2Index + 3); // Include "v2/"
} else {
// "v2/" not found, return the original string
return url;
}
}

/**
* Update each device to have the default network of VISIBLE_DEVICES_ONLY
*/

// Make a GET request
const url = "http://localhost:3000/api/v1/devices";
const url = "http://localhost:3000/api/v2/devices/summary";
const config = {
headers: {
Authorization: "",
Expand All @@ -22,9 +34,11 @@ axios
// Process batch of 10 items
batch.forEach(async (device) => {
// console.log("the device _id", device._id);
const url = `http://localhost:3000/api/v1/devices?id=${device._id}`;
const url = `http://localhost:3000/api/v2/devices/soft?id=${device._id}`;
const new_api_code = extractBeforeV2IncludingSlash(device.api_code);

const data = {
network: "airqo",
api_code: new_api_code,
};

if (isEmpty(device.network)) {
Expand Down
Loading