Implementing a fast sort with Microsoft Flow using Parallel Compute

This is #FlowNinja hack 112. Parallel Compute Sort.

I had written about how to sort with a variable (this is insertion sort) back in 2018 How to implement sort with Microsoft Flow-in-3-actions-within-a-loop

But in this previous method, the use of variable means we can’t run apply to each in parallel, so this method was always slow when array is large. Today, while chatting with Hiro - I had a sudden idea to revisit the pattern and see if I can make this quicker.

Photo by Amy Shamblen on Unsplash

Photo by Amy Shamblen on Unsplash


The problem

To create a sort, using parallel apply to each, and get a sorted array at the end.



In 2018, I didn’t have many of the patterns I need to make this new 2020 sort method. Firstly, to get results from parallel apply to each, we need Pieter’s Method (Compose apply to each inner output) to fan-in after parallel fan-in.

Second, we need to sort the actual array, and I came up with a pretty interesting method.



How this works

Consider array [ “d”, “e”, “c”, “b”, “a” ]
If we say for each character, filter array for items that are < than the current item, we’d get:

3:d, 4:e, 2:c, 1:b, 0:a

Then, if we consider, hey, we have 5 items

[0, 1, 2, 3, 4] => map to this dictionary, we’d get [ “a”, “b”, “c”, “d”, “e” ]



Side Story

I actually was thinking about this pattern while driving home, once it clicked I had to pull over the side, park the car, take out my laptop and write this Flow, and after I saw it work I drove home.



Steps

Some additional considerations


If the original array has duplicates

[ “a”, “b”, “b”, “c” ]
0:a, 1:b, 3:c

We’ll see 2 is missing. This is not end of the world, but when we do the final map of

[0,1,2,3] => [ “a”, “b”, null, “c” ]

Observation

I was worried that JSON('{ "0": "a", "0":"a" }') would give an error, but it seems like the duplicate key is ignored. This could be an interesting way to detect duplicates in the future by building a dictionary.





Talks and Workshop at Difinity 2020 conference Auckland

henry-mcintosh-xbo-Hz_Gs74-unsplash.jpg

Hello, Kia Ora Auckland.

I’m glad to be returning to New Zealand on February 17-20 for the Difinity Conference in Auckland. I try to tell people that I’m actually 1/3 kiwi - I grew up in Auckland in my youth. So it’s always good to see you all again.


I’m presenting three topics:

February 17 Workshop: Master your inner Flow

This is an updated - all day workshop covering beginner and advanced Power Automate (Microsoft Flow). With several enhanced lab materials that you can choose to work through in the afternoon:

  • Complex Business Approvals

  • Power Apps integration

  • Data collection of Excel

  • Automating Teams

February 18 Talk: Take control of Flow and Power Apps Governance

Talk: Take control of Flow and PowerApps Governance

This is a multi-faceted look at Power Platform governance - what’s available within the Admin platform, extended with Microsoft’s Center of Excellence kit, practical guides on how to deploy CoE, and ultimately, technical ideas and examples of how we can extend CoE to provide an all encompassing governance solution.

Ultimately, there is a techincal part to governance, and there is a much much bigger human part. We will cover the technical, but leave you with ideas you must form regarding where and how you will utilize the technical components to guide your organization into the best use of this fantastic platform.

February 19 Talk: How to make everything with Microsoft Flow in 2020

This is a talk that makes you look at Power Automate (Microsoft Flow) with a totally different angle, and realize that it is not merely workflows, some email alerts, or an rule to copy a file.

Power Automate is the key to connect and enrich your entire platform investment - within Power Platform, extended to Dynamics, Office 365 or Data platform, and beyond even the Microsoft ecosystem.

You can make and automate everything with Power Automate (Microsoft Flow).


Make sure to say hello

I don’t travel a lot, so when I do visit New Zealand - come say hello, ask me anything about Flow (Automate), Power Apps, Power Governance, Office 365, Microsoft Graph, Azure. I have twenty years of consulting experience to back me up and I know solutions to many of your problems.

I hope to see you in Auckland on February 17-20 at the Difinity Conference.

Difinity Conference website

Difinity 2020 agenda



Flow - Format Number advanced tips and tricks

I put together a video to celebrate the new update in Microsoft Flow (Power Automate) and Logic Apps - formatNumber()

The video explains 3 tips and 2 gotchas.

  • Tip 1: Use action in prod right now

  • Tip 2: Use ### and 000 patterns in formatNumber() expressions

  • Gotcha 2.1: May be don't use of $### - use $##0

  • Gotcha 2.2: May be don't use $ - use C or C2

  • Tip 3: Use formatNumber in collections with Select/Create HTML

If you want the clipboard paste of the Format Number action to use, use this.

{
    "id": "c1fa8a84-e2c0-4be0-823e-3a45-1e6834aa",
    "brandColor": "#a098e6",
    "connectionReferences": {},
    "icon": "https://psuxaustralia.azureedge.net/Content/Images/DesignerOperations/numberfunctions.png",
    "isTrigger": false,
    "operationName": "Format_number",
    "operationDefinition": {
        "type": "Expression",
        "kind": "FormatNumber",
        "inputs": {
            "number": 12345,
            "format": "C4"
        },
        "runAfter": {
            "Compose": [
                "Succeeded"
            ]
        }
    }
}

Power Platform Governance with Power Clarity

Clear the FOG - let there be Clarity

Clear the FOG - let there be Clarity

Some asked me recently how does my SaaS product Power Clarity work with Microsoft’s Center of Excellence kit.

We can look at this from two directions:

  • Turnkey Governance with Power Clarity (running without CoE)

  • Enhanced Governance with Power Clarity (running with CoE)

  • ALM with Power Clarity

Center of Excellence kit (CoE)

I’m a big fan of CoE built by Microsoft and the community. I love that Microsoft acknowledges there is a need for governance, and the community rises to build tools to help. CoE starter kit is a collection of resources that will be a template to create your own Center of Excellence kit.

Power Clarity is a turnkey solution - you grant your credentials, it starts scanning. You get dashboards, forms and reports and workflows. I want to stress that Power Clarity is an enhanced version of Flow Studio - something I’ve been building since 2018, so it was developed in parallel to CoE, there are similarities, which leads to how they can be used together.


Here’s a list of roadmap on our first year (2020).

Turnkey Governance with Power Clarity (running without CoE)

If you are a company using the power platform and has not developed or deployed CoE - then Power Clarity can be used as a turnkey starting point.

If you don’t know where to start, or how to build one, we will get you started quickly to help you uncover the platform.

If you don’t have a power governance solution, and need one in 15 minutes, we are your… service.

  • No install - we manage it and keep improving it.
    Power Clarity is a SaaS service so the constant improvements are baked into the subscription. (In my opinion, we aren’t very expensive either).

  • Add one or many concurrent scanning accounts

  • Use accounts that are not administrator accounts - Power Clarity can scan with or without admin permissions, or operating in mix mode between different environments. If you have environment admin - you can see everything. If you only has environment maker - you can still monitor assets that you own.

  • Power Clarity performs hourly scanning, because it’s an incremental scanner and very light on API access. We understand at high API throughput - both source and storage can get upset. Power Clarity incremental scanning of a tenant of 1000 (flows/apps) is typically less than 3 minutes.

  • Power Clarity does not require premium accounts

Enhanced Governance with Power Clarity

If you have CoE deployed and already has CDS entities - then Power Clarity can be used to greatly enhance that data collection exercise.

You no longer need to worry about ‘how the data is collected’, but can keep customizing your reporting and utilization of that data to drive your governance outcomes.
In fact, we give you more events, signals, and policy actions you can perform.
We want you to have clarity.

  • Writes to Azure Storage Table and CDS Entities (use Power Clarity to collect data instead of CoE’s admin sync flows)

    (Note: CDS write requires the scanning account to have Premium license for CDS access)

  • Deep API reads into Flows - runs, run actions, run errors and remediation

  • Recall run metadata beyond 30 days

  • When events happen - you can get an email report, or get a webhook call so you can handle it yourself. Power Clarity provides complete tenant flow error reporting without try/catch pattern. Since it’s inception, Power Clarity is a Flow monitoring service as well as a Power Platform governance service.

  • Advanced detection of cross tenant resources

  • Advanced detection of flows utilizing premium licenses

  • Advanced count of flows utilizing API rate limits

  • White and black list filtering of resources (domains, URLs) and connectors

  • Clash detection of flows and DLP policies

  • Clash detection of flows using deprecated actions

Application Lifecycle Management with Power Clarity

Because Power Clarity maintains offline granted access, it can perform advanced active actions to the Power Platform on your behalf, actions that don’t currently have an connector.

  • Flow Studio’s migrate feature (which lets us copy, switch connections and deploy flows), can be invoked from Clarity API call

  • Call Power Platform management cmdlets via Power Clarity API.

  • Continuous backup of flow definitions and Power Apps snapshots

  • Power Clarity provides an advanced GraphQL view of the entire data set available within its stores.

  • Read and reports on managed and unmanaged resources in Solutions

  • History of changes across the entity tracking

See it for yourself

Send me an email john.liu at flowstudio.app to get started.

Running Serverless Apollo GraphQL on AzureFunctions with cheap Azure Blob Table database(s)

leonardo-ramos-CJ4mbwSK3EY-unsplash.jpg

Merry Christmas holidays and happy new years.

This is a bit of a holiday reading, I wanted to start by give you the sense of what I’ve been tinkering as an experiment, and it looks increasingly like this is going into production before 2019 ends.



Plan

  • Have

    • I have a bunch of Azure storage Tables (about 70k rows of cheap database - at $1 / month storage) - they are spread out across several containers but for the basic purposes - I think of them as independent tables that lack proper relationships defined between them.
      I think that’s appropriate to describe most no-sql databases.

    • Azure Functions based CRUD REST APIs wraps around the Azure Blob Table - problem - no good caching or relationship mechanism. I wasn’t a big fan of keep rolling out CRUD REST endpoints, feeling that I should try find a different way.

  • Idea

    • Run a GraphQL server on Azure Functions

  • Learn:

    • If you want to run GraphQL on Azure Functions - there’s a dotnet way and there’s a NodeJS way

    • The dotnet version

      • https://www.tpeczek.com/2019/05/serverless-graphql-with-azure-functions.html

      • https://medium.com/@seekdavidlee/eklee-azure-functions-graphql-a-serverless-graphql-implementation-on-azure-484611c3680b

      • I’ve personally made a stack decision to stick to NodeJS/TypeScript/JavaScript, so I won’t be exploring the dotnet server.

    • The NodeJS version

      • A quick look around will send you to Apollo Server which has done 3 videos with Microsoft Azure/Channel9 on getting started, running on AppService, and running on Azure Functions (on consumption).

      • Part 1 https://www.youtube.com/watch?v=7R33hGFV4f0

      • Part 2 https://www.youtube.com/watch?v=Mt4bBpOdwyE

      • Part 3 https://www.youtube.com/watch?v=unUeFApHeT0

  • Write

    • Steps after Apollo + AzureFunctions 3-part videos.

    • Resolving GraphQL entities to Azure Blob Table

    • Resolving Relationships

    • Securing with Azure AD

    • Deploying to AzureFunctions

    • Reroute via Azure Function proxy

  • Future

    • Switch to Apollo RESTDataSource

    • Apollo Client with Angular and Observable bindings

    • Apollo Server with Redis cache



Learn

After the three videos, particularly the first and the third - to understand GraphQL server, and hosting it on AzureFunctions, you’ll end up with a working graphql server running on localhost showing you books array from memory. Getting here after watching three videos was extremely fast ~30minutes. Very surprising how much shortcut we took to get here this fast.

const typeDefs = gql`
    type Book {
        title: String
        author: String
    }
    type Query {
        books: [Book]
    }
`;

const resolvers = {
    Query: {
        books: () => books,
    },
};

let books = [
    {
        title: "A",
        author: "Mark B"
    },
    {
        title: "C",
        author: "John D"
    }
];

const playgroundSettings: any = {
    "schema.polling.enable": false
};
const server = new ApolloServer({ 
    typeDefs, 
    resolvers, 
    playground: {
        settings: playgroundSettings
    },
    debug: false 
});
export default server.createHandler();


A really fancy thing Apollo did was to put the graphql endpoint on POST, and run the playground test environment on GET. Pointing a browser to the same endpoint will show the playground.

Here is the playground running the sample books query. Debug works wonderfully running in localhost.

By default, the playground will do continuous polling on the endpoint. That’s going to keep the Function awake and incur a lot of costs. It might also keep bumping into any local debug breakpoints. So let’s turn that off. I also set debug to false.


Write - Resolving Azure Table

Next, we need to extend from here. My original AzureFunctions has REST endpoints that calls Azure storage Tables via the Azure.Storage SDK. So bring those in.

import { ApolloServer, gql } from "apollo-server-azure-functions"; 
import * as azure from "azure-storage";

And start to switch out the code

const typeDefs = gql`
    type Flow {
        RowKey: String
        name: String
        displayName: String
        critical: Boolean
        environmentName: String
        state: String
    }
    type Query {
        flows: [Flow]
    }
`;

const resolvers = {
    Query: {
        //books: () => books,
        flows: (parent,args,context) => azureTableQuery('flows'),
    }
};

const tableService = azure.createTableService();
const partition = "xxxx-partition-key";

const azureTableQuery = (table) => {
    let pRows = new Promise((resolve, reject)=>{

        let query = new azure.TableQuery().where('PartitionKey eq ?', partition);
        tableService.queryEntities(table, query, null, (error, result, response)=>{
            if(error){
                reject(error);
            }
            else {
                resolve(response.body['value']);
            }
        });
    });
    return pRows;
}

Notes: Azure.Storage does old style callbacks, so I’m wrapping them in a Promise that I can resolve. When I resolve - I want the raw JSON from the service, not the ‘tweaked’ JSON in result (it does weird things to your JSON entities, that’s a blog for another time).

A really nice thing about Apollo Server resolve function is that it knows how to handle promises naturally. So this tweak basically is me saying - hey, go fetch from Azure Storage Table from the ‘flows’ container.

To run this locally, we’ll need to store an Azure storage key in local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "MyStore": "DefaultEndpointsProtocol=https;AccountName=mystore;AccountKey=FOOxxx"
  }
}

We can reference this like so:

const tableService = azure.createTableService(process.env["MyStore"]);

Write - Resolving relationships

const azureTableRetrieve = (table, key) => {
    return new Promise((resolve, reject)=>{
        tableService.retrieveEntity(table, partition, key, null,
        (error, result, response)=>{
            if(error){
                reject(error);
            }
            else {
                let entity: any = response.body;
                resolve(entity);
            }
        });
    });
}

const typeDefs = gql`
    type Environment {
        RowKey: String
        name: String
        displayName: String
        isDefault: Boolean
    }
    type Flow {
        RowKey: String
        id: String
        name: String
        displayName: String
        critical: Boolean

        environmentName: String
        ref_environment: Environment
        state: String
    }
}`;

const resolvers = {
    Query: {
        environment: (parent,{name},context) => azureTableRetrieve('environments', name),
        flows: (parent,args,context) => azureTableQuery('flows'),
        flow: (parent,{name},context) => azureBlobTableRetrieve('flows', name)
    },
    Flow: {
        ref_environment: (parent,args,context) =>
          azureTableRetrieve('environments', parent.environmentName),
    }
};

Notes:

I call “ref_” because I’m terrible at making property names. Lots of fields like “environmentName” or “environment” are already taken. It got too hard picking new names, I decided to call all entity references ref_

Deploy to Azure Functions

Need to deploy two parts:

Friends don’t let friends right click to deploy unless another friend put it in the right click menu. In that case, please don’t make me choose my friends.

Also, right click and upload local settings file to send our MyStore setting into Azure functions.

That’s two right clicks and deployment done!

Connect Azure Function proxy

My existing API routes are already covered with Azure Functions proxy. So I added a new route that takes graphql over to this separate Azure Functions. I also bury the functionkey in the route so users can’t see it.

Future

There’s a bunch more ideas for the future.

  • Explore federated authentication and user level security.

  • Figure out why Azure Storage SDK next generation doesn’t do Tables

  • Switch from using SDK to Azure Storage REST and use Apollo’s excellent RESTDataSource implementation instead (it already implemented HTTP caching, pagination, top, etc)

  • implement mutations, especially e-tag handling.

  • implement server side Redis cache

  • Implement Apollo Client to fetch data for my existing Angular front-end

  • Implement client side cache and observable pattern

Let me know if you find this interesting and want to follow along. I wrote this mostly as documentation for what I’m doing and the incremental changes I’m making as I go.