Skip to content

Instantly share code, notes, and snippets.

View dmcgrath's full-sized avatar

Dan McGrath dmcgrath

View GitHub Profile
@dmcgrath
dmcgrath / gist:c3665c2c655c595f1e2ee527e2973737
Created May 7, 2019 21:52
Collection Group Queries - Query Sample Data
# Query example call center data
# Demonstrates both collection queries and collection group queries
# Data generated via https://gist.github.com/dmcgrath/0c404b5f2f17f1383e4f14631e579783
from google.cloud import firestore
db = firestore.Client(project="<your project id here>")
review_calls = db.collection_group(u'calls').where(u'sample',u'<',5).limit(5)
call_docs = review_calls.get()
for call_doc in call_docs:
@dmcgrath
dmcgrath / gist:0c404b5f2f17f1383e4f14631e579783
Last active May 25, 2019 12:31
Collection Group Queries - Generate Sample Data
# Create example call center data
# Each agent has their own subcollection of calls
# You can run this generator multiple times in parallel
from google.cloud import firestore
import random
# Default values create 5000 documents.
MAX_AGENTS = 100
MAX_CALLS = 50
MAX_RATING = 5
from google.cloud import firestore
from time import sleep
# Add a new document
db = firestore.Client()
doc_ref = db.collection(u'live_news').document(u'2019-01-31')
def pad_message(m):
pad_size = len(m)
return u'\u00A0'*pad_size + m + u'\u00A0'*(pad_size+1)
; function findItem() { ;
; var item ;
; while(item_not_found) { ;
// search
; } ;
; return item ;
; } ;
; var item = findItem() ;
var user = null;
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().useDeviceLanguage();
firebase.auth().signInWithPopup(provider).then(function(result) {
user = result.user;
// All the Firestore code with 'enablePersistence()' goes here
@dmcgrath
dmcgrath / gist:edaad606007d461c9850689e16c87763
Created December 15, 2017 03:19
Full initial Cloud Firestore page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ComputasBrann Notes</title>
<!-- update the version number as needed -->
<script defer src="/__/firebase/4.8.0/firebase.js"></script>
<!-- include only the Firebase features as you need -->
noteDocument.set({owner:0}, {merge: true});
noteDocument.onSnapshot(function(documentSnapshot){
noteData = documentSnapshot.data();
if(noteData.owner !== uuid ) {
noteElement.innerHTML = noteData.text || '';
}
});
noteElement = document.getElementById("note");
noteDocument = dbClient.collection("notes").doc(noteId);
noteElement.oninput = function(){
noteDocument.set({
owner: uuid,
text: this.innerHTML
});
};
@dmcgrath
dmcgrath / gist:a7796731c6a9553bf2dbf825b0460a20
Created December 15, 2017 03:05
Initialize Cloud Firestore
noteId = window.location.hash; // was h
project = "computasbrann-example";
// f = firebase
firebase.initializeApp({
authDomain:project+".firebaseapp.com",
projectId:project
});
var dbClient=null;
var uuid = uuidv4();
html{
background: #CCC;
font-family: Monaco, monospace;
font-size: 14px;
color: #444;
line-height: 1.4;
max-width: 60rem;
margin: 0 auto;
height: 100%;
}