问:
slack doesn't recognize github webhook payload format
I'm trying to create a slack app that uses incoming webhooks. I want my github repository to post to slack whenever the wiki is updated. I believe I've set up the webhook on github just fine, because I can see that it is attempting a delivery whenever I update the wiki. However, there's always the error, "no_text". I think this error means slack is expecting an item named "text," but the payload from github provides none. I verified this by trying two curl commands from the command prompt (I'm on windows):

curl -X POST -H "Content-type: application/json" --data "{\"text\":\"Hello, World!\"}" [MY_WEBHOOK_URL]

curl -X POST -H "Content-type: application/json" --data "{\"foobar\":\"Hello, World!\"}" [MY_WEBHOOK_URL]

This first one works as expected; the message "Hello, World!" gets posted to the slack channel I wanted, and I got back the "ok" message from curl. The second one did not work; the message was not posted, and I got back the message "no_text" from curl.

I can think of two possible solutions to this problem:

Change the format of the payload coming from github to include an item called "text" and other properties slack actually recognizes.

Get slack to recognize the format the payload is already in, perhaps by telling it to post the contents of a property other than "text."

I don't know how to accomplish either of these, or if they're even possible. Or perhaps there's another solution I haven't thought of?

Note: I already tried to use the github slack app, but couldn't figure out how to get it to post updates to the wiki. (See my other question if you'd like: slack github integration doesn't find wiki repository)

答:
I'm actually looking to do the same thing as you right now. Because the github and slack hooks are fundamentally different, you will need to have something in the middle to process the github webhooks into a Slack message to be posted via an incoming webhook.

You're going to need to do a couple different things (in no particular order):

Set up Github to send out hooks for the specific events you wish to be notified of.

Configure a middle man (I am currently using AWS SNS and Lambda)

Set up slack for the webhook.

For the github webhooks, you will need to leverage the more powerful github API to create the hook. You could do this with curl, but that's kind of a pain so I am using a JS script to take care of it. You will need to npm install github bluebird in the same directory before running something like this:

```<code>var GitHubApi = require("github");

var github = new GitHubApi({
    // optional
    debug: true,
    protocol: "https",
    host: "api.github.com", // should be api.github.com for GitHub
    pathPrefix: "", // for some GHEs; none for GitHub
    headers: {
        "user-agent": "ocelotsloth-conf" // GitHub is happy with a unique user agent
    },
    Promise: require('bluebird'),
    followRedirects: false, // default: true; there's currently an issue with non-get redirects, so allow ability to disable follow-redirects
    timeout: 5000
});

// user token
github.authenticate({
    type: "token",
    token: "GITHUB_TOKEN_HERE",
});

// https://mikedeboer.github.io/node-github/#api-repos-createHook
github.repos.createHook({
  owner: "ocelotsloth",
  repo: "lib-ical",
  name: "amazonsns",
  events: [
    //"commit_comment",
    //"create",
    //"delete",
    //"gollum",
    //"issue_comment",
    "issues"
    //"label",
    //"milestone",
    //"pull_request",
    //"pull_request_review",
    //"pull_request_review_comment",
    //"push",
    //"release"
  ],
  config: {
    aws_key: "AWS_KEY",
    aws_secret: "AWS_SECRET",
    sns_region: "us-east-1",
    sns_topic: "SNS_TOPIC_ARN"
  },

}, function(err, res) {
    console.log(JSON.stringify(res, null, '\t'));
});
</code>```

I remember following a blog post a while ago about setting up the SNS topic to work properly, but I don't remember exactly where it is anymore. Some googling should help. Also, you should be able to set up your own server for github to send these to and avoid having to set up AWS at all if you want to avoid the complexity. See https://mikedeboer.github.io/node-github/#api-repos-createHook for specific instructions on that method. You will need to use editHook after you create the hook, so either get it right the first time or use edit it. You just need to change the method call to editHook and add the id to the call as well.

Something important to see, you can define all of the different Events that you want github to send to you. For all of these, along with their formats, look at https://developer.github.com/v3/activity/events/types/.

To actually post these events to slack, I have a lambda script that currently looks like this (I literally just started writing this today, and haven't implemented more than just posting issue events, but it should do well as a starting point). For this script, you will need to npm install identify-github-event slack-webhook and have your incoming webhook set up as well.

```<code>var identifyGithubEvent = require('identify-github-event');
var SlackWebhook = require('slack-webhook')

// slack's link syntax
function link(url, txt) {
return "<" + url + "|" + txt + ">";
}

exports.handler = function(event, context) {
// 1. extract GitHub event from SNS message
var ghEvent = JSON.parse(event.Records[0].Sns.Message);
var eventType, eventName, numb;
console.log(ghEvent);

var ghEventType = identifyGithubEvent(ghEvent);

if (!ghEventType) {
  return;
}

var text = "Event!  " + ghEventType;

if (ghEventType === 'IssueCommentEvent') {
  var who = link(ghEvent.comment.user.html_url, ghEvent.comment.user.login);
  var what = link(ghEvent.issue.html_url, "Issue " + ghEvent.issue.number + ": \"" + ghEvent.issue.title + "\"");
  text = who + " commented on " + what;
}
else if (ghEventType === 'IssuesEvent') {
    var who = link(ghEvent.sender.html_url, ghEvent.sender.login);
    var action = ghEvent.action;
    var issueNumber = ghEvent.issue.number;
    var issueName = link(ghEvent.issue.html_url, ghEvent.issue.title + "\"");

    if (action === "opened" | action === "closed") {
        text = {
            attachments: [{
              "fallback": who + " opened Issue" + issueNumber + ": " + issueName,
              "color": "#36a64f",
              "pretext": "New issue " + action + ":",
              "author_name": ghEvent.sender.login,
              "author_link": ghEvent.sender.html_url,
              "thumb_url": ghEvent.sender.avatar_url,
              "title": "#" + issueNumber + ": " + ghEvent.issue.title,
              "title_link": ghEvent.issue.html_url,
              "text": ghEvent.issue.body,
              "fields": [
                {
                  "title": "Status",
                  "value": ghEvent.issue.state,
                  "short": true
                },
                {
                  "title": "Labels",
                  "value": ghEvent.issue.labels.map(label => label.name).join("\n"),
                  "short": true
                }
              ],
              "footer": "lib-ical",
              "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
              "mrkdwn_in": ["text"]
            }]
        };
    } else return;
}

  // 'commit_comment':
  // 'create':
  // 'delete':
  // 'issues':
  // 'label':
  // 'member':
  // 'milestone':
  // 'pull_request':
  // 'pull_request_review':
  // 'pull_request_review_comment':
  // 'push':
  // 'release':

var slack = new SlackWebhook('https://hooks.slack.com/services/SLACK-WEBHOOK-URL', {
  defaults: {
    username: 'GitHub -- user/project',
    channel: '#CHANNEL-NAME',
    icon_emoji: ':github:'
  }
})

slack.send(text);

};
</code>```

It's far from perfect, but it gives a really nice result:

For that specific example it's an issue close, but currently that script will also work on open. The script also does limited markdown processing, so if the issue contains any source blocks, it will be rendered properly inside of slack.

I hope this helps you with your approach, feel free to ask me to elaborate on anything else.

Body:
I'm actually looking to do the same thing as you right now. Because the github and slack hooks are fundamentally different, you will need to have something in the middle to process the github webhooks into a Slack message to be posted via an incoming webhook.

You're going to need to do a couple different things (in no particular order):

Set up Github to send out hooks for the specific events you wish to be notified of.

Configure a middle man (I am currently using AWS SNS and Lambda)

Set up slack for the webhook.

For the github webhooks, you will need to leverage the more powerful github API to create the hook. You could do this with curl, but that's kind of a pain so I am using a JS script to take care of it. You will need to npm install github bluebird in the same directory before running something like this:

var GitHubApi = require("github");

var github = new GitHubApi({
    // optional
    debug: true,
    protocol: "https",
    host: "api.github.com", // should be api.github.com for GitHub
    pathPrefix: "", // for some GHEs; none for GitHub
    headers: {
        "user-agent": "ocelotsloth-conf" // GitHub is happy with a unique user agent
    },
    Promise: require('bluebird'),
    followRedirects: false, // default: true; there's currently an issue with non-get redirects, so allow ability to disable follow-redirects
    timeout: 5000
});

// user token
github.authenticate({
    type: "token",
    token: "GITHUB_TOKEN_HERE",
});

// https://mikedeboer.github.io/node-github/#api-repos-createHook
github.repos.createHook({
  owner: "ocelotsloth",
  repo: "lib-ical",
  name: "amazonsns",
  events: [
    //"commit_comment",
    //"create",
    //"delete",
    //"gollum",
    //"issue_comment",
    "issues"
    //"label",
    //"milestone",
    //"pull_request",
    //"pull_request_review",
    //"pull_request_review_comment",
    //"push",
    //"release"
  ],
  config: {
    aws_key: "AWS_KEY",
    aws_secret: "AWS_SECRET",
    sns_region: "us-east-1",
    sns_topic: "SNS_TOPIC_ARN"
  },

}, function(err, res) {
    console.log(JSON.stringify(res, null, '\t'));
});

I remember following a blog post a while ago about setting up the SNS topic to work properly, but I don't remember exactly where it is anymore. Some googling should help. Also, you should be able to set up your own server for github to send these to and avoid having to set up AWS at all if you want to avoid the complexity. See https://mikedeboer.github.io/node-github/#api-repos-createHook for specific instructions on that method. You will need to use editHook after you create the hook, so either get it right the first time or use edit it. You just need to change the method call to editHook and add the id to the call as well.

Something important to see, you can define all of the different Events that you want github to send to you. For all of these, along with their formats, look at https://developer.github.com/v3/activity/events/types/.

To actually post these events to slack, I have a lambda script that currently looks like this (I literally just started writing this today, and haven't implemented more than just posting issue events, but it should do well as a starting point). For this script, you will need to npm install identify-github-event slack-webhook and have your incoming webhook set up as well.

var identifyGithubEvent = require('identify-github-event');
var SlackWebhook = require('slack-webhook')

// slack's link syntax
function link(url, txt) {
return "<" + url + "|" + txt + ">";
}

exports.handler = function(event, context) {
// 1. extract GitHub event from SNS message
var ghEvent = JSON.parse(event.Records[0].Sns.Message);
var eventType, eventName, numb;
console.log(ghEvent);

var ghEventType = identifyGithubEvent(ghEvent);

if (!ghEventType) {
  return;
}

var text = "Event!  " + ghEventType;

if (ghEventType === 'IssueCommentEvent') {
  var who = link(ghEvent.comment.user.html_url, ghEvent.comment.user.login);
  var what = link(ghEvent.issue.html_url, "Issue " + ghEvent.issue.number + ": \"" + ghEvent.issue.title + "\"");
  text = who + " commented on " + what;
}
else if (ghEventType === 'IssuesEvent') {
    var who = link(ghEvent.sender.html_url, ghEvent.sender.login);
    var action = ghEvent.action;
    var issueNumber = ghEvent.issue.number;
    var issueName = link(ghEvent.issue.html_url, ghEvent.issue.title + "\"");

    if (action === "opened" | action === "closed") {
        text = {
            attachments: [{
              "fallback": who + " opened Issue" + issueNumber + ": " + issueName,
              "color": "#36a64f",
              "pretext": "New issue " + action + ":",
              "author_name": ghEvent.sender.login,
              "author_link": ghEvent.sender.html_url,
              "thumb_url": ghEvent.sender.avatar_url,
              "title": "#" + issueNumber + ": " + ghEvent.issue.title,
              "title_link": ghEvent.issue.html_url,
              "text": ghEvent.issue.body,
              "fields": [
                {
                  "title": "Status",
                  "value": ghEvent.issue.state,
                  "short": true
                },
                {
                  "title": "Labels",
                  "value": ghEvent.issue.labels.map(label => label.name).join("\n"),
                  "short": true
                }
              ],
              "footer": "lib-ical",
              "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
              "mrkdwn_in": ["text"]
            }]
        };
    } else return;
}

  // 'commit_comment':
  // 'create':
  // 'delete':
  // 'issues':
  // 'label':
  // 'member':
  // 'milestone':
  // 'pull_request':
  // 'pull_request_review':
  // 'pull_request_review_comment':
  // 'push':
  // 'release':

var slack = new SlackWebhook('https://hooks.slack.com/services/SLACK-WEBHOOK-URL', {
  defaults: {
    username: 'GitHub -- user/project',
    channel: '#CHANNEL-NAME',
    icon_emoji: ':github:'
  }
})

slack.send(text);

};

It's far from perfect, but it gives a really nice result:

For that specific example it's an issue close, but currently that script will also work on open. The script also does limited markdown processing, so if the issue contains any source blocks, it will be rendered properly inside of slack.

I hope this helps you with your approach, feel free to ask me to elaborate on anything else.


来源: stackexchange

问:
Using R + For Loops to Create Variables
Supposed that I have the following data:

```<code>a1 <- 1
a2 <- 2
a3 <- 3
</code>```

In my actual data, I have way more variable and what I am aiming to do is create interaction variables between them and then put them into one big dataframe like this:

```<code>a1a1 <- a1*a1
a1a2 <- a1*a2
a1a3 <- a1*a3

a2a2 <- a2*a1
a2a3 <- a2*a2

a3a3 <- a3*a3

mydf <- data.frame(a1a1,a1a2,...)
</code>```

I am certain I could use a forloop to accomplish what I want but I am horribly new to R and coding methodology altogether. My intuition tells me to do something along the lines of this:

```<code> n <- 3
 j <- 3

 for( i in 1:n) {
    k <- 1
    for( j in 1:k) {

 aiaj <- ai*aj
 #Not sure how to put into a data.frame for each variable
 k <- k + 1
 }
 }
</code>```

The way the loop is constructed, I believe it would make the variables a1a1,a1a2,a1a3 on the first iteration, on the second iteration it would make a2a2,a2a3, and then on the third iteration a3a3. I've tried this, and it doesn't work so I am hoping I can get some suggestions. Additionally, I have no idea how I would keep adding to the data.frame

答:
How about you try this:

suppose this is your data frame

```<code>q1 <- data.frame(a = sample(1:20, 4), b = sample(1:20, 4),c=sample(1:20, 4))
</code>```

you can use lapply to interact the columns with this function. There may be efficient methods but I hope this solves your problem.

```<code>library(dplyr)

 mn <- lapply(1:3, function(j) data.frame(lapply(1:3, function(i)
         q1[,i]*q1[,j])))%>%bind_cols()
</code>```

Column names may not be pretty but you can change them at any time using colnames

ContentLicense: CC BY-SA 3.0

Title: Using R + For Loops to Create Variables

Body:
Supposed that I have the following data:

a1 <- 1
a2 <- 2
a3 <- 3

In my actual data, I have way more variable and what I am aiming to do is create interaction variables between them and then put them into one big dataframe like this:

a1a1 <- a1*a1
a1a2 <- a1*a2
a1a3 <- a1*a3

a2a2 <- a2*a1
a2a3 <- a2*a2

a3a3 <- a3*a3

mydf <- data.frame(a1a1,a1a2,...)

I am certain I could use a forloop to accomplish what I want but I am horribly new to R and coding methodology altogether. My intuition tells me to do something along the lines of this:

 n <- 3
 j <- 3

 for( i in 1:n) {
    k <- 1
    for( j in 1:k) {

 aiaj <- ai*aj
 #Not sure how to put into a data.frame for each variable
 k <- k + 1
 }
 }

The way the loop is constructed, I believe it would make the variables a1a1,a1a2,a1a3 on the first iteration, on the second iteration it would make a2a2,a2a3, and then on the third iteration a3a3. I've tried this, and it doesn't work so I am hoping I can get some suggestions. Additionally, I have no idea how I would keep adding to the data.frame

Body:
How about you try this:

suppose this is your data frame

q1 <- data.frame(a = sample(1:20, 4), b = sample(1:20, 4),c=sample(1:20, 4))

you can use lapply to interact the columns with this function. There may be efficient methods but I hope this solves your problem.

library(dplyr)

 mn <- lapply(1:3, function(j) data.frame(lapply(1:3, function(i)
         q1[,i]*q1[,j])))%>%bind_cols()

Column names may not be pretty but you can change them at any time using colnames


问:
How to ignore all from an observable if the other observable has data in RxJS?
I have two observables, one receives data from the browser localstorage and the other is from the database through WebAPI.

I want to subscribe to them so if the observable from the localstorage has data, don't initiate the one to get the data from the database.

If the observable from the localstorage does not have any
data, invoke the ajax call to get the data from the WebAPI.

In the following example, I should get only 20, 40, 60, 80, 100 because the first observable has data. The second observable did not run because the first observable started emitting data.

答:
You can use skipWhile and check for the data and return true or false.</p>

```<code>observableObject.skipWhile((data)=> {
      if(data){
          return false;
      }
});
</code>```

Body:
You can use skipWhile and check for the data and return true or false.

observableObject.skipWhile((data)=> {
      if(data){
          return false;
      }
});


问:
Is an alamofire request to a HTTPS domain any different from that to HTTP?
I just added SSL to my backend framework (Django REST API) and I want my iOS application to talk to it. Do I have to do anything differently on the iOS side of my project? How do I tell Alamofire to encrypt the data its sending? Or Does it happen automatically?</p>

答: Just update URLs inside your app to use "https" and you are done.</p>


问:
How to select text value from a variable div id tag?
```<code><div id="Modal<%= index %>">
     <div>
      <input type="text" id="textvalue">
    </div>
</div>
</code>```

In the above code I need to get value of text field but the problem coming is the div id is variable according to index i.e it may be 'Modal0' or 'Modal1' or 'Modal"n"' depending on value of index. So, if i write $(#textvalue).val() then it is always giving the text entered in id 'Modal0'. What will be solution for this? And the big issue is that I can not  use 'Modal0' or 'Modal1' ... id because that is generated dynamically.

答:
Im not sure if this is what you're trying to do, but if the clicking of buttons will only affect the ID of the same container div of the input.textvalue element then this will do the trick:

```<code>$('div[id^=Modal] #textvalue').val());
</code>```

DEMO:

$(function(){
  $('a').on('click', function(e) {
    const ID = $(this).attr('href')
    const counterID = ID.substring(ID.length-1);
    const targetID = $('div[id^="Modal"]').attr('id');
    $('div[id^="Modal"]').attr('id', 'Modal'+(counterID-1));
    console.log('DIV ID:', 'Modal'+(counterID-1));
  });

  $('button').on('click', function(){
   console.log('DIV ID: ' + $('div[id^=Modal]').attr('id'), ' ,TEXT VAL: ' + $('div[id^=Modal] #textvalue').val());
  });
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Modal 1
Modal 2
Modal 3
<div id="Modal">
  <div>
    <input type="text" id="textvalue">
  </div>
  <button>submit</button>
</div></code>```

Body:
<div id="Modal<%= index %>">

      <input type="text" id="textvalue">

In the above code I need to get value of text field but the problem coming is the div id is variable according to index i.e it may be 'Modal0' or 'Modal1' or 'Modal"n"' depending on value of index. So, if i write $(#textvalue).val() then it is always giving the text entered in id 'Modal0'. What will be solution for this? And the big issue is that I can not  use 'Modal0' or 'Modal1' ... id because that is generated dynamically.

Body:
Im not sure if this is what you're trying to do, but if the clicking of buttons will only affect the ID of the same container div of the input.textvalue element then this will do the trick:

$('div[id^=Modal] #textvalue').val());

DEMO:

$(function(){
  $('a').on('click', function(e) {
    const ID = $(this).attr('href')
    const counterID = ID.substring(ID.length-1);
    const targetID = $('div[id^="Modal"]').attr('id');
    $('div[id^="Modal"]').attr('id', 'Modal'+(counterID-1));
    console.log('DIV ID:', 'Modal'+(counterID-1));
  });

  $('button').on('click', function(){
   console.log('DIV ID: ' + $('div[id^=Modal]').attr('id'), ' ,TEXT VAL: ' + $('div[id^=Modal] #textvalue').val());
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#Modal1">Modal 1</a>
<a href="#Modal2">Modal 2</a>
<a href="#Modal3">Modal 3</a>
<div id="Modal">
  <div>
    <input type="text" id="textvalue">
  </div>
  <button>submit</button>
</div>


问:
different time in API response at first call and then call again?
I created an API in Laravel.
When I first call the API in Postman it shows 504ms, then I call it again and it shows 235ms in Postman. If I call it once again it returns about 220ms to 280ms max?

If I stop  making calls to the api for few minutes (about 10-15 min) when I call it again it takes around 500ms for the first time and then it shows  near 200.

Is it cached data or persistence call or Zend opCache?

Can anybody help me find out why or what is this?

答: Laravel supports popular caching backends like Memcached and Redis out of the box. But if you are not using their facades inside your application you shouldnt have any of this issues. Your problem is not the laravel but your server/environment configuration.</p>


问:
Mysql - select several rows with same value
I've got a table called 'data', and some columns:

user,unit,price,discount,description.

```<code> -----   -----  ------  ---------   -----------
| user | unit |  price | discount | description |
 -----   -----  ------  ---------   -----------
| test | unit |  100   |    50    |     des     |
-----   -----  -------   --------   -----------
| test2| unit2 |  200  |    20    |     des     |
 -----   -----  -----    --------   -----------
</code>```

```<code><?php
 if($_SERVER['REQUEST_METHOD']=='GET'){
 $id  = $_GET['id'];
 require_once('dbConnect.php');
 $sql = "SELECT * FROM data WHERE description='".$id."'";
 $r = mysqli_query($con,$sql);
 $res = mysqli_fetch_array($r);
 $result = array();
 array_push($result,array(
     "user"=>$res['user'],
     "unit"=>$res['unit'],
     "price"=>$res['price'],
     "discount"=>$res['discount']
 )
 );
 echo json_encode(array("result"=>$result));
 mysqli_close($con);
 }
</code>```

From this code, I get:

```<code>{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]}
</code>```

so it's just the first row. I want to get both of them like this:

```<code>{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]}
{"result2":[{"user":"test2","unit":"unit2","price":"200","discount":"20"}]}
</code>```

so there will be 2 arrays.

答:
first of all your query is open to sql injection you should use mysqli preprared statement

so then your code would look something like this

Edit: my original answer was incomplete as i didn't test it below is my corrected answer

```<code><?php
 if($_SERVER['REQUEST_METHOD']=='GET'){
 $id  = $_GET['id'];
 require_once('dbConnect.php');
 $sql = "SELECT * FROM data WHERE description=?";
 $stmt = mysqli_prepare($con,$sql);
 $stmt->bind_param("s", $id);
 $stmt->execute(); //originally I combined this and next line, it was incorrect
 if ($result = $stmt->get_result()){
    //originally I used wrong method below
    while($row = $result->fetch_assoc()) {
            $myArray[] = $row;
    }
    //uncomment below if you're sending response as json responese
    //header('Content-Type: application/json');
    echo json_encode($myArray);
}

$result->close();
</code>```

Body:
I've got a table called 'data', and some columns:

user,unit,price,discount,description.

 -----   -----  ------  ---------   -----------
| user | unit |  price | discount | description |
 -----   -----  ------  ---------   -----------
| test | unit |  100   |    50    |     des     |
-----   -----  -------   --------   -----------
| test2| unit2 |  200  |    20    |     des     |
 -----   -----  -----    --------   -----------

<?php
 if($_SERVER['REQUEST_METHOD']=='GET'){
 $id  = $_GET['id'];
 require_once('dbConnect.php');
 $sql = "SELECT * FROM data WHERE description='".$id."'";
 $r = mysqli_query($con,$sql);
 $res = mysqli_fetch_array($r);
 $result = array();
 array_push($result,array(
     "user"=>$res['user'],
     "unit"=>$res['unit'],
     "price"=>$res['price'],
     "discount"=>$res['discount']
 )
 );
 echo json_encode(array("result"=>$result));
 mysqli_close($con);
 }

From this code, I get:

{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]}

so it's just the first row. I want to get both of them like this:

{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]}
{"result2":[{"user":"test2","unit":"unit2","price":"200","discount":"20"}]}

so there will be 2 arrays.

Body:
first of all your query is open to sql injection you should use mysqli preprared statement

so then your code would look something like this

Edit: my original answer was incomplete as i didn't test it below is my corrected answer

<?php
 if($_SERVER['REQUEST_METHOD']=='GET'){
 $id  = $_GET['id'];
 require_once('dbConnect.php');
 $sql = "SELECT * FROM data WHERE description=?";
 $stmt = mysqli_prepare($con,$sql);
 $stmt->bind_param("s", $id);
 $stmt->execute(); //originally I combined this and next line, it was incorrect
 if ($result = $stmt->get_result()){
    //originally I used wrong method below
    while($row = $result->fetch_assoc()) {
            $myArray[] = $row;
    }
    //uncomment below if you're sending response as json responese
    //header('Content-Type: application/json');
    echo json_encode($myArray);
}

$result->close();


问:
How mod rewrite can impact on PHP, JS or AS3 variables and paths
Mod rewrite changes the look of URLs  but does it matter for server-side scripts (PHP)?

Does it change the $_GET[] in PHP? Will sth like:

if ( strpos($pulledId,':') > 0 ) {
$pulledId = substr($pulledId,0,strpos($pulledId,':'));
}

work, when there is no "id=" in diplayed URL?
How about JavaScript?
How about Flash? Can mod rewrite be a problem when loading external files from server ?

AS3 or JS are client-side scripts. So I assume they get URL as it is in a browser,
but PHP, JAVA are server-side, so they should get URL which haven't been processed by mod rewrite.

答:
mod_rewrite can perform two types of redirections:

Internal redirections: RewriteRule ^catalogue/product-(\d+)/?$ products.php?product_id=$1 [L]

HTTP redirections: RewriteRule ^foo?$ http://example.com/bar [L]

Additionally, it can strip off the original query string or append it to the resulting URL (if you use the [QSA] flag).

PHP basically sees the same as the browser unless there's an internal redirection, in which case it sees the final URL and (optionally) the original query string.

ContentLicense: CC BY-SA 2.5


问:
Unable to get my development server to work
I installed Wampserver (including the 2 Visual C++ service packs).  I placed my tutorial files in the c:\wamp\www\ directory.

On the service tray, I'll go to "localhost" & "Your Projects" & I'll try to open an .html document that submits a form to a .php document.

I get a "Server Not Found" error.

Thank you in advance!

It worked for me several years ago.  The tutorial files are in an O'Reilly book ("Missing Manual")

答:
WAMP uses Apache. Go to your start menu, and search for WAMP, which should display a WAMP icon in your hidden icons section of your windows task bar. Just click on it, and click start all services, and bam!

Check out this post on SuperUser: https://superuser.com/a/980974


问:
Access to instance not referenced by user code of IDisposable into "using" statement
I have this piece of code:

```<code>using(var a = myFunction()){

    a.Process();

}
</code>```

There is some way to reference not defined instance (a in the above sample) into the using statement?

I mean, to write some like:

```<code>using(myFunction()){

    [Reflection?, another stuff?].Process();

}
</code>```

Where [Reflection?, another stuff?] should be the equivalent to a.

The instance (the value returned by myFunction()) exist into the using scope, I am sure, because the method Dispose() of IDisposable is fired properly.

Some clue?

答:
If you want to use the result of myFunction you should use it by declaring a variable.

myFunction could return null. You could define Process as an extension method for the type of the return value of myFunction. That way you can ignore null and not get any null reference exceptions.

Body:
I have this piece of code:

using(var a = myFunction()){

    a.Process();

}

There is some way to reference not defined instance (a in the above sample) into the using statement?

I mean, to write some like:

using(myFunction()){

    [Reflection?, another stuff?].Process();

}

Where [Reflection?, another stuff?] should be the equivalent to a.

The instance (the value returned by myFunction()) exist into the using scope, I am sure, because the method Dispose() of IDisposable is fired properly.

Some clue?


问:
angular2 svgPanZoom, window is not defined, webpack issue?
I would like to implement a zoom feature on my website which has a SVG image displayed.

I saw this library github.com/ariutta/svg-pan-zoom which provides the exact features i need,

However i can't seem to get it to work with angular2, window is not reachable.

<s>after some research i figured i have to shim window into the webpack export for svg-pan-zoom. Maybe i'm not looking for the right thing but i think it is quite amazing there is so much work that needs to be done just to import a 3rd party javascript.
best clue i could find is this : https://github.com/ariutta/svg-pan-zoom/issues/207</s>
EDIT: See answer.

<s>I used this angular 2 aspnet core starting project:  https://damienbod.com/2017/01/01/building-production-ready-angular-apps-with-visual-studio-and-asp-net-core/</s>

EDIT: actually it was this one
https://github.com/MarkPieszak/aspnetcore-angular2-universal but the branch got updated at the time i made this post which got me confused

I have this service here,

svg-pan-zoom.service.ts

```<code>import { Injectable } from '@angular/core'
import { isBrowser } from 'angular2-universal';
import * as svgPanZoom from 'svg-pan-zoom';

@Injectable()
export class SvgPanZoomService {

    getPanZoom(element: any) {
        if (isBrowser) {
            svgPanZoom(element);
        }
    }
}
</code>```

which is called here
map.component.ts

```<code>import { Component, AfterViewInit } from '@angular/core';
import { SvgPanZoomService } from '../../injectables/svg-pan-zoom.service';
import { isBrowser } from 'angular2-universal';

@Component({
    selector: 'map-full',
    template: require('./map.component.html'),
    styles: [require('./map.component.css')]
})
export class MapComponent implements AfterViewInit {

    constructor(private svgZoom: SvgPanZoomService) {
        if (isBrowser) {
            this.svgZoom.getPanZoom('#evSvgMap');
        }
    }
}
</code>```

The SvgPanZoomService service is referenced in my app.module

the Svg-pan-zoom lib is referenced in my package.json,

In the end my build gives me 2 js files, main-client.js and vendor.js

I can browse main-client.js and see it has references to svg-pan-zoom,

it is present in my browser sources when my page loads

But when it comes to loading the part where it should be doing stuff, i get this error.

```<code>An unhandled exception occurred while processing the request.

Exception: Call to Node module failed with error:
Prerendering failed because of error: ReferenceError: window is not defined
at D:\[mystuff]\node_modules\svg-pan-zoom\dist\svg-pan-zoom.js:1493:8
</code>```

Now i read i'm not meant to access window from a component, but i have no say in what the lib calls, i read somewhere here adding (isBrowser) should verify im not calling this server-side (why would i want the server to zoom this is ui right?)

here is my webpack.config.js

```<code>var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
            { test: /\.html$/, loader: 'raw' },
            { test: /\.css$/, loader: 'to-string!css' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
    entry: {
        'main-client': './ClientApp/boot-client.ts'
    },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    devtool: isDevBuild ? 'inline-source-map' : null,
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
    ].concat(isDevBuild ? [] : [
        // Plugins that apply in production builds only
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin()
    ])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
    entry: { 'main-server': './ClientApp/boot-server.ts' },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, './ClientApp/dist')
    },
    target: 'node',
    devtool: 'inline-source-map',
    externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});

module.exports = [clientBundleConfig, serverBundleConfig];
</code>```

the app then loads another webpack.config.vendor.js

```<code>var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('vendor.css');

module.exports = {
    resolve: {
        extensions: [ '', '.js' ]
    },
    module: {
        loaders: [
            { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
            { test: /\.css(\?|$)/, loader: extractCSS.extract(['css']) }
            { test: require("svg-pan-zoom"),
                loader: "imports-loader?window=>window./svg-pan-zoom.js"} // wild try
        ]
    },
    entry: {
        vendor: [
            '@angular/common',
            '@angular/compiler',
            '@angular/core',
            '@angular/http',
            '@angular/platform-browser',
            '@angular/platform-browser-dynamic',
            '@angular/router',
            '@angular/platform-server',
            'angular2-universal',
            'angular2-universal-polyfills',
            'bootstrap',
            'bootstrap/dist/css/bootstrap.css',
            'es6-shim',
            'es6-promise',
            'jquery',
            'zone.js',
            'svg-pan-zoom' //i added this, no clue if it's relevant.
                           //EDIT :Turns out it was important, very much so.
        ]
    },
    output: {
        path: path.join(__dirname, 'wwwroot', 'dist'),
        filename: '[name].js',
        library: '[name]_[hash]',
    },
    plugins: [
        extractCSS,
        new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery'}), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DllPlugin({
            path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
            name: '[name]_[hash]'
        })
    ].concat(isDevBuild ? [] : [
        new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
    ])
};
</code>```

i also have 2 files boot-client and boot-server related to the webpack.config.js which are instructions for the packaging of the server and client files.

thanks.

答:
This works

https://github.com/MarkPieszak/aspnetcore-angular2-universal#universal-gotchas

When building Universal components in Angular 2 there are a few things
  to keep in mind.
  window, document, navigator, and other browser types
  - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work. You do have some options, if
  you truly need some of this functionality:

If you need to use them, consider limiting them to only your client
  and wrapping them situationally. You can use the Object injected using
  the PLATFORM_ID token to check whether the current platform is browser
  or server.

```<code>import { PLATFORM_ID } from '@angular/core';
 import { isPlatformBrowser, isPlatformServer } from '@angular/common';

 constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }

 ngOnInit() {
   if (isPlatformBrowser(this.platformId)) {
      // Client only code.
      ...
   }
   if (isPlatformServer(this.platformId)) {
     // Server only code.
     ...
   }
 }
</code>```

However keep in mind that if you're using webpack you need to separate your client and server side code by making two separate bundles, the server side bundle must not contain references to the client side javascript libraries you are using, in this case svg-pan-zoom calls window which does not exist server side.

this separation can be achieved by adding a section such as below inside your webpack.config

```<code>    module: {
        rules:
        [{test: /svg-pan-zoom/,loader: 'null-loader'}]
    }
</code>```

this null-loader requires a npm install null-loader --save
more info : https://github.com/webpack-contrib/null-loader

Once you've separated the bundles, make sure to make every call to your scripts that may require window, document, navigator, i encountered issues with localStorage too, inside the if (isPlatformBrowser(this.platformId)) { //your code } blocks

EDIT now it's all good: Now i got it working I'll still keep this under here as it kept my spirits up when i could not figure out how to make anything work. webpack is very different from all the things i had seen before and i just was too lazy to read the docs but they do exist and in the end it is a very powerful tool that can get things done. The difference is that if you do not use server side rendering, features such as adding meta tags and descriptions will not be executed at server time. I believe the angular2 app is considered as client javascript by a google crawler for instance and thus will not be loaded making your SEO efforts worthless.

Another suggestion was to get rid of the server side rendering feature, which i did, and it worked for this and any other client related issue that arised later on.
https://github.com/MarkPieszak/aspnetcore-angular2-universal#faq---also-check-out-the-faq-issues-label

How can I disable Universal / SSR (Server-side rendering)?

Simply comment out the logic within HomeController, and replace
  @Html.Raw(ViewData["SpaHtml"]) with just your applications root
  AppComponent tag ("app" in our case): .

You could also remove any isPlatformBrowser/etc logic, and delete the
  boot-server, browser-app.module & server-app.module files, just make
  sure your boot-client file points to app.module.

Body:
I would like to implement a zoom feature on my website which has a SVG image displayed.

I saw this library github.com/ariutta/svg-pan-zoom which provides the exact features i need,

However i can't seem to get it to work with angular2, window is not reachable.

<s>after some research i figured i have to shim window into the webpack export for svg-pan-zoom. Maybe i'm not looking for the right thing but i think it is quite amazing there is so much work that needs to be done just to import a 3rd party javascript.
best clue i could find is this : https://github.com/ariutta/svg-pan-zoom/issues/207</s>
EDIT: See answer.

<s>I used this angular 2 aspnet core starting project:  https://damienbod.com/2017/01/01/building-production-ready-angular-apps-with-visual-studio-and-asp-net-core/</s>

EDIT: actually it was this one
https://github.com/MarkPieszak/aspnetcore-angular2-universal but the branch got updated at the time i made this post which got me confused

I have this service here,

svg-pan-zoom.service.ts

import { Injectable } from '@angular/core'
import { isBrowser } from 'angular2-universal';
import * as svgPanZoom from 'svg-pan-zoom';

@Injectable()
export class SvgPanZoomService {

    getPanZoom(element: any) {
        if (isBrowser) {
            svgPanZoom(element);
        }
    }
}

which is called here
map.component.ts

import { Component, AfterViewInit } from '@angular/core';
import { SvgPanZoomService } from '../../injectables/svg-pan-zoom.service';
import { isBrowser } from 'angular2-universal';

@Component({
    selector: 'map-full',
    template: require('./map.component.html'),
    styles: [require('./map.component.css')]
})
export class MapComponent implements AfterViewInit {

    constructor(private svgZoom: SvgPanZoomService) {
        if (isBrowser) {
            this.svgZoom.getPanZoom('#evSvgMap');
        }
    }
}

The SvgPanZoomService service is referenced in my app.module

the Svg-pan-zoom lib is referenced in my package.json,

In the end my build gives me 2 js files, main-client.js and vendor.js

I can browse main-client.js and see it has references to svg-pan-zoom,

it is present in my browser sources when my page loads

But when it comes to loading the part where it should be doing stuff, i get this error.

An unhandled exception occurred while processing the request.

Exception: Call to Node module failed with error:
Prerendering failed because of error: ReferenceError: window is not defined
at D:\[mystuff]\node_modules\svg-pan-zoom\dist\svg-pan-zoom.js:1493:8

Now i read i'm not meant to access window from a component, but i have no say in what the lib calls, i read somewhere here adding (isBrowser) should verify im not calling this server-side (why would i want the server to zoom this is ui right?)

here is my webpack.config.js

var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
            { test: /\.html$/, loader: 'raw' },
            { test: /\.css$/, loader: 'to-string!css' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
    entry: {
        'main-client': './ClientApp/boot-client.ts'
    },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    devtool: isDevBuild ? 'inline-source-map' : null,
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
    ].concat(isDevBuild ? [] : [
        // Plugins that apply in production builds only
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin()
    ])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
    entry: { 'main-server': './ClientApp/boot-server.ts' },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, './ClientApp/dist')
    },
    target: 'node',
    devtool: 'inline-source-map',
    externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});

module.exports = [clientBundleConfig, serverBundleConfig];

the app then loads another webpack.config.vendor.js

var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('vendor.css');

module.exports = {
    resolve: {
        extensions: [ '', '.js' ]
    },
    module: {
        loaders: [
            { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
            { test: /\.css(\?|$)/, loader: extractCSS.extract(['css']) }
            { test: require("svg-pan-zoom"),
                loader: "imports-loader?window=>window./svg-pan-zoom.js"} // wild try
        ]
    },
    entry: {
        vendor: [
            '@angular/common',
            '@angular/compiler',
            '@angular/core',
            '@angular/http',
            '@angular/platform-browser',
            '@angular/platform-browser-dynamic',
            '@angular/router',
            '@angular/platform-server',
            'angular2-universal',
            'angular2-universal-polyfills',
            'bootstrap',
            'bootstrap/dist/css/bootstrap.css',
            'es6-shim',
            'es6-promise',
            'jquery',
            'zone.js',
            'svg-pan-zoom' //i added this, no clue if it's relevant.
                           //EDIT :Turns out it was important, very much so.
        ]
    },
    output: {
        path: path.join(__dirname, 'wwwroot', 'dist'),
        filename: '[name].js',
        library: '[name]_[hash]',
    },
    plugins: [
        extractCSS,
        new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery'}), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DllPlugin({
            path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
            name: '[name]_[hash]'
        })
    ].concat(isDevBuild ? [] : [
        new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
    ])
};

i also have 2 files boot-client and boot-server related to the webpack.config.js which are instructions for the packaging of the server and client files.

thanks.

Body:
This works

https://github.com/MarkPieszak/aspnetcore-angular2-universal#universal-gotchas

When building Universal components in Angular 2 there are a few things
  to keep in mind.
  window, document, navigator, and other browser types
  - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work. You do have some options, if
  you truly need some of this functionality:

If you need to use them, consider limiting them to only your client
  and wrapping them situationally. You can use the Object injected using
  the PLATFORM_ID token to check whether the current platform is browser
  or server.

import { PLATFORM_ID } from '@angular/core';
 import { isPlatformBrowser, isPlatformServer } from '@angular/common';

 constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }

 ngOnInit() {
   if (isPlatformBrowser(this.platformId)) {
      // Client only code.
      ...
   }
   if (isPlatformServer(this.platformId)) {
     // Server only code.
     ...
   }
 }

However keep in mind that if you're using webpack you need to separate your client and server side code by making two separate bundles, the server side bundle must not contain references to the client side javascript libraries you are using, in this case svg-pan-zoom calls window which does not exist server side.

this separation can be achieved by adding a section such as below inside your webpack.config

    module: {
        rules:
        [{test: /svg-pan-zoom/,loader: 'null-loader'}]
    }

this null-loader requires a npm install null-loader --save
more info : https://github.com/webpack-contrib/null-loader

Once you've separated the bundles, make sure to make every call to your scripts that may require window, document, navigator, i encountered issues with localStorage too, inside the if (isPlatformBrowser(this.platformId)) { //your code } blocks

EDIT now it's all good: Now i got it working I'll still keep this under here as it kept my spirits up when i could not figure out how to make anything work. webpack is very different from all the things i had seen before and i just was too lazy to read the docs but they do exist and in the end it is a very powerful tool that can get things done. The difference is that if you do not use server side rendering, features such as adding meta tags and descriptions will not be executed at server time. I believe the angular2 app is considered as client javascript by a google crawler for instance and thus will not be loaded making your SEO efforts worthless.

Another suggestion was to get rid of the server side rendering feature, which i did, and it worked for this and any other client related issue that arised later on.
https://github.com/MarkPieszak/aspnetcore-angular2-universal#faq---also-check-out-the-faq-issues-label

How can I disable Universal / SSR (Server-side rendering)?

Simply comment out the logic within HomeController, and replace
  @Html.Raw(ViewData["SpaHtml"]) with just your applications root
  AppComponent tag ("app" in our case): .

You could also remove any isPlatformBrowser/etc logic, and delete the
  boot-server, browser-app.module & server-app.module files, just make
  sure your boot-client file points to app.module.


问:
Why do nested MaybeT's cause exponential allocation
I have a program.

```<code>import Control.Monad
import Control.Monad.Identity
import Control.Monad.Trans.Maybe

import System.Environment

tryR :: Monad m => ([a] -> MaybeT m [a]) -> ([a] -> m [a])
tryR f x = do
  m <- runMaybeT (f x)
  case m of
    Just t -> return t
    Nothing -> return x

check :: MonadPlus m => Int -> m Int
check x = if x `mod` 2 == 0 then return (x `div` 2) else mzero

foo :: MonadPlus m => [Int] -> m [Int]
foo [] = return []
foo (x:xs) = liftM2 (:) (check x) (tryR foo xs)

runFoo :: [Int] -> [Int]
runFoo x = runIdentity $ tryR foo x

main :: IO ()
main = do
  [n_str] <- getArgs
  let n = read n_str :: Int
  print $ runFoo [2,4..n]
</code>```

The main interesting thing about this program is that it can have many nested layers of MaybeT's. Here, doing so serves absolutely no purpose, but it did in the original program where I encountered this problem.

Care to take a guess of the time complexity of this program?

Okay, you cheated by reading the title of this question. Yes, it's exponential:

```<code>[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 50                                                                                (03-31 17:15)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
./ExpAlloc 50  8.10s user 0.06s system 99% cpu 8.169 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 52                                                                                (03-31 17:15)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
./ExpAlloc 52  16.10s user 0.12s system 99% cpu 16.227 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 54                                                                                (03-31 17:16)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
./ExpAlloc 54  32.32s user 0.23s system 99% cpu 32.561 total
</code>```

Some further inspection shows the reason is because it allocates an exponential amount of memory, which naturally takes an exponential amount of time:

```<code>[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 40 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
     939,634,520 bytes allocated in the heap
       5,382,816 bytes copied during GC
          75,808 bytes maximum residency (2 sample(s))
          66,592 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      1796 colls,     0 par    0.008s   0.009s     0.0000s    0.0000s
  Gen  1         2 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.243s  (  0.246s elapsed)
  GC      time    0.008s  (  0.009s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.252s  (  0.256s elapsed)

  %GC     time       3.2%  (3.6% elapsed)

  Alloc rate    3,869,930,149 bytes per MUT second

  Productivity  96.8% of total user, 95.3% of total elapsed

./ExpAlloc 40 +RTS -s  0.25s user 0.00s system 98% cpu 0.260 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 42 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
   1,879,159,424 bytes allocated in the heap
      10,767,048 bytes copied during GC
          95,504 bytes maximum residency (3 sample(s))
          71,152 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      3593 colls,     0 par    0.016s   0.018s     0.0000s    0.0000s
  Gen  1         3 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.493s  (  0.498s elapsed)
  GC      time    0.016s  (  0.018s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.510s  (  0.517s elapsed)

  %GC     time       3.1%  (3.5% elapsed)

  Alloc rate    3,810,430,292 bytes per MUT second

  Productivity  96.8% of total user, 95.7% of total elapsed

./ExpAlloc 42 +RTS -s  0.51s user 0.01s system 99% cpu 0.521 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 44 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
   3,758,208,408 bytes allocated in the heap
      21,499,312 bytes copied during GC
         102,056 bytes maximum residency (5 sample(s))
          73,784 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      7186 colls,     0 par    0.032s   0.037s     0.0000s    0.0009s
  Gen  1         5 colls,     0 par    0.000s   0.001s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.979s  (  0.987s elapsed)
  GC      time    0.033s  (  0.038s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    1.013s  (  1.024s elapsed)

  %GC     time       3.2%  (3.7% elapsed)

  Alloc rate    3,840,757,815 bytes per MUT second

  Productivity  96.7% of total user, 95.6% of total elapsed

./ExpAlloc 44 +RTS -s  1.01s user 0.01s system 99% cpu 1.029 total
</code>```

I cannot for the life of me figure out why it does this. I'd appreciate any light people could shed on the situation.

答:
The transformers package (currently at version 0.5.4.0) implements MonadTrans using liftM:

```<code>lift :: Monad m => m a -> MaybeT m a
lift = MaybeT . liftM Just
</code>```

where liftM is a combinator defined as

```<code>liftM :: Monad m => (a -> b) -> m a -> m b
liftM f m = m >>= \a -> return (f a)
</code>```

Furthermore, return is defined for MaybeT as

```<code>return :: Monad m => a -> MaybeT m a
return a = lift . return
</code>```

Reduce the definition:

```<code>return :: Monad m => a -> MaybeT m a
return a = MaybeT (return a >>= \a -> return (Just a))
</code>```

where the two inner return are instantiated at type m.

One call to return @(MaybeT m) calls return @m twice, hence the exponential behavior you observe for a tower of MaybeT.

This is fixable by using fmap instead of liftM, but this is backwards incompatible, when Functor was not a superclass of Monad.

EDIT: Other transformers do not have this issue, as return is not defined using lift, which provides an even better fix.

```<code>return = MaybeT . return . Just
</code>```

Here is a more minimal test case:

```<code>{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
import Control.Monad.Trans.Maybe
import System.Environment

f :: forall m proxy. Monad m => proxy m -> Int -> ()
f _ 0 = (return () :: m ()) `seq` ()
f _ n = f (undefined :: proxy (MaybeT m)) (n - 1)

main = do
  n : _ <- getArgs
  f (undefined :: proxy []) (read n) `seq` return ()
</code>```

Output

```<code>> for i in {18..21} ; time ./b $i
./b $i  0.35s user 0.04s system 99% cpu 0.390 total
./b $i  0.71s user 0.07s system 99% cpu 0.782 total
./b $i  1.38s user 0.18s system 99% cpu 1.565 total
./b $i  2.82s user 0.32s system 100% cpu 3.139 total
</code>```

Body:
I have a program.

import Control.Monad
import Control.Monad.Identity
import Control.Monad.Trans.Maybe

import System.Environment

tryR :: Monad m => ([a] -> MaybeT m [a]) -> ([a] -> m [a])
tryR f x = do
  m <- runMaybeT (f x)
  case m of
    Just t -> return t
    Nothing -> return x

check :: MonadPlus m => Int -> m Int
check x = if x `mod` 2 == 0 then return (x `div` 2) else mzero

foo :: MonadPlus m => [Int] -> m [Int]
foo [] = return []
foo (x:xs) = liftM2 (:) (check x) (tryR foo xs)

runFoo :: [Int] -> [Int]
runFoo x = runIdentity $ tryR foo x

main :: IO ()
main = do
  [n_str] <- getArgs
  let n = read n_str :: Int
  print $ runFoo [2,4..n]

The main interesting thing about this program is that it can have many nested layers of MaybeT's. Here, doing so serves absolutely no purpose, but it did in the original program where I encountered this problem.

Care to take a guess of the time complexity of this program?

Okay, you cheated by reading the title of this question. Yes, it's exponential:

[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 50                                                                                (03-31 17:15)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
./ExpAlloc 50  8.10s user 0.06s system 99% cpu 8.169 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 52                                                                                (03-31 17:15)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
./ExpAlloc 52  16.10s user 0.12s system 99% cpu 16.227 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 54                                                                                (03-31 17:16)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
./ExpAlloc 54  32.32s user 0.23s system 99% cpu 32.561 total

Some further inspection shows the reason is because it allocates an exponential amount of memory, which naturally takes an exponential amount of time:

[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 40 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
     939,634,520 bytes allocated in the heap
       5,382,816 bytes copied during GC
          75,808 bytes maximum residency (2 sample(s))
          66,592 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      1796 colls,     0 par    0.008s   0.009s     0.0000s    0.0000s
  Gen  1         2 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.243s  (  0.246s elapsed)
  GC      time    0.008s  (  0.009s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.252s  (  0.256s elapsed)

  %GC     time       3.2%  (3.6% elapsed)

  Alloc rate    3,869,930,149 bytes per MUT second

  Productivity  96.8% of total user, 95.3% of total elapsed

./ExpAlloc 40 +RTS -s  0.25s user 0.00s system 98% cpu 0.260 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 42 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
   1,879,159,424 bytes allocated in the heap
      10,767,048 bytes copied during GC
          95,504 bytes maximum residency (3 sample(s))
          71,152 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      3593 colls,     0 par    0.016s   0.018s     0.0000s    0.0000s
  Gen  1         3 colls,     0 par    0.000s   0.000s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.493s  (  0.498s elapsed)
  GC      time    0.016s  (  0.018s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.510s  (  0.517s elapsed)

  %GC     time       3.1%  (3.5% elapsed)

  Alloc rate    3,810,430,292 bytes per MUT second

  Productivity  96.8% of total user, 95.7% of total elapsed

./ExpAlloc 42 +RTS -s  0.51s user 0.01s system 99% cpu 0.521 total
[jkoppel@dhcp-18-189-103-38:~/tmp]$ time ./ExpAlloc 44 +RTS -s                                                                                (03-31 17:17)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
   3,758,208,408 bytes allocated in the heap
      21,499,312 bytes copied during GC
         102,056 bytes maximum residency (5 sample(s))
          73,784 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      7186 colls,     0 par    0.032s   0.037s     0.0000s    0.0009s
  Gen  1         5 colls,     0 par    0.000s   0.001s     0.0001s    0.0001s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.979s  (  0.987s elapsed)
  GC      time    0.033s  (  0.038s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    1.013s  (  1.024s elapsed)

  %GC     time       3.2%  (3.7% elapsed)

  Alloc rate    3,840,757,815 bytes per MUT second

  Productivity  96.7% of total user, 95.6% of total elapsed

./ExpAlloc 44 +RTS -s  1.01s user 0.01s system 99% cpu 1.029 total

I cannot for the life of me figure out why it does this. I'd appreciate any light people could shed on the situation.

Body:
The transformers package (currently at version 0.5.4.0) implements MonadTrans using liftM:

lift :: Monad m => m a -> MaybeT m a
lift = MaybeT . liftM Just

where liftM is a combinator defined as

liftM :: Monad m => (a -> b) -> m a -> m b
liftM f m = m >>= \a -> return (f a)

Furthermore, return is defined for MaybeT as

return :: Monad m => a -> MaybeT m a
return a = lift . return

Reduce the definition:

return :: Monad m => a -> MaybeT m a
return a = MaybeT (return a >>= \a -> return (Just a))

where the two inner return are instantiated at type m.

One call to return @(MaybeT m) calls return @m twice, hence the exponential behavior you observe for a tower of MaybeT.

This is fixable by using fmap instead of liftM, but this is backwards incompatible, when Functor was not a superclass of Monad.

EDIT: Other transformers do not have this issue, as return is not defined using lift, which provides an even better fix.

return = MaybeT . return . Just

Here is a more minimal test case:

{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
import Control.Monad.Trans.Maybe
import System.Environment

f :: forall m proxy. Monad m => proxy m -> Int -> ()
f _ 0 = (return () :: m ()) `seq` ()
f _ n = f (undefined :: proxy (MaybeT m)) (n - 1)

main = do
  n : _ <- getArgs
  f (undefined :: proxy []) (read n) `seq` return ()

Output

> for i in {18..21} ; time ./b $i
./b $i  0.35s user 0.04s system 99% cpu 0.390 total
./b $i  0.71s user 0.07s system 99% cpu 0.782 total
./b $i  1.38s user 0.18s system 99% cpu 1.565 total
./b $i  2.82s user 0.32s system 100% cpu 3.139 total


问:
c# using ActiveDir to get names under a managers org chart?
i am using ODBC to pull data from active directory to get the email for a particular username using the code below.

how can i use AD to pull all the usernames of peolel who report into a prticular manager?

i can transverse the org chart in outlook so im thinking i can do the same using AD...

ideas?

```<code>System.Data.OleDb.OleDbConnection con;
        System.Data.OleDb.OleDbCommand cmd;

        con = new System.Data.OleDb.OleDbConnection("Provider=ADsDSOObject;dsn=Active Directory Provider");
        con.Open();

        //Create a command object on this connection
        string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM' WHERE samaccountname = '" + UserName.Replace(@"AMRS\", "") + "'";
        cmd = new System.Data.OleDb.OleDbCommand(strSQL, con);

        try
        {
            return Convert.ToString (  cmd.ExecuteScalar() );
        }
        catch (System.Data.OleDb.OleDbException exc)
        {
            return "ERROR: " + exc.ToString();
        }
        finally
        {
            con.Close();
        }
</code>```

答:
See if the manager attribute in AD is set? It should return you the distinguished name of the manager.  You can then parse the string to figure out the samAccountName of the manager.
Then just repeat your search using the manager's distinguished name.

Now if the manager attribute isn't set....

Maybe search by department code, and then check the title of everyone in the department?

Might want to look into the Directory Services class.

This link gives you a basic tutorial on how to query AD

Body:
i am using ODBC to pull data from active directory to get the email for a particular username using the code below.

how can i use AD to pull all the usernames of peolel who report into a prticular manager?

i can transverse the org chart in outlook so im thinking i can do the same using AD...

ideas?

System.Data.OleDb.OleDbConnection con;
        System.Data.OleDb.OleDbCommand cmd;

        con = new System.Data.OleDb.OleDbConnection("Provider=ADsDSOObject;dsn=Active Directory Provider");
        con.Open();

        //Create a command object on this connection
        string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM' WHERE samaccountname = '" + UserName.Replace(@"AMRS\", "") + "'";
        cmd = new System.Data.OleDb.OleDbCommand(strSQL, con);

        try
        {
            return Convert.ToString (  cmd.ExecuteScalar() );
        }
        catch (System.Data.OleDb.OleDbException exc)
        {
            return "ERROR: " + exc.ToString();
        }
        finally
        {
            con.Close();
        }


问:
Get back my changes in a working directory after reverting them by `git checkout -- myfile`
I was working in my working directory, without running git add and git commit yet.

Then I would like to see how things work before my changes, so I reverted my changes on two files by

```<code>git checkout -- myfile1
git checkout -- myfile2
</code>```

Then i realized I would like to switch between before- and after-my-change, and learned that  git stash can be used to save my changes in a stack and pop it out later.

Now since I revert everything in the working directory, is it possible to get back my changes and then use git stash to switch between before and after my changes?

Thanks.

答:
Git cannot help you here.  Unless they were copied there from a Git commit (or the index), files in your work-tree are only in your work-tree; anything you do to them there, Git does not save until you git add them to copy them into Git's index.

Depending on your OS and other OS-specific items, you may be able to get the contents back from something other than Git, but that's all quite outside Git itself.

Body:
I was working in my working directory, without running git add and git commit yet.

Then I would like to see how things work before my changes, so I reverted my changes on two files by

git checkout -- myfile1
git checkout -- myfile2

Then i realized I would like to switch between before- and after-my-change, and learned that  git stash can be used to save my changes in a stack and pop it out later.

Now since I revert everything in the working directory, is it possible to get back my changes and then use git stash to switch between before and after my changes?

Thanks.


问:
onReady not firing for embedded youtube iframe
I am having issues getting the youtube api working. The API itself loads and the onYouTubeIframeAPIReady() function gets called, but the onReady doesn't work.

```<code>var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function onYouTubeIframeAPIReady() {
  log('API')
  log(document.getElementById('yt-pilezspnvu'));
  var player = new YT.Player('yt-pilezspnvu', {
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}

function onPlayerReady(event) {
  log(event);
}
function onPlayerStateChange(event) {
  log(event);
}
</code>```

The code above isn't wrapped in any functions or anything. The errors in the picture below are just my adblock stuff. I've tried adding origin=http://example.com as pointed out in this thread https://stackoverflow.com/a/20505337/736967 but still not working.

答:
I think that you need some extra bits that are missing.

Try adding in this to your code.

```<code>  ytplayer = new YT.Player('ytplayer', {

      height: '385',
      width: '640',
      videoId: '[your-videoID]',
      playerVars: {'wmode': 'opaque', 'autohide': 1 , 'enablejsapi': 1 , 'origin': 'http://www.yousite.com', 'rel': 0},

        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange,
        }
</code>```

playerVars has many parts and you can use or not. But you definatly need to have something load into the player. VideoID or PlayList. Else nothing will happen.

Body:
I am having issues getting the youtube api working. The API itself loads and the onYouTubeIframeAPIReady() function gets called, but the onReady doesn't work.

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function onYouTubeIframeAPIReady() {
  log('API')
  log(document.getElementById('yt-pilezspnvu'));
  var player = new YT.Player('yt-pilezspnvu', {
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}

function onPlayerReady(event) {
  log(event);
}
function onPlayerStateChange(event) {
  log(event);
}

The code above isn't wrapped in any functions or anything. The errors in the picture below are just my adblock stuff. I've tried adding origin=http://example.com as pointed out in this thread https://stackoverflow.com/a/20505337/736967 but still not working.

Body:
I think that you need some extra bits that are missing.

Try adding in this to your code.

  ytplayer = new YT.Player('ytplayer', {

      height: '385',
      width: '640',
      videoId: '[your-videoID]',
      playerVars: {'wmode': 'opaque', 'autohide': 1 , 'enablejsapi': 1 , 'origin': 'http://www.yousite.com', 'rel': 0},

        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange,
        }

playerVars has many parts and you can use or not. But you definatly need to have something load into the player. VideoID or PlayList. Else nothing will happen.


问:
Left join with ISNULL condition query taking long time to execute on MySQL RDS
Kindly help me to optimize below query,this query took more than 60 mints to execute with the ISNULL condition but if we replace ISNULL to <=> condition its took 15 mints to execute but our expectation is this query should take maxmimum 2 mints.

Both table have:- 3880494 records.

SELECT hs.headendid
FROM headendlineups_stagging hs
LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
WHERE ISNULL(hlp.headendid)

Replaced ISNULL with <=> operator

```<code>SELECT hs.headendid,hlp.headendid AS productionheadend
FROM headendlineups_stagging hs
LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
-- WHERE ISNULL(hlp.headendid)
WHERE hlp.headendid <=> NULL;
</code>```

Explain plain of both queries

```<code>**mysql> EXPLAIN
    ->  SELECT hs.headendid
    ->  FROM headendlineups_stagging hs
    ->  LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    ->  WHERE ISNULL(hlp.headendid)\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)**

mysql> EXPLAIN
    -> SELECT hs.headendid,hlp.headendid AS productionheadend
    -> FROM headendlineups_stagging hs
    -> LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    -> WHERE hlp.headendid <=> NULL\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)
</code>```

Kindly help me to understand if both queries are scanning the same number of records then why the execution time of both queries are different? and kindly suggest the better way of writing above query.

答:
The difference in time is likely to be caching; run each one again.

Change the LEFT JOIN to NOT EXISTS ( SELECT * FROM headendlineups_23march2017 hlp WHERE hs.headendid=hlp.headendid )  (It might or might not help.

Based on the EXPLAINs, it is doing the best it can -- namely Using index on both tables.  Face it, you have to scan the entire first table 3.8 million times, then check the second table 3.8 million times.

How much RAM?  What is the value of innodb_buffer_pool_size?  SHOW TABLE SIZE (for each); I want to compare to the buffer pool size.

Body:
Kindly help me to optimize below query,this query took more than 60 mints to execute with the ISNULL condition but if we replace ISNULL to <=> condition its took 15 mints to execute but our expectation is this query should take maxmimum 2 mints.

Both table have:- 3880494 records.

SELECT hs.headendid
FROM headendlineups_stagging hs
LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
WHERE ISNULL(hlp.headendid)

Replaced ISNULL with <=> operator

SELECT hs.headendid,hlp.headendid AS productionheadend
FROM headendlineups_stagging hs
LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
-- WHERE ISNULL(hlp.headendid)
WHERE hlp.headendid <=> NULL;

Explain plain of both queries

**mysql> EXPLAIN
    ->  SELECT hs.headendid
    ->  FROM headendlineups_stagging hs
    ->  LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    ->  WHERE ISNULL(hlp.headendid)\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)**

mysql> EXPLAIN
    -> SELECT hs.headendid,hlp.headendid AS productionheadend
    -> FROM headendlineups_stagging hs
    -> LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    -> WHERE hlp.headendid <=> NULL\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)

Kindly help me to understand if both queries are scanning the same number of records then why the execution time of both queries are different? and kindly suggest the better way of writing above query.


问:
Log4j2-RollingFile not writing when TimeBasedTriggeringPolicy enabled
Log4j2 is having trouble reading the date in the filePattern of my RollingFile Appender.  When I add a TimeBasedTriggeringPolicy and it re-configures my config file I get this error:

"ERROR Unable to inject fields into builder class for plugin type
  class org.apache.logging.log4j.core.appender.RollingFileAppender,
  element RollingFile. java.lang.IllegalStateException: Pattern does not
  contain a date"

I have put a breakpoint in the log4j2 code and found that the reason is the frequency variable that is part of the PatternProcessor object is null.  I don't know why this is null or how to set it to something other than null.

If I comment out the TimeBasedTriggeringPolicy, the appender begins to work and writes the logs to my file.

Here are the log4j dependencies in my POM:

```<code><dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-web</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
</dependency>
</code>```

Here is my log4j2.xml file:

```<code><?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="DEBUG">
    <Properties>
        <Property name="baseDir">/logs/oracle/domains/cim</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{DEFAULT} [%t] %-5p %c -%m%n}"/>
        </Console>
        <RollingFile name="RollingFile" fileName="${baseDir}/cim.log"
            filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout pattern="%d{DEFAULT} %-5p %c - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="31"/>
        </RollingFile>
    </Appenders>
  <Loggers>
    <Logger name="com.cim" level="debug" additivity="true">
        <Appender-Ref ref="RollingFile"/>
    </Logger>
    <Root level="debug" additivity="false">
        <Appender-Ref ref="Console"/>
    </Root>
  </Loggers>
</Configuration>
</code>```

Here is the initial stack trace after the Error message above:

```<code>at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNextTime(PatternProcessor.java:119)
    at org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.initialize(TimeBasedTriggeringPolicy.java:59)
    at org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy.initialize(CompositeTriggeringPolicy.java:52)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.setTriggeringPolicy(RollingFileManager.java:216)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.updateData(RollingFileManager.java:439)
    at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:119)
    at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:128)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:58)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:882)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:874)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:498)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:227)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:239)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:530)
    at org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:653)
    at org.apache.logging.log4j.core.config.ConfiguratonFileWatcher$ReconfigurationRunnable.run(ConfiguratonFileWatcher.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
</code>```

答:
The error message "Pattern does not contain a date" is clearly wrong, because your pattern is filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log", which does contain a date

However, there is a problem with the pattern. You did specify two triggering policies, a date-based and a size-based one.

The SizeBasedTriggeringPolicy also needs something in the filePattern, specifically %i, because when this policy triggers, it needs to rename the file to something.

With the size-based policy you usually end up with a bunch of rolled over log files like this:

```<code>app-1.log
app-2.log
app-3.log
...
</code>```

The filePattern needs to contain a %i conversion pattern, to tell Log4j2 where to put the counter, or it won't be able to rename the file.

ContentLicense: CC BY-SA 4.0

Body:
Log4j2 is having trouble reading the date in the filePattern of my RollingFile Appender.  When I add a TimeBasedTriggeringPolicy and it re-configures my config file I get this error:

"ERROR Unable to inject fields into builder class for plugin type
  class org.apache.logging.log4j.core.appender.RollingFileAppender,
  element RollingFile. java.lang.IllegalStateException: Pattern does not
  contain a date"

I have put a breakpoint in the log4j2 code and found that the reason is the frequency variable that is part of the PatternProcessor object is null.  I don't know why this is null or how to set it to something other than null.

If I comment out the TimeBasedTriggeringPolicy, the appender begins to work and writes the logs to my file.

Here are the log4j dependencies in my POM:

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-web</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
</dependency>

Here is my log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="DEBUG">
    <Properties>
        <Property name="baseDir">/logs/oracle/domains/cim</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{DEFAULT} [%t] %-5p %c -%m%n}"/>
        </Console>
        <RollingFile name="RollingFile" fileName="${baseDir}/cim.log"
            filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log" append="true">
            <PatternLayout pattern="%d{DEFAULT} %-5p %c - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="31"/>
        </RollingFile>
    </Appenders>
  <Loggers>
    <Logger name="com.cim" level="debug" additivity="true">
        <Appender-Ref ref="RollingFile"/>
    </Logger>
    <Root level="debug" additivity="false">
        <Appender-Ref ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

Here is the initial stack trace after the Error message above:

at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNextTime(PatternProcessor.java:119)
    at org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.initialize(TimeBasedTriggeringPolicy.java:59)
    at org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy.initialize(CompositeTriggeringPolicy.java:52)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.setTriggeringPolicy(RollingFileManager.java:216)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.updateData(RollingFileManager.java:439)
    at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:119)
    at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:128)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
    at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:58)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:882)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:874)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:498)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:227)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:239)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:530)
    at org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:653)
    at org.apache.logging.log4j.core.config.ConfiguratonFileWatcher$ReconfigurationRunnable.run(ConfiguratonFileWatcher.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Body:
The error message "Pattern does not contain a date" is clearly wrong, because your pattern is filePattern="${baseDir}/cim-%d{yyyy-MM-dd}.log", which does contain a date

However, there is a problem with the pattern. You did specify two triggering policies, a date-based and a size-based one.

The SizeBasedTriggeringPolicy also needs something in the filePattern, specifically %i, because when this policy triggers, it needs to rename the file to something.

With the size-based policy you usually end up with a bunch of rolled over log files like this:

app-1.log
app-2.log
app-3.log
...

The filePattern needs to contain a %i conversion pattern, to tell Log4j2 where to put the counter, or it won't be able to rename the file.