Skip to content

Instantly share code, notes, and snippets.

View nishitpatel01's full-sized avatar
🎯
Focusing

Nishit nishitpatel01

🎯
Focusing
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nishitpatel01
nishitpatel01 / create-cf-pubsub.py
Last active October 12, 2022 19:03
Create cloud function and pubsub topics
REGION = "your-cloud-function-region"
TOPIC_NAME = "sensor-data-processing"
SOURCE_URI = "cloud-function-file-directory"
ENTRY_POINT = "detect_anomaly"
# create to pub/sub topic
gcloud pubsub topics create TOPIC_NAME
# Create cloud function
gcloud functions deploy sensor-data-processing \
@nishitpatel01
nishitpatel01 / main.py
Last active October 12, 2022 18:04
main python code for TSI cloud function
import base64
import google.oauth2.id_token
import google.auth.transport.requests
import json
import os
import csv
import requests
import datetime
from datetime import date
import time
@nishitpatel01
nishitpatel01 / requirement.txt
Created October 12, 2022 17:56
requirement file for cloud function
google-auth
requests==2.24.0
numpy==1.22.3
oauth2client
google-cloud-bigquery==3.3.1
@nishitpatel01
nishitpatel01 / evaluate_slice.py
Created October 12, 2022 17:54
evaluateSlice method sample payload
evaluate_slice_payload = {
"detectionTime": "2021-08-09T21:31:54+00:00",
"pinnedDimensions": [
{
"name": "measure",
"stringVal": "LTTH"
}
],
"timeseriesParams": {
"forecastHistory": "259200s",
delete_ds_endpt = f'https://timeseriesinsights.googleapis.com/v1/projects/{PROJECT_ID}/datasets/{dataset}'
res = query_ts(method="DELETE", endpoint=delete_ds_endpt, data=None, auth_token=token_array[0])
res
new_event_payload = {
{
"events": [
{
"groupId":"1324354349507023708",
"dimensions":[
{"name":"measure","stringVal":"LTTH"},
{"name":"Humidity","doubleVal":36},
{"name":"Light","doubleVal":99},
{"name":"h2_raw","doubleVal":1041},
@nishitpatel01
nishitpatel01 / query_tsi_datatset.py
Last active September 20, 2022 18:03
Query TSI dataset for anomaly or forecasting
#create json payload for API query
request_body = {
"detectionTime": "2021-06-27T15:00:00Z",
"slicingParams": {
"dimensionNames": ["measure"]
},
"timeseriesParams": {
"forecastHistory": "259200s",
"granularity": "1800s",
"metric": "temp",
@nishitpatel01
nishitpatel01 / list_tsi_datasets.py
Created August 7, 2022 17:18
List all TSI datasets
#list all API datasets
res = query_ts(method="GET", endpoint=ts_endpoint, data=None, auth_token=token)
res
@nishitpatel01
nishitpatel01 / create_tsi_dataset.py
Last active August 7, 2022 17:09
Timeseries Insights API - Create Dataset
# TODO - REPLACE WITH SA ACCOUNT KEY
KEY_FILE = '/<path-to-your-service-account-file>/file.json' #REPLACE THIS WITH PROJECT SPECIFIC SERVICE ACCOUNT KEY
ts_endpoint = f'https://timeseriesinsights.googleapis.com/v1/projects/{PROJECT_ID}/datasets'
# Helper function
def query_ts(method, endpoint, data, auth_token):
data = str(data)
headers = {'Content-type': 'application/json', "Authorization": f"Bearer {auth_token}"}
if method == "GET":