{
   "templateGroup": {
      "uuid": "3432442",
      "name": "Sweet Factory",
      "description": "Configure Sweet Factory Rules",
      "ruleTemplates": [
         {
            "uuid": "identifying-continuous-production-decrease",
            "name": "Identify Continuous Production Decrease",
            "description": "Alert factory managers if rate of production continuously decreases for `X` time period",
            "type": "template",
            "instanceCount": "many",
            "script": "var timeRange = validateTimeRange(${timeRangeInput});\nvar username = getUsername('${email}');\n\n// Validates the input provided for time range\nfunction validateTimeRange(number) {\n\tif (!isNaN(number) && (number > 0)) {\n\t\treturn number;\n\t} else {\n\t\tthrow 'A positive number expected for time range';\n\t}\n}\n\n// Gets the username from provided email\nfunction getUsername(email) {\n\tif (email.match(/\\S+@\\S+/g)) {\n\t\tif (email.match(/\\S+@\\S+/g)[0] === email) {\n\t\t\treturn email.split('@')[0];\n\t\t}\n\t\tthrow 'Invalid email address provided';\n\t}\n\tthrow 'Invalid email address provided';\n}",
            "templates": [
               {
                  "type": "siddhiApp",
                  "content": "@App:name('SweetFactory-TrendAnalysis')\n\n@source(type='http', @map(type='json'))\ndefine stream SweetProductionStream (name string, amount double, factoryId int);\n\n@sink(type='log', @map(type='text', @payload(\"\"\"\nHi ${username},\nProduction at Factory {{factoryId}} has gone\nfrom {{initalamout}} to {{finalAmount}} in ${timeInterval} seconds!\"\"\")))\ndefine stream ContinousProdReductionStream (factoryId int, initaltime long, finalTime long, initalamout double, finalAmount double);\n\nfrom SweetProductionStream#window.timeBatch(${timeInterval} sec)\nselect factoryId, sum(amount) as amount, currentTimeMillis() as ts\ninsert into ProdRateStream;\n\npartition with ( factoryId of ProdRateStream )\nbegin\n  from every e1=ProdRateStream,\n  e2=ProdRateStream[ts - e1.ts <= ${timeRange} and e1.amount > amount ]*,\n  e3=ProdRateStream[ts - e1.ts >= ${timeRange} and e1.amount > amount ]\n  select e1.factoryId, e1.ts as initaltime, e3.ts as finalTime, e1.amount as initalamout, e3.amount as finalAmount\n  insert into ContinousProdReductionStream;\nend;"
               }
            ],
            "properties": {
               "timeInterval": {
                  "fieldName": "Time Interval (in seconds)",
                  "description": "Production amounts are considered per time interval",
                  "defaultValue": "6"
               },
               "timeRangeInput": {
                  "fieldName": "Time Range (in milliseconds)",
                  "description": "Time period in which, product amounts are analyzed for decrease",
                  "defaultValue": "5"
               },
               "email": {
                  "fieldName": "Manager Email ID",
                  "description": "Email address to show in greeting",
                  "defaultValue": "example@email.com"
               }
            }
         }
      ]
   }
}