{
  "templateGroup" : {
    "name" : "Stock Exchange",
    "uuid":"stock-exchange",
    "description" : "Domain for stock exchange analytics",
    "ruleTemplates" : [
      {
        "name" : "Stock Data Analysis" ,
        "uuid" : "stock-data-analysis",
        "type" : "template",
        "instanceCount" : "many",
        "script" :
          "
          /*
          Derives share volume margin deviation, between the user given min and max share volume margins
          */
          function deriveVolumeMarginDeviation(minShareVolumesMargin, maxShareVolumesMargin){
            return (maxShareVolumesMargin - minShareVolumesMargin);
          }
          // To test whether this unwanted variable causes any issues
          var marginDeviation = deriveVolumeMarginDeviation(${minShareVolumesMargin}, ${maxShareVolumesMargin});
          var mediumShareVolumesMargin = marginDeviation/2;
          ",
        "description" : "Analyzes data of company stocks, related to share volumes",
        "templates" : [
          { "type" : "siddhiApp",
            "content" :
            "@App:name('lowShareVolumesAnalysis')
            @Source(type = 'http', receiver.url='http://localhost:5005/inputStream',  basic.auth.enabled='false',
            @map(type='text'))
            define stream StockInputStream(symbol string, price float, volume long, company string);

            define stream LowShareVolumesStream(symbol string, price float, totalVolume long, company string);

            from StockInputStream[volume < ${minShareVolumesMargin}]
            select symbol, price, volume as totalVolume, company
            insert into LowShareVolumesStream;

            from LowShareVolumesStream#log('Low share volume : ')
            insert into ShareVolumesStream;"
          },
          { "type" : "siddhiApp",
            "content" :
            "@App:name('mediumShareVolumesAnalysis')
            @Source(type = 'http', receiver.url='${httpReceiverURL}',  basic.auth.enabled='false',
            @map(type='${sourceMapType}'))
            define stream StockInputStream(symbol string, price float, volume long, company string);

            define stream MediumShareVolumesStream(symbol string, price float, totalVolume long, company string);

            from StockInputStream[volume == ${mediumShareVolumesMargin}]
            select symbol, price, volume as totalVolume, company
            insert into MediumShareVolumesStream;

            from MediumShareVolumesStream#log('Medium share volume : ')
            insert into ShareVolumesStream;"
          }
        ],
        "properties" : {
          "httpReceiverURL" : {"fieldName":"Receiver URL","description" : "Enter the URL for the http receiver", "defaultValue" : "https://localhost:8005/stockInputStream"},
          "sourceMapType" : {"fieldName":"Mapping type for data source","description" : "Enter the format to which, data from the source are mapped", "defaultValue" : "xml", "options" : ["xml", "json"]},
          "sinkMapType" : {"fieldName":"Mapping type for data sink","description" : "Enter the format to which, data to the sink are mapped", "defaultValue" : "xml", "options" : ["xml", "json"]},
          "minShareVolumesMargin" : {"fieldName":"Minimum margin for volume shares", "description" : "Enter the threshold value below which, share volumes are considered as low volume shares", "defaultValue" : "10"},
          "maxShareVolumesMargin" : {"fieldName":"Maximum margin for volume shares", "description" : "Enter the threshold value above which, share volumes are considered as high volume shares", "defaultValue" : "10000"}
        }
      },
      {
        "name" : "Stock Exchange Input",
        "uuid" : "stock-exchange-input",
        "type" : "input",
        "instanceCount" : "many",
        "description" : "configured http source to recieve stock exchange updates",
        "script" : "",
        "templates" : [
          { "type" : "siddhiApp",
            "content" :
            "@App:name('appName1')
            @Source(type = 'http', receiver.url='${receiverUrl}',  basic.auth.enabled='false',
            @map(type='text'))
            define stream StockInputStream(symbol string, price float, volume long, name string);",
            "exposedStreamDefinition" :"define stream StockInputStream(symbol string, price float, volume long, name string);"
          }
        ],
        "properties" : {
          "receiverUrl" : {"fieldName":"Receiver URL","description" : "Enter the URL of the http receiver", "defaultValue" : "https://localhost:8005/stockInputStream"}
        }
      },
      {
        "name" : "Stock Exchange Output" ,
        "uuid" :  "stock-exchange-output",
        "type" : "output",
        "instanceCount" : "many",
        "description" : "configured http sink to output the filtered stock exchange data",
        "script" : "",
        "templates" : [
          { "type" : "siddhiApp",
            "content" :
             "@App:name('appName2')

             define stream StockOutputStream(companyName string, companySymbol string, sellingPrice float);

             from StockOutputStream#log('${logMessage} :')
             insert into outputStream;",
             "exposedStreamDefinition" :"define stream StockOutputStream(companyName string, companySymbol string, sellingPrice float);"
          }
        ],
        "properties" : {
          "logMessage" : {"fieldName":"Logging Message","description" : "Enter the message, that should be displayed while logging", "defaultValue" : "Filtered Stock data"}
        }
      }
    ]
  }
}
