问:
Why do eig() and ishermitian() behave differently on different computers and MATLAB versions, and is there a way around it?
I'll first state my question and situation succinctly, then give a few details on the progress I've made so far in addressing the issues.

The function eig() behaves differently on different combinations of computer architecture and MATLAB version, in two ways I've noticed. First, the order of the eigenvalues and eigenvectors in the output is different, for the same exact code. Now, I've read in several places that MATLAB does NOT guarantee any specific order of those in the output, so one should always sort afterwards just to be safe, so even though I think this is part of a larger problem, I'll let that slide. The other way in which the behavior differs is that the eigenvalues are wrong in the cases where their order is not ascending. This is how I know they're wrong: I am dealing with complex Hermitian matrices of modest size, and the eigenvalues I get are complex, but they should be real for Hermitian matrices. On the architecture/version combinations where the order is ascending, the eigenvalues are also real as they should be. When they're complex, the imaginary parts are on the order of machine epsilon, so it's clear they're not supposed to be there at all. So, the hacky way around this is just to take the real part just in case it's complex when it shouldn't be, and then sort. A practical problem with this is that, when eig() is utilizing its routine that thinks the matrix is non-Hermitian, it takes twice as long and uses twice as much memory, presumably because of complex as opposed to real numbers. So, the questions are, why is this, and do we have a way around it?

Sorry for the long introduction. One thing I noticed is that, on the architecture/version combinations with the correct eigenvalues, it also consistently gives me the correct answer if I give a complex Hermitian matrix directly to ishermitian(). And when the eigenvalues are incorrect, ishermitian() is also incorrect. Some simple examples follow. So, my thought is that eig() is testing whether the matrix is Hermitian the same way ishermitian() does.

On the architecture/version combinations with incorrect eigenvalues, ishermitian() is still correct for something simple:

```<code>>> test=[1 2 3]+[4 5 6]*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     1
</code>```

The outer product of a complex vector and its conjugate transpose is a Hermitian matrix by definition. So we can try this again with a matrix which is around the same size as the ones I am dealing with. From the same computer:

```<code>>> test=randn(1,64)+randn(1,64)*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     0
</code>```

And we can see the corresponding problem with eig():

```<code>>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1              1024  double    complex
</code>```

And now, identical code on a different computer:

```<code>>> test=randn(1,64)+randn(1,64)*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     1

>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1               512  double
</code>```

I've tested this on a variety of systems, and I am not noticing any rule about what architectures or versions are responsible for this, so I won't bore with all those details. But I'm curious how frequent one result is versus the other. In my experience so far, it seems about half and half. Strangely, on the same computer:

```<code>>> test2=test';
>> mat=test2*test;
>> ishermitian(mat)

ans =

     0

>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1              1024  double    complex
</code>```

So if you assign the transpose to another variable, there is some precision problem like on the other computer, but if you don't, it works. Am I missing something about why this is happening, and/or is there a way around it?

答: To overcome this problem, try to round to some (small) degree or just explicitly make the matrix hermitian by copying (with no numeric operations involved).</p>

Body:
I'll first state my question and situation succinctly, then give a few details on the progress I've made so far in addressing the issues.

The function eig() behaves differently on different combinations of computer architecture and MATLAB version, in two ways I've noticed. First, the order of the eigenvalues and eigenvectors in the output is different, for the same exact code. Now, I've read in several places that MATLAB does NOT guarantee any specific order of those in the output, so one should always sort afterwards just to be safe, so even though I think this is part of a larger problem, I'll let that slide. The other way in which the behavior differs is that the eigenvalues are wrong in the cases where their order is not ascending. This is how I know they're wrong: I am dealing with complex Hermitian matrices of modest size, and the eigenvalues I get are complex, but they should be real for Hermitian matrices. On the architecture/version combinations where the order is ascending, the eigenvalues are also real as they should be. When they're complex, the imaginary parts are on the order of machine epsilon, so it's clear they're not supposed to be there at all. So, the hacky way around this is just to take the real part just in case it's complex when it shouldn't be, and then sort. A practical problem with this is that, when eig() is utilizing its routine that thinks the matrix is non-Hermitian, it takes twice as long and uses twice as much memory, presumably because of complex as opposed to real numbers. So, the questions are, why is this, and do we have a way around it?

Sorry for the long introduction. One thing I noticed is that, on the architecture/version combinations with the correct eigenvalues, it also consistently gives me the correct answer if I give a complex Hermitian matrix directly to ishermitian(). And when the eigenvalues are incorrect, ishermitian() is also incorrect. Some simple examples follow. So, my thought is that eig() is testing whether the matrix is Hermitian the same way ishermitian() does.

On the architecture/version combinations with incorrect eigenvalues, ishermitian() is still correct for something simple:

>> test=[1 2 3]+[4 5 6]*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     1

The outer product of a complex vector and its conjugate transpose is a Hermitian matrix by definition. So we can try this again with a matrix which is around the same size as the ones I am dealing with. From the same computer:

>> test=randn(1,64)+randn(1,64)*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     0

And we can see the corresponding problem with eig():

>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1              1024  double    complex

And now, identical code on a different computer:

>> test=randn(1,64)+randn(1,64)*1i;
>> mat=test'*test;
>> ishermitian(mat)

ans =

     1

>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1               512  double

I've tested this on a variety of systems, and I am not noticing any rule about what architectures or versions are responsible for this, so I won't bore with all those details. But I'm curious how frequent one result is versus the other. In my experience so far, it seems about half and half. Strangely, on the same computer:

>> test2=test';
>> mat=test2*test;
>> ishermitian(mat)

ans =

     0

>> vals=eig(mat);
>> whos vals
  Name       Size            Bytes  Class     Attributes

  vals      64x1              1024  double    complex

So if you assign the transpose to another variable, there is some precision problem like on the other computer, but if you don't, it works. Am I missing something about why this is happening, and/or is there a way around it?


来源: stackexchange

问:
Unexpected static counter variable behaviour
Lately I tried getting my head around the static keyword and here I attempt to simply use the static keyword for a variable declaration within a function. Like so:

```<code>void counter()
{
    static int counter = 0; //should be initialized only once and once only
    counter++; //increment for every call of this function
}
</code>```

I understand that due to the variable being static, it will live outside the function and therefore from wherever I decide to print out counter, it should give me the number of times the function counter() was called. So I did a simple test as shown:

```<code>int main()
{
    for(unsigned int i = 0; i < 10; i++){
        counter();
    }
    std::cout << counter << std::endl;

    return 0;
}
</code>```

From this test I expected to get number 10... but instead the number of counts the code returned was 1.

Please, what am I missing here?

I found other submissions to "similar" issues such as this one:
static counter in c++
But they mostly revolve around the static keyword being used in classes.

答: counter is still a local variable inside void counter() so you still need to return it.

ContentLicense: CC BY-SA 3.0

Body:
Lately I tried getting my head around the static keyword and here I attempt to simply use the static keyword for a variable declaration within a function. Like so:

void counter()
{
    static int counter = 0; //should be initialized only once and once only
    counter++; //increment for every call of this function
}

I understand that due to the variable being static, it will live outside the function and therefore from wherever I decide to print out counter, it should give me the number of times the function counter() was called. So I did a simple test as shown:

int main()
{
    for(unsigned int i = 0; i < 10; i++){
        counter();
    }
    std::cout << counter << std::endl;

    return 0;
}

From this test I expected to get number 10... but instead the number of counts the code returned was 1.

Please, what am I missing here?

I found other submissions to "similar" issues such as this one:
static counter in c++
But they mostly revolve around the static keyword being used in classes.


问:
Java - Passing 2D array to function
```<code>-------------------------------------------------------------
|             | herndon | fairfax | baltimore | centerville |
-------------------------------------------------------------
|   herndon   |    0    |    50   |    100    |     20      |
-------------------------------------------------------------
|   fairfax   |   50    |    0    |    70     |     110     |
-------------------------------------------------------------
|  baltimore  |   100   |    70   |     0     |     200     |
-------------------------------------------------------------
| centerville |   20    |    110  |    200    |      0      |
-------------------------------------------------------------
</code>```

Java - Passing 2D array to function

1) defining a 2D array for distance and populate it -> done ( Its shown in program )

2) defining a city array -> done  ( Its shown in program )

3) ask the user for  soure and destination of the city :

e.g.

Enter from city:

 Enter To city:

Lets use JOptionPane and Error handling function when user enters cityNames.

4)call function" findDistance "and passsing array of citynames ,array of distances to this function.

5) call function " display" distance <- showing the distances of those cities that was entered by user .

problems i am having :

a)  passing arrays of citynames and array  of distance to findDistance function

(followup #4)

b) passing the argumants to function display.
(followup #5)

here is what i did so Far !

```<code>package test;

class Test1 {
    public static void main(String[] args) {
        int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}}; // step 1
        String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};  // step 2
        findDistance(distances); // ->  a . 1st problem , i am having problem to pass citynames and distances to this function.
        // display  ()     // -> b .  2nd problem i am having : i have a  issue to pass the arguments to this function to Display Distances between 2 cities .
    public static void findDistance(String[] names, int[] dist, int[] src) {
        for(int i = 0; i < src.length; i++) {
            // i would like to use JOptioanPane function and error handling
            // instead of system.out.println.
            System.out.println("enter from city" + src[i]);
        }
        for(int i = 0; i < dist.length; i++) {
            System.out.println("enter from city" + dist[i]);
        }
        int i = 0;
        dist[i] = src[i];
        return;
    }
}
</code>```

答:
Based on your question and the given code, I wrote the below class. I intentionally left method bodies empty so you'll need to figure out some details there yourself. If you can fill bodies of findDistance and display methods you can accomplish what you want.

```<code>package Test;

import java.util.Scanner;

class Test1 {
    public static void main(String[] args) {
        int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}};
        String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter from city:");
        String src = keyboard.nextLine();
        System.out.println("enter to city:");
        String dest = keyboard.nextLine();
        int distance = findDistance(CityNames, distances, src, dest);
        display(distance);
    }

    public static int findDistance(String[] CityNames, int[][] distances, String src, String dest) {
        // Find and return the distance between src and dest.
        return 0;
    }

    public static void display(int distance) {
        // Display the distance to user.
    }
}
</code>```

Title: Java - Passing 2D array to function

Body:
-------------------------------------------------------------
|             | herndon | fairfax | baltimore | centerville |
-------------------------------------------------------------
|   herndon   |    0    |    50   |    100    |     20      |
-------------------------------------------------------------
|   fairfax   |   50    |    0    |    70     |     110     |
-------------------------------------------------------------
|  baltimore  |   100   |    70   |     0     |     200     |
-------------------------------------------------------------
| centerville |   20    |    110  |    200    |      0      |
-------------------------------------------------------------

Java - Passing 2D array to function

1) defining a 2D array for distance and populate it -> done ( Its shown in program )

2) defining a city array -> done  ( Its shown in program )

3) ask the user for  soure and destination of the city :

e.g.

Enter from city:

 Enter To city:

Lets use JOptionPane and Error handling function when user enters cityNames.

4)call function" findDistance "and passsing array of citynames ,array of distances to this function.

5) call function " display" distance <- showing the distances of those cities that was entered by user .

problems i am having :

a)  passing arrays of citynames and array  of distance to findDistance function

(followup #4)

b) passing the argumants to function display.
(followup #5)

here is what i did so Far !

package test;

class Test1 {
    public static void main(String[] args) {
        int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}}; // step 1
        String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};  // step 2
        findDistance(distances); // ->  a . 1st problem , i am having problem to pass citynames and distances to this function.
        // display  ()     // -> b .  2nd problem i am having : i have a  issue to pass the arguments to this function to Display Distances between 2 cities .
    public static void findDistance(String[] names, int[] dist, int[] src) {
        for(int i = 0; i < src.length; i++) {
            // i would like to use JOptioanPane function and error handling
            // instead of system.out.println.
            System.out.println("enter from city" + src[i]);
        }
        for(int i = 0; i < dist.length; i++) {
            System.out.println("enter from city" + dist[i]);
        }
        int i = 0;
        dist[i] = src[i];
        return;
    }
}

Body:
Based on your question and the given code, I wrote the below class. I intentionally left method bodies empty so you'll need to figure out some details there yourself. If you can fill bodies of findDistance and display methods you can accomplish what you want.

package Test;

import java.util.Scanner;

class Test1 {
    public static void main(String[] args) {
        int distances[][] = {{0, 50, 100, 20}, {50, 0, 70, 110}, {100, 70, 0, 200}, {20, 110, 200, 0}};
        String CityNames[] = {"herndon", "fairfax", "baltimore", "centerville"};
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter from city:");
        String src = keyboard.nextLine();
        System.out.println("enter to city:");
        String dest = keyboard.nextLine();
        int distance = findDistance(CityNames, distances, src, dest);
        display(distance);
    }

    public static int findDistance(String[] CityNames, int[][] distances, String src, String dest) {
        // Find and return the distance between src and dest.
        return 0;
    }

    public static void display(int distance) {
        // Display the distance to user.
    }
}


问:
Where can I find locust logs?
I'm using locust to stress test our app.

I'm getting errors because the POST call seems incorrect. Where can I see the logs for locust? I'd like to see what the post call looks like to see what's wrong.

Here's my code in case someone can tell me what I'm doing wrong:

```<code>from locust import HttpLocust, TaskSet, task

json3 = """{"stack_name": "beenz-php-app-12", "disable_rollback": true, "template": "php", "timeout_mins": 60}"""

class MyTaskSet(TaskSet):
    @task
    def send(self):
             response = self.client.post("/stacks", json3, headers={'X-Auth-Key': 'xxxx', 'Content-Type': 'application/json', 'X-Auth-User': 'xxxx', 'Accept': 'application/json', 'X-Auth-Token':'xxxx'})
            print "Response status code:", response.status_code
            print "Response content:", response.content

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000
</code>```

Thanks!

答:
The --logfile option mentioned by the others is probably the easiest route. Note that you probably want to log your messages rather than printing them. I think locust sets up a special stdout logger such that print messages go to the console, but not to the log file.

An alternative to --logfile is to utilize python's logging system to add your own file appender. This is a good option if you need a rolling file appender, or have a log format that you prefer over the format that locust configures.

Here is an example of how we setup and use logging in our locust test. Notice how each instance of the taskset logs to its own logger. That makes it easy to filter the log file to a single locust. Also note that we log all HTTP errors as warnings.

```<code>from locust import HttpLocust, TaskSet, task
import itertools
import logging
import socket
from logging.handlers import RotatingFileHandler

def append_file_logger():
    root_logger = logging.getLogger()
    log_format = "%(asctime)s.%(msecs)03d000 [%(levelname)s] {0}/%(name)s : %(message)s".format(socket.gethostname())
    formatter = logging.Formatter(log_format, '%Y-%m-%d %H:%M:%S')
    file_handler = RotatingFileHandler('./locust.log', maxBytes=5 * 1024 * 1024, backupCount=3)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    root_logger.addHandler(file_handler)

append_file_logger()

counter = itertools.count()

class FooTaskSet(TaskSet):
    def on_start(self):
        self.logger = logging.getLogger('locust-%03d' % counter.next())
        self.logger.info('Hatching locust')

    @task
    def send(self):
        response = self.client.post(...)
        if not response.ok:
            self.logger.warn('Error sending post') # TODO add status code, url, and reponse to the log
</code>```

Final suggestion : If you have multiple tasks, configure them to log all http errors with the same format. Makes it easy to extract data from your log.

Title: Where can I find locust logs?

Body:
I'm using locust to stress test our app.

I'm getting errors because the POST call seems incorrect. Where can I see the logs for locust? I'd like to see what the post call looks like to see what's wrong.

Here's my code in case someone can tell me what I'm doing wrong:

from locust import HttpLocust, TaskSet, task

json3 = """{"stack_name": "beenz-php-app-12", "disable_rollback": true, "template": "php", "timeout_mins": 60}"""

class MyTaskSet(TaskSet):
    @task
    def send(self):
             response = self.client.post("/stacks", json3, headers={'X-Auth-Key': 'xxxx', 'Content-Type': 'application/json', 'X-Auth-User': 'xxxx', 'Accept': 'application/json', 'X-Auth-Token':'xxxx'})
            print "Response status code:", response.status_code
            print "Response content:", response.content

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000

Thanks!

Body:
The --logfile option mentioned by the others is probably the easiest route. Note that you probably want to log your messages rather than printing them. I think locust sets up a special stdout logger such that print messages go to the console, but not to the log file.

An alternative to --logfile is to utilize python's logging system to add your own file appender. This is a good option if you need a rolling file appender, or have a log format that you prefer over the format that locust configures.

Here is an example of how we setup and use logging in our locust test. Notice how each instance of the taskset logs to its own logger. That makes it easy to filter the log file to a single locust. Also note that we log all HTTP errors as warnings.

from locust import HttpLocust, TaskSet, task
import itertools
import logging
import socket
from logging.handlers import RotatingFileHandler

def append_file_logger():
    root_logger = logging.getLogger()
    log_format = "%(asctime)s.%(msecs)03d000 [%(levelname)s] {0}/%(name)s : %(message)s".format(socket.gethostname())
    formatter = logging.Formatter(log_format, '%Y-%m-%d %H:%M:%S')
    file_handler = RotatingFileHandler('./locust.log', maxBytes=5 * 1024 * 1024, backupCount=3)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    root_logger.addHandler(file_handler)

append_file_logger()

counter = itertools.count()

class FooTaskSet(TaskSet):
    def on_start(self):
        self.logger = logging.getLogger('locust-%03d' % counter.next())
        self.logger.info('Hatching locust')

    @task
    def send(self):
        response = self.client.post(...)
        if not response.ok:
            self.logger.warn('Error sending post') # TODO add status code, url, and reponse to the log

Final suggestion : If you have multiple tasks, configure them to log all http errors with the same format. Makes it easy to extract data from your log.


问:
Flask app streaming multiple logs to multiple clients
I have written a small Flask app to stream multiple log files to a browser over the internet.

```<code>import json
import os
import re
import flask
from shelljob import proc

import eventlet
eventlet.sleep()
eventlet.monkey_patch()

app = flask.Flask(__name__)

@app.route( '/stream/<string:case_name>/<string:wind_dir>' )
def stream(case_name, wind_dir):
    g = proc.Group()
    foamrun = os.environ["FOAM_RUN"]
    foamcase = os.path.join(foamrun, case_name, wind_dir)
    log_file = os.path.join(foamcase, 'logs', 'run.log')
    print log_file
    p = g.run( [ "tail", "-f", log_file ] )
    def read_process():
        while g.is_pending():
            lines = g.readlines()
            for proc, line in lines:
                # process line and create payload
                yield "data:" + json.dumps(payload) + "\n\n"

    return flask.Response( read_process(), mimetype='text/event-stream' )

@app.after_request
def after_request(response):
  response.headers.add('Access-Control-Allow-Origin', '*')
  response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
  response.headers.add('Access-Control-Allow-Methods', 'GET')
  return response

if __name__ == "__main__":
    foamrun = os.environ["FOAM_RUN"]
    app.run(threaded=True, host='0.0.0.0', port=9001)
</code>```

I run this app with gunicorn with the command

```<code>gunicorn server:app -k eventlet -b 0.0.0.0:9001
</code>```

When I open the two links:

```<code>http://X.X.X.X:9001/stream/test01_base_Baseline/NW
http://X.X.X.X:9001/stream/test01_base_Baseline/N
</code>```

I have a strange behaviour. One of the two streams works as I expect, but the second one hangs or is streamed in bulks. For example, on the first page I receive a line each second, while on the second page I receive around 15-20 lines every 20 seconds or so. The behaviour is also not consistent. Sometimes it is the first page that hangs and the second that behaves regularly.

I am quite new to web development.

EDIT

I have tried to replace read_process with a much simpler version

```<code>def read_process():
    i = 1
    while True:
       payload = 'line' + str(i)
       i += 1
       yield "data:" + json.dumps(payload) + "\n\n"
       sleep(1)
</code>```

This version does not havethe same issue and behaves as I would expect. The two streams are received together.

答:
What's happening g.readlines() blocks until more data is available from tail process. Unfortunately, it's blocking whole program instead of one green thread.

It should be possible to use eventlet.tpool.Proxy(open(log_file, 'rb')) without tail process.

If that fails, next best option right now is to do file operations in separate OS thread and communicate data via global variables and synchronize access to said variables via local socket. I know that's lame because it repeats half code from shelljob and another half from eventlet.tpool. Sorry, we have a bug in tpool that prevents easier solution.

Body:
I have written a small Flask app to stream multiple log files to a browser over the internet.

import json
import os
import re
import flask
from shelljob import proc

import eventlet
eventlet.sleep()
eventlet.monkey_patch()

app = flask.Flask(__name__)

@app.route( '/stream/<string:case_name>/<string:wind_dir>' )
def stream(case_name, wind_dir):
    g = proc.Group()
    foamrun = os.environ["FOAM_RUN"]
    foamcase = os.path.join(foamrun, case_name, wind_dir)
    log_file = os.path.join(foamcase, 'logs', 'run.log')
    print log_file
    p = g.run( [ "tail", "-f", log_file ] )
    def read_process():
        while g.is_pending():
            lines = g.readlines()
            for proc, line in lines:
                # process line and create payload
                yield "data:" + json.dumps(payload) + "\n\n"

    return flask.Response( read_process(), mimetype='text/event-stream' )

@app.after_request
def after_request(response):
  response.headers.add('Access-Control-Allow-Origin', '*')
  response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
  response.headers.add('Access-Control-Allow-Methods', 'GET')
  return response

if __name__ == "__main__":
    foamrun = os.environ["FOAM_RUN"]
    app.run(threaded=True, host='0.0.0.0', port=9001)

I run this app with gunicorn with the command

gunicorn server:app -k eventlet -b 0.0.0.0:9001

When I open the two links:

http://X.X.X.X:9001/stream/test01_base_Baseline/NW
http://X.X.X.X:9001/stream/test01_base_Baseline/N

I have a strange behaviour. One of the two streams works as I expect, but the second one hangs or is streamed in bulks. For example, on the first page I receive a line each second, while on the second page I receive around 15-20 lines every 20 seconds or so. The behaviour is also not consistent. Sometimes it is the first page that hangs and the second that behaves regularly.

I am quite new to web development.

EDIT

I have tried to replace read_process with a much simpler version

def read_process():
    i = 1
    while True:
       payload = 'line' + str(i)
       i += 1
       yield "data:" + json.dumps(payload) + "\n\n"
       sleep(1)

This version does not havethe same issue and behaves as I would expect. The two streams are received together.


问:
Disable (make read-only) text input on mat-datepicker when using a reactive form
I am using Angular Material 2's mat-datepicker and want the input element disabled so the user cannot edit the value using the text input.

There are detailed instructions in the Angular Material 2 docs on how to disable different parts of the mat-datepicker however, these do not seem to cover how to disable the text input when it is part of a reactive form.

The Angular Material docs suggest you disable the text input in the following way:

```<code><mat-form-field>
              // Add the disabled attribute to the input element ======
              <input disabled
                     matInput
                     [matDatepicker]="dateJoined"
                     placeholder="Date joined"
                     formControlName="dateJoined">
              <mat-datepicker-toggle matSuffix [for]="dateJoined"></mat-datepicker-toggle>

              // Add [disabled]=false to the mat-datepicker =======
              <mat-datepicker [disabled]="false"
                              startView="year"
                              #dateJoined></mat-datepicker>
            </mat-form-field>
</code>```

However, if your datepicker is part of a reactive form the text element remains active and you get the following  message from Angular:

It looks like you're using the disabled attribute with a reactive form
directive. If you set disabled to true when you set up this control in
your component class, the disabled attribute will actually be set in
the DOM for you. We recommend using this approach to avoid 'changed
after checked' errors.
Example:
form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true})});

I updated the FormGroup in the component to disable the FormControl, which has the desired effect of disabling the input, however, if you then get the value of the FormGroup using this.form.value the disabled form control is no longer present.

Is there a work around for this which does not involve having a separate template driven form using ngModel just for the mat-datepicker(s)?

EDIT: added “read-only” to post title to clarify this isn’t about disabling the control - because what I was asking about was to make the input element read-only, but not disable to control itself

答:
To create a disabled FormControl is really simple.

1 - Don't use disabled attribute in your template;

2 - Instantiate your FormGroup like this:

```<code>this.formGroup = this.formBuilder.group({
  dateJoined: { disabled: true, value: '' }
  // ...
});
</code>```

That being said, although you want to prevent users from typing something in the input, you still want to let them select a date by clicking the button (more specifically in matSuffix), right?

If it's correct, disable doesn't work for this case, because it'll disable all the input (including the button in matSuffix).

To solve your case, you can use readonly. Instantiate the FormGroup normally and then in template:

```<code><input
  matInput
  readonly <- HERE
  [matDatepicker]="dateJoined"
  placeholder="Date joined"
  formControlName="dateJoined">
</code>```

DEMO

ContentLicense: CC BY-SA 4.0

Body:
I am using Angular Material 2's mat-datepicker and want the input element disabled so the user cannot edit the value using the text input.

There are detailed instructions in the Angular Material 2 docs on how to disable different parts of the mat-datepicker however, these do not seem to cover how to disable the text input when it is part of a reactive form.

The Angular Material docs suggest you disable the text input in the following way:

<mat-form-field>
              // Add the disabled attribute to the input element ======
              <input disabled
                     matInput
                     [matDatepicker]="dateJoined"
                     placeholder="Date joined"
                     formControlName="dateJoined">
              <mat-datepicker-toggle matSuffix [for]="dateJoined"></mat-datepicker-toggle>

              // Add [disabled]=false to the mat-datepicker =======
              <mat-datepicker [disabled]="false"
                              startView="year"
                              #dateJoined></mat-datepicker>
            </mat-form-field>

However, if your datepicker is part of a reactive form the text element remains active and you get the following  message from Angular:

It looks like you're using the disabled attribute with a reactive form
directive. If you set disabled to true when you set up this control in
your component class, the disabled attribute will actually be set in
the DOM for you. We recommend using this approach to avoid 'changed
after checked' errors.
Example:
form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true})});

I updated the FormGroup in the component to disable the FormControl, which has the desired effect of disabling the input, however, if you then get the value of the FormGroup using this.form.value the disabled form control is no longer present.

Is there a work around for this which does not involve having a separate template driven form using ngModel just for the mat-datepicker(s)?

EDIT: added “read-only” to post title to clarify this isn’t about disabling the control - because what I was asking about was to make the input element read-only, but not disable to control itself

Body:
To create a disabled FormControl is really simple.

1 - Don't use disabled attribute in your template;

2 - Instantiate your FormGroup like this:

this.formGroup = this.formBuilder.group({
  dateJoined: { disabled: true, value: '' }
  // ...
});

That being said, although you want to prevent users from typing something in the input, you still want to let them select a date by clicking the button (more specifically in matSuffix), right?

If it's correct, disable doesn't work for this case, because it'll disable all the input (including the button in matSuffix).

To solve your case, you can use readonly. Instantiate the FormGroup normally and then in template:

<input
  matInput
  readonly <- HERE
  [matDatepicker]="dateJoined"
  placeholder="Date joined"
  formControlName="dateJoined">

DEMO


问:
Is it a good practice to wrap all layouts in CoordinatorLayout?
I'm thinking of a way to implement Android Snackbars in my app. Basically, I want to be able to show Snackbar from anywhere in the app.

As I found out, android.support.design.widget.Snackbar performs the best when put in android.support.design.widget.CoordinatorLayout. Otherwise, I cannot swipe it away, it shows over navigation drawer and doesn't interact with Floating Action Button.

So the question is: Is it a good practice to wrap ALL my layouts in CoordinatorLayout, get the reference for it in a BaseActivity, so that it can be passed to Snackbar from almost anywhere?

That seems to be a solid way to ensure the Snackbar and other layout components behave correctly, but... well, means touching all layouts and having one BaseActivity which is extended by all other Activities and which would be accessed from any Fragment wanting to show a Snackbar.

Is there a better way?

答:
These are the options you have. Use one of these as you need in the project.

The best way

The best way of doing this is what you already said in your question, add a BaseActivity and extend all your activities from it. According to the official documentation of CoordinatorLayout,

CoordinatorLayout is intended for two primary use cases:

As a top-level application decor or chrome layout

As a container for a specific interaction with one or more child views

So CoordinatorLayout is created primarily for this reason (Though there are also other reasons). There will be the least performance issues as mentioned in the documentation.

By Using FrameLayout

As already answered by Rainmaker, you can use a Activity with a reference to the CoordinatorLayout layout in your layouts folder where the child will be a framelayout.

```<code><?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/activity_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
</code>```

Then you will be using only one activity with setContentView(R.layout.root_coordinate_layout). Then you will convert all other activities into fragments and add them with :

```<code>MyFragment myf = new MyFragment();
FragmentTransaction transaction = getFragmentManager()
    .beginTransaction()
    .add(R.id.your_layout, myf)
    .commit();
</code>```

The programmatic way

This is another way of doing the same thing. But this is a bit more complex and need a lot of works to do.

In all your activity, instead of setContentView(R.id.your_layout), use this:

```<code>LayoutInflater inflater = LayoutInflater.from(this);
ConstraintLayout yourLayout = (ConstraintLayout) inflater.inflate(R.layout.your_layout, null, false);
CoordinatorLayout layout = new CoordinatorLayout(this);
// Set its property as you wish ...
layout.addView(mainScreen);
setContentView(layout);
</code>```

Body:
These are the options you have. Use one of these as you need in the project.

The best way

The best way of doing this is what you already said in your question, add a BaseActivity and extend all your activities from it. According to the official documentation of CoordinatorLayout,

CoordinatorLayout is intended for two primary use cases:

As a top-level application decor or chrome layout

As a container for a specific interaction with one or more child views

So CoordinatorLayout is created primarily for this reason (Though there are also other reasons). There will be the least performance issues as mentioned in the documentation.

By Using FrameLayout

As already answered by Rainmaker, you can use a Activity with a reference to the CoordinatorLayout layout in your layouts folder where the child will be a framelayout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/activity_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

Then you will be using only one activity with setContentView(R.layout.root_coordinate_layout). Then you will convert all other activities into fragments and add them with :

MyFragment myf = new MyFragment();
FragmentTransaction transaction = getFragmentManager()
    .beginTransaction()
    .add(R.id.your_layout, myf)
    .commit();

The programmatic way

This is another way of doing the same thing. But this is a bit more complex and need a lot of works to do.

In all your activity, instead of setContentView(R.id.your_layout), use this:

LayoutInflater inflater = LayoutInflater.from(this);
ConstraintLayout yourLayout = (ConstraintLayout) inflater.inflate(R.layout.your_layout, null, false);
CoordinatorLayout layout = new CoordinatorLayout(this);
// Set its property as you wish ...
layout.addView(mainScreen);
setContentView(layout);


问:
Java client calling WSE 2.0 with DIME attachment
I need to integrate with a legacy .NET Web Service that uses WSE 2.0 for WS-Security and DIME. The catch is I need to do this from a Java application.

I'm expecting that Axis2 works fine with the WS-Security because folks around here have done it before. It's the DIME that I'm concerned about. I see a reference to DIME at http://ws.apache.org/axis/java/client-side-axis.html, but I'm wondering if anyone has actually done this with Axis and a WSE 2.0 Web Service.

答: My investigation and experiments on this topic a year ago showed that Axis 2 doesn't supports DIME attachments despite the fact that there were some unclear notes in documentation and defines in the code. Now documentation is cleared up and there are no any notes on DIME support. But Axis 1.4 works with DIME attachments OK. Downgrade you Axis library to 1.4 if you can't force your partners to upgrade their service to support MTOM/XOP.</p>

ContentLicense: CC BY-SA 2.5


问:
Render curly braces as plain text in React/JSX
I am having problems displaying { and } as text in React. I saw a similar question that someone said to wrap the entire string in curlies, but this is not working:

```<code>let queries_block = this.state.previous_queries.map((dataset) => {
            return (<p>{"{{}}"}<p>)
        });

        if (results) {
            results_block = (
                <div>
                    <p>Queries:</p>
                    {queries_block}
                    <br/><br/>
                    <p>Results: {results_count}</p>
                    <JSONPretty id="json-pretty" json={results}></JSONPretty>
                </div>
            );
        } else {
            results_block = null;
        }
</code>```

The return (
{"{{}}"}
) causes

```<code>ERROR in ./src/components/app.js
Module build failed: SyntaxError: Unexpected token, expected } (47:13)

  45 |                     <JSONPretty id="json-pretty" json={results}></JSONPretty>
  46 |                 </div>
> 47 |             );
     |              ^
  48 |         } else {
  49 |             results_block = null;
  50 |         }

 @ ./src/index.js 15:11-38
webpack: Failed to compile.
</code>```

Is there an easy way to escape curly braces in jsx?

答:
If you'd like to render curly braces as plain text within a JSX document simply use the HTML character codes.

Left Curly Brace { : {

Right Curly Brace } : }

Body:
I am having problems displaying { and } as text in React. I saw a similar question that someone said to wrap the entire string in curlies, but this is not working:

let queries_block = this.state.previous_queries.map((dataset) => {
            return (
{"{{}}"}
)
        });

        if (results) {
            results_block = (

Queries:

                    {queries_block}

Results: {results_count}

                    <JSONPretty id="json-pretty" json={results}></JSONPretty>

            );
        } else {
            results_block = null;
        }

The return (
{"{{}}"}
) causes

ERROR in ./src/components/app.js
Module build failed: SyntaxError: Unexpected token, expected } (47:13)

  45 |                     <JSONPretty id="json-pretty" json={results}></JSONPretty>
  46 |

> 47 |             );
     |              ^
  48 |         } else {
  49 |             results_block = null;
  50 |         }

 @ ./src/index.js 15:11-38
webpack: Failed to compile.

Is there an easy way to escape curly braces in jsx?

Body:
If you'd like to render curly braces as plain text within a JSX document simply use the HTML character codes.

Left Curly Brace { : &#123;

Right Curly Brace } : &#125;


问:
SQLite multiple SCAN TABLEs for a VIEW
My EXPLAIN QUERY PLAN says that a table underlying a VIEW is going to be scanned multiple times; is that really the case, and can I avoid it?

Here's a contrived example similar to my production data, which shows the issue with two made-up sports teams, in a world where we only trust people whose names begin with D, E, or F.

```<code>CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE trust (glob TEXT);
CREATE VIEW trusted_people AS
  SELECT * FROM people JOIN trust ON name GLOB glob;

CREATE TABLE teams (
  keeper INTEGER REFERENCES people(id),
  winger INTEGER REFERENCES people(id),
  scorer INTEGER REFERENCES people(id)
);

INSERT INTO people(name) VALUES ('alf'), ('bob'), ('chad'), ('don'), ('ed'), ('frank');
INSERT INTO trust(glob) VALUES ('d*'), ('e*'), ('f*');
INSERT INTO teams VALUES (1, 2, 3), (4, 5, 6);

EXPLAIN QUERY PLAN SELECT tk.name, tw.name, ts.name FROM teams
  JOIN trusted_people AS tk ON teams.keeper = tk.id
  JOIN trusted_people AS tw ON teams.winger = tw.id
  JOIN trusted_people AS ts ON teams.scorer = ts.id
;

-- SCAN TABLE teams
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SCAN TABLE trust
-- SCAN TABLE trust
-- SCAN TABLE trust
</code>```

答:
In general, in relational SQL a view will only perform as well as its underlying query.  In the case of the view trusted_people, using an index is not possible when joining to this view, since this is a derived table.  Hence, the join may not perorm so well.  To make matters worse, you are joining to this view three times.

But I see what I believe to be a problem in your database schema.  I think you can reduce your entire query to only two joins, by making the following change to the teams table:

```<code>team_id | player_id | role
1       | 1         | 'keeper'
1       | 2         | 'winger'
1       | 3         | 'scorer'
2       | 4         | 'keeper'
2       | 5         | 'winger'
2       | 6         | 'scorer'
</code>```

Now the following query should yield the results you want:

```<code>SELECT t1.*
FROM teams t1
INNER JOIN people p
    ON t1.player_id = p.id
INNER JOIN trust t2
    ON p.name GLOB t2.glob;
</code>```

If you wanted a CSV list of each matching player per team, you could use this:

```<code>SELECT
    t1.team_id,
    GROUP_CONCAT(p.name) AS players
FROM teams t1
INNER JOIN people p
    ON t1.player_id = p.id
INNER JOIN trust t2
    ON p.name GLOB t2.glob
GROUP BY
    t1.team_id;
</code>```

Note than the second join between people and trust could still benefit from some index tuning, but just making this change alone might leave the performance at a level you can tolerate.

Title: SQLite multiple SCAN TABLEs for a VIEW

Body:
My EXPLAIN QUERY PLAN says that a table underlying a VIEW is going to be scanned multiple times; is that really the case, and can I avoid it?

Here's a contrived example similar to my production data, which shows the issue with two made-up sports teams, in a world where we only trust people whose names begin with D, E, or F.

CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE trust (glob TEXT);
CREATE VIEW trusted_people AS
  SELECT * FROM people JOIN trust ON name GLOB glob;

CREATE TABLE teams (
  keeper INTEGER REFERENCES people(id),
  winger INTEGER REFERENCES people(id),
  scorer INTEGER REFERENCES people(id)
);

INSERT INTO people(name) VALUES ('alf'), ('bob'), ('chad'), ('don'), ('ed'), ('frank');
INSERT INTO trust(glob) VALUES ('d*'), ('e*'), ('f*');
INSERT INTO teams VALUES (1, 2, 3), (4, 5, 6);

EXPLAIN QUERY PLAN SELECT tk.name, tw.name, ts.name FROM teams
  JOIN trusted_people AS tk ON teams.keeper = tk.id
  JOIN trusted_people AS tw ON teams.winger = tw.id
  JOIN trusted_people AS ts ON teams.scorer = ts.id
;

-- SCAN TABLE teams
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SEARCH TABLE people USING INTEGER PRIMARY KEY (rowid=?)
-- SCAN TABLE trust
-- SCAN TABLE trust
-- SCAN TABLE trust

Body:
In general, in relational SQL a view will only perform as well as its underlying query.  In the case of the view trusted_people, using an index is not possible when joining to this view, since this is a derived table.  Hence, the join may not perorm so well.  To make matters worse, you are joining to this view three times.

But I see what I believe to be a problem in your database schema.  I think you can reduce your entire query to only two joins, by making the following change to the teams table:

team_id | player_id | role
1       | 1         | 'keeper'
1       | 2         | 'winger'
1       | 3         | 'scorer'
2       | 4         | 'keeper'
2       | 5         | 'winger'
2       | 6         | 'scorer'

Now the following query should yield the results you want:

SELECT t1.*
FROM teams t1
INNER JOIN people p
    ON t1.player_id = p.id
INNER JOIN trust t2
    ON p.name GLOB t2.glob;

If you wanted a CSV list of each matching player per team, you could use this:

SELECT
    t1.team_id,
    GROUP_CONCAT(p.name) AS players
FROM teams t1
INNER JOIN people p
    ON t1.player_id = p.id
INNER JOIN trust t2
    ON p.name GLOB t2.glob
GROUP BY
    t1.team_id;

Note than the second join between people and trust could still benefit from some index tuning, but just making this change alone might leave the performance at a level you can tolerate.


问:
script to dump single record from table in mysql
I want to dump single record from mysql table named as table1 for id = 5.

What is mysql query for same?

I think we can use mysqldump but i have used it with full records of table. But how to use it with single record?

答: Use this query "SELECT * FROM table1 WHERE id='5' LIMIT 1" . Should work fine.</p>


问:
Angular View Rendering multiple times in place, why?
Im using the new angular template, and everytime I make a code change, the view render either underneath, on top of, or beneath the existing view, instead of updating it. Does anyone know why? It didnt do this when I created the project,
 but I can't see why it would now.

```<code><mat-toolbar style="background:purple">
    <mat-button-toggle (click)="sidenav.toggle()"><i class="material-icons" style="color:white">menu</i></mat-button-toggle>
</mat-toolbar>
<mat-sidenav-container>
    <mat-sidenav #sidenav style="width: 20%;">
        <mat-selection-list>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item two</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
        </mat-selection-list>
    </mat-sidenav>
    <router-outlet></router-outlet>
</mat-sidenav-container>

<!DOCTYPE html>
<html style="height:100%;">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - RemoteTrainer</title>

    <base href="~/" />
    <link href="~/dist/deeppurple-amber.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js" type="text/javascript"></script>
</head>
<body>
    @RenderBody()

    @RenderSection("scripts", required: false)
</body>
</html>

<h1>Hello, world!</h1>
<p>Welcome to your new single-page application, built with:</p>
<ul>
    <li><a href='https://get.asp.net/'>ASP.NET Core</a> and <a href='https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx'>C#</a> for cross-platform server-side code</li>
    <li><a href='https://angular.io/'>Angular</a> and <a href='http://www.typescriptlang.org/'>TypeScript</a> for client-side code</li>
    <li><a href='https://webpack.github.io/'>Webpack</a> for building and bundling client-side resources</li>
    <li><a href='http://getbootstrap.com/'>Bootstrap</a> for layout and styling</li>
</ul>
<p>To help you get  started, we've also set up:</p>
<ul>
    <li><strong>Client-side navigation</strong>. For example,  click <em>Counter</em> then <em>Back</em> to return here.</li>
    <li><strong>Server-side prerendering</strong>. For faster initial loading and improved SEO, your Angular app is prerendered on the server. The resulting HTML is then transferred to the browser where a client-side copy of the app takes over.</li>
    <li><strong>Webpack dev middleware</strong>. In development mode, there's no need to run the <code>webpack</code> build tool. Your client-side resources are dynamically built on demand. Updates are available as soon as you modify any file.</li>
    <li><strong>Hot module replacement</strong>. In development mode, you don't even need to reload the page after making most changes. Within seconds of saving changes to files, your Angular app will be rebuilt and a new instance injected is into the page.</li>
    <li><strong>Efficient production builds</strong>. In production mode, development-time features are disabled, and the <code>webpack</code> build tool produces minified static CSS and JavaScript files.</li>
</ul>
<button mat-fab>Click Me</button>
<mat-slider min="1" max="5" step="0.5" value="1.5"></mat-slider>
</code>```

答: For anyone interested, I found the answer to my question elsewhere. The problem is caused by including the browser animation module. Adding oldRootElem!.remove(); underneath oldRootElem!.parentNode!.insertBefore(newRootElem, oldRootElem); in boot.browser.ts fixes the issue by disposing of the old view after appending the new one. </p>

Body:
Im using the new angular template, and everytime I make a code change, the view render either underneath, on top of, or beneath the existing view, instead of updating it. Does anyone know why? It didnt do this when I created the project,
 but I can't see why it would now.

<mat-toolbar style="background:purple">
    <mat-button-toggle (click)="sidenav.toggle()">menu</mat-button-toggle>
</mat-toolbar>
<mat-sidenav-container>
    <mat-sidenav #sidenav style="width: 20%;">
        <mat-selection-list>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item two</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
            <mat-list-item>Item one</mat-list-item>
        </mat-selection-list>
    </mat-sidenav>
    <router-outlet></router-outlet>
</mat-sidenav-container>

<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - RemoteTrainer</title>

    <base href="~/" />
    <link href="~/dist/deeppurple-amber.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    @RenderBody()

    @RenderSection("scripts", required: false)

Hello, world!

Welcome to your new single-page application, built with:

ASP.NET Core and C# for cross-platform server-side code

Angular and TypeScript for client-side code

Webpack for building and bundling client-side resources

Bootstrap for layout and styling

To help you get  started, we've also set up:

Client-side navigation. For example,  click Counter then Back to return here.

Server-side prerendering. For faster initial loading and improved SEO, your Angular app is prerendered on the server. The resulting HTML is then transferred to the browser where a client-side copy of the app takes over.

Webpack dev middleware. In development mode, there's no need to run the webpack build tool. Your client-side resources are dynamically built on demand. Updates are available as soon as you modify any file.

Hot module replacement. In development mode, you don't even need to reload the page after making most changes. Within seconds of saving changes to files, your Angular app will be rebuilt and a new instance injected is into the page.

Efficient production builds. In production mode, development-time features are disabled, and the webpack build tool produces minified static CSS and JavaScript files.

<button mat-fab>Click Me</button>
<mat-slider min="1" max="5" step="0.5" value="1.5"></mat-slider>


问:
FileUpload error - "The form field fileInput did not contain a file" (CFML - FW/1)
I'm trying to upload a file on a ColdFusion site and am receiving the following error:

"The form field fileInput did not contain a file."

The file along with some text values are submitted through a form on an Edit page. The form is submitted to the "save" method in the controller, which sends two calls to fileService. The text values are passed as expected and updating them works fine. When I dump the RC Scope struct "fileInput" (the name of the input I am using to get the file) shows as [empty string]. It is not shown in the Form scope at all. Does anyone have an idea on what could be causing the issue with this? There are a couple of answered questions that are related to forms that use cffile (tags), but everything on the site I'm working on uses FileUpload (cfscript), so I'm not sure how to apply those answers here.

FYI The site uses Framework One (FW/1 version 4.1) as its MVC framework. I'm using ColdFusion 2016. Following are snippets of the code being used.

View: Edit.cfm

```<code><form action="#BuildURL( action='file.save' )#" name="filesForm" method="post" enctype="multipart/form-data">
  <input id="file_id" name="file_id" type="hidden" value="#rc.file_id#">
  <div class="form-group">
    <label for="name">File Name</label>
    <input type="text" class="form-control" id="name" name="name" placeholder="Enter File Name" value="#rc.name#">
  </div>
  <div class="form-group">
    <label for="fileInput">File Upload</label>
    <input type="file" id="fileInput" name"fileInput">
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary" name="Submit">Save</button>
  </div>
</form>
</code>```

Controller: file.cfc

```<code>public void function edit(rc){
  if (StructKeyExists(rc, 'file_id') && rc.file_id > 0){
    var q = fileService.getFile(rc.file_id);
    rc.file_id = q.file_id;
    rc.name = q.name;
  }
}

public void function save(required struct rc){
  rc.file_id = variables.fileService.save(rc.file_id, rc.name);

  param name = "rc.fileInput" default="";
  rc.result = variables.fileService.uploadFile(file = "fileInput");

  variables.fw.redirect(action='file.edit', append='file_id', queryString = "msg=updated");
}
</code>```

Service: file.cfc

```<code>struct function uploadFile(required string file){
  fileUploadResult = FileUpload(getTempDirectory(), arguments.file, "application/pdf", "MakeUnique");
}
</code>```

答:
I am not familiar with FW/1, but maybe in public void function save(), fileInput should be rc.fileInput, i.e.</p>

```<code> rc.result = variables.fileService.uploadFile(file = rc.fileInput);
</code>```

Body:
I'm trying to upload a file on a ColdFusion site and am receiving the following error:

"The form field fileInput did not contain a file."

The file along with some text values are submitted through a form on an Edit page. The form is submitted to the "save" method in the controller, which sends two calls to fileService. The text values are passed as expected and updating them works fine. When I dump the RC Scope struct "fileInput" (the name of the input I am using to get the file) shows as [empty string]. It is not shown in the Form scope at all. Does anyone have an idea on what could be causing the issue with this? There are a couple of answered questions that are related to forms that use cffile (tags), but everything on the site I'm working on uses FileUpload (cfscript), so I'm not sure how to apply those answers here.

FYI The site uses Framework One (FW/1 version 4.1) as its MVC framework. I'm using ColdFusion 2016. Following are snippets of the code being used.

View: Edit.cfm

<form action="#BuildURL( action='file.save' )#" name="filesForm" method="post" enctype="multipart/form-data">
  <input id="file_id" name="file_id" type="hidden" value="#rc.file_id#">

    File Name
    <input type="text" class="form-control" id="name" name="name" placeholder="Enter File Name" value="#rc.name#">

    File Upload
    <input type="file" id="fileInput" name"fileInput">

    <button type="submit" class="btn btn-primary" name="Submit">Save</button>

</form>

Controller: file.cfc

public void function edit(rc){
  if (StructKeyExists(rc, 'file_id') && rc.file_id > 0){
    var q = fileService.getFile(rc.file_id);
    rc.file_id = q.file_id;
    rc.name = q.name;
  }
}

public void function save(required struct rc){
  rc.file_id = variables.fileService.save(rc.file_id, rc.name);

  param name = "rc.fileInput" default="";
  rc.result = variables.fileService.uploadFile(file = "fileInput");

  variables.fw.redirect(action='file.edit', append='file_id', queryString = "msg=updated");
}

Service: file.cfc

struct function uploadFile(required string file){
  fileUploadResult = FileUpload(getTempDirectory(), arguments.file, "application/pdf", "MakeUnique");
}

Body:
I am not familiar with FW/1, but maybe in public void function save(), fileInput should be rc.fileInput, i.e.

 rc.result = variables.fileService.uploadFile(file = rc.fileInput);


答:
You are printing counter in main(), where it wasn't declared. Just because you declared it as static in counter() doesn't mean you can access it in main().

If you do :

```<code>void counter() {
    static int counter = 0;
    std::cout << counter++ << std::endl;
}

int main() {
    for(unsigned int i = 0; i < 10; i++){
        counter();
    }
    return 0;
}
</code>```

You can see the counter incrementing.

Title: Unexpected static counter variable behaviour

Body:
You are printing counter in main(), where it wasn't declared. Just because you declared it as static in counter() doesn't mean you can access it in main().

If you do :

void counter() {
    static int counter = 0;
    std::cout << counter++ << std::endl;
}

int main() {
    for(unsigned int i = 0; i < 10; i++){
        counter();
    }
    return 0;
}

You can see the counter incrementing.


问:
MailChimp PUT /lists/{list_id}/members/{subscriber_hash} Interests Additive?
I'm implementing the MailChimp APIs in my order processing system. I'm looking to automatically update interests for my subscribers when they order products from two categories.

After logging their order in MailChimp, I'm calling the PUT /lists/{list_id}/members/{subscriber_hash} API to log the interest tied to the current order. E.g if the current order is an app, I'd put them in the "software" interest.

My concern is, let's say this user bought a book before, so they're already a subscriber with the "books" interest. If I send a PUT request to this with only the "software" interest set to true, does that remove the previous interest already saved with the subscriber?

答:
No, sending a PUT request that contains only a single interest will not affect any of the subscriber's other interests. Using your example, if a subscriber already has the 'books' interest set to true, then a new request that sets the 'software' interest to true will result in both interests being true.

The result of the PUT request will contain the interest IDs and their statuses in the response, something like this:

```<code>...
"interests": {
    "books_interest_id": true,
    "software_interest_id": true
},
...
</code>```

Body:
No, sending a PUT request that contains only a single interest will not affect any of the subscriber's other interests. Using your example, if a subscriber already has the 'books' interest set to true, then a new request that sets the 'software' interest to true will result in both interests being true.

The result of the PUT request will contain the interest IDs and their statuses in the response, something like this:

...
"interests": {
    "books_interest_id": true,
    "software_interest_id": true
},
...


问:
Why are these .then() occurring out of order?
I've got a node application that spawns a child_process. When the child_process is finished running, I'd like to resolve a promise. The following code works, but the .then() statements occur out of order:

```<code>const storage = require('./storage');
const logging = require('./logging');
const process = require('child_process').spawn;

function convertIncomingFile(pathToFile) {
  logging.info(`Converting ${pathToFile}`);
  const convert = process(`cat`, [pathToFile], {});

  return Promise.resolve(
    convert.stdout.on('data', (data) => {
      logging.info(data.toString('utf8'));
    }),
    convert.stderr.on('data', (err) => {
      logging.error(err);
    }),
    convert.on('close', (code) => {
      logging.info(`Conversion finished with status code ${code}`);
    })
  );
}

module.exports = {
  convertFile: (filename) => {
    storage.downloadFile(filename).
      then((localFilename) => {
        logging.info(`File saved to: ${localFilename}`);
      }).
      then(() => convertIncomingFile(`./files/${filename}`)).
      then(() => {
        logging.info(`Coversion of ${filename} complete.`);
      }).
      catch((apiErr) => {
        logging.error(apiErr);
      });
  }
};
</code>```

The output I get is:

```<code>info: File saved to: ./files/package.json
info: Converting ./files/package.json
info: Coversion of package.json complete.
info: {
 <file contents>
}

info: Conversion finished with status code 0
</code>```

As you can see the Conversion of package.json complete. statement occurs before the file contents are logged and the conversion status code statement. Why is this the case and how do I get the 'Conversion complete' statement to come after the 'status code' statement?

答:
Promise.resolve means return a solved value that you give it, it's not realy async as you expected. Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for more detailed info

function convertIncomingFile(pathToFile) {
  logging.info(`Converting ${pathToFile}`);
  const convert = process(`cat`, [pathToFile], {});

  return new Promise((resolve, reject) => {
    convert.stdout.on('data', (data) => {
        logging.info(data.toString('utf8'));
      }),
      convert.stderr.on('data', (err) => {
        logging.error(err);
        reject()
      }),
      convert.on('close', (code) => {
        logging.info(`Conversion finished with status code ${code}`);
        resolve()
      })
  })
}```

Body:
I've got a node application that spawns a child_process. When the child_process is finished running, I'd like to resolve a promise. The following code works, but the .then() statements occur out of order:

const storage = require('./storage');
const logging = require('./logging');
const process = require('child_process').spawn;

function convertIncomingFile(pathToFile) {
  logging.info(`Converting ${pathToFile}`);
  const convert = process(`cat`, [pathToFile], {});

  return Promise.resolve(
    convert.stdout.on('data', (data) => {
      logging.info(data.toString('utf8'));
    }),
    convert.stderr.on('data', (err) => {
      logging.error(err);
    }),
    convert.on('close', (code) => {
      logging.info(`Conversion finished with status code ${code}`);
    })
  );
}

module.exports = {
  convertFile: (filename) => {
    storage.downloadFile(filename).
      then((localFilename) => {
        logging.info(`File saved to: ${localFilename}`);
      }).
      then(() => convertIncomingFile(`./files/${filename}`)).
      then(() => {
        logging.info(`Coversion of ${filename} complete.`);
      }).
      catch((apiErr) => {
        logging.error(apiErr);
      });
  }
};

The output I get is:

info: File saved to: ./files/package.json
info: Converting ./files/package.json
info: Coversion of package.json complete.
info: {
 <file contents>
}

info: Conversion finished with status code 0

As you can see the Conversion of package.json complete. statement occurs before the file contents are logged and the conversion status code statement. Why is this the case and how do I get the 'Conversion complete' statement to come after the 'status code' statement?


问:
Asterisk outbound calls per extension
I have an asterisk server where i have a group of extensions that can make an outbound call. These extensions will never receive a call and are kinda one time use. Meaning a user will use it and then it will be randomly assigned to another user. Users can range from 1 to X rather than creating say 100 extensions could I just reuse the same Extension for every user? Are there limitations to this opposed to giving each user a dedicated extension?</p>

答:
Asterisk not care what you do with extensions. How you expect it do that?

You even can have few billions extensions on server, only few loaded in memory and working.

Only limitation is number of extensions you put in your config file. If you need number of different extensions, user asterisk realtime/extensions in mysql.

You are responsible for tracking which extensions used by which user at current time, asterisk will not do that becuase of no info availible at pbx level.

Title: Asterisk outbound calls per extension


问:
Error: [filter:notarray] Expected array but received: 0
error

```<code>Error: [filter:notarray] Expected array but received: 0
</code>```

html

Activator (a menu item) of the getConcessionaireList function.

```<code><ion-item ng-click="getConcessionaireList()" >
</ion-item>
</code>```

Input search for the lists of displayed data from $scope.concessionairesList

```<code><input type="search" placeholder="Search" ng-model="searchQuery">
<ion-list>
</code>```

Values from $scope.concessioniresList is to be displayed here when Activator is clicked.

```<code><ion-item class="item item-icon-left" ng-repeat="x in concessionairesList track by $index | filter:searchQuery" href="#/app/encodereading/{{x.accountNumber}}">
    {{x.accountNumber}} - {{x.meterNumber}} - {{x.lastName | capitalize}}, {{x.firstName | capitalize}} {{x.middleName | initial}}
</ion-item>
</code>```

js

Data that will be fetched from database using php

```<code>$scope.getConcessionaireList = function(){
      $http.get('http://localhost/test/php/getConcessionaires.php').then(function(res){
        $scope.show($ionicLoading);
        console.log(res.data);
        $scope.concessionairesList = res.data;
      }).finally(function(){
        $timeout(function(){
          $scope.hide($ionicLoading);
        }, 1000)
      })
    }
</code>```

答:
You are using track by $index before you are applying your filter. To resolve this, change your expression to:

```<code><ion-item class="item item-icon-left" ng-repeat="x in concessionairesList  | filter:searchQuery track by $index" href="#/app/encodereading/{{x.accountNumber}}">
</code>```

Body:
error

Error: [filter:notarray] Expected array but received: 0

html

Activator (a menu item) of the getConcessionaireList function.

<ion-item ng-click="getConcessionaireList()" >
</ion-item>

Input search for the lists of displayed data from $scope.concessionairesList

<input type="search" placeholder="Search" ng-model="searchQuery">
<ion-list>

Values from $scope.concessioniresList is to be displayed here when Activator is clicked.

<ion-item class="item item-icon-left" ng-repeat="x in concessionairesList track by $index | filter:searchQuery" href="#/app/encodereading/{{x.accountNumber}}">
    {{x.accountNumber}} - {{x.meterNumber}} - {{x.lastName | capitalize}}, {{x.firstName | capitalize}} {{x.middleName | initial}}
</ion-item>

js

Data that will be fetched from database using php

$scope.getConcessionaireList = function(){
      $http.get('http://localhost/test/php/getConcessionaires.php').then(function(res){
        $scope.show($ionicLoading);
        console.log(res.data);
        $scope.concessionairesList = res.data;
      }).finally(function(){
        $timeout(function(){
          $scope.hide($ionicLoading);
        }, 1000)
      })
    }

Body:
You are using track by $index before you are applying your filter. To resolve this, change your expression to:

<ion-item class="item item-icon-left" ng-repeat="x in concessionairesList  | filter:searchQuery track by $index" href="#/app/encodereading/{{x.accountNumber}}">


问:
RPG timestamp validation, is this best practice?
For RPG timestamp validation, is this best practice?  It seem that there should be a way to test using TEST opcode?</p>

```<code>d DateTimeST      S               Z
d Value           S             26A
d*---------------------------------------------------------------------------
c                   Eval      Value = '2017-11-16-09.54.40.840586'
c                   Eval      DateTimeST = %timestamp(Value)
c                   If        %Error
c                   EXSR      DoSometing
c                   ENDIF
c*-------------------------------------------------------------------
c     DoSometing    BEGSR
c                   ENDSR
c*-------------------------------------------------------------------
c                   Eval         *inlr = *on
c                   return
</code>```

答: You'll get an RNX0112 error at runtime. Use Monitor/On-Error *All/EndMon</p>

Body:
For RPG timestamp validation, is this best practice?  It seem that there should be a way to test using TEST opcode?

d DateTimeST      S               Z
d Value           S             26A
d*---------------------------------------------------------------------------
c                   Eval      Value = '2017-11-16-09.54.40.840586'
c                   Eval      DateTimeST = %timestamp(Value)
c                   If        %Error
c                   EXSR      DoSometing
c                   ENDIF
c*-------------------------------------------------------------------
c     DoSometing    BEGSR
c                   ENDSR
c*-------------------------------------------------------------------
c                   Eval         *inlr = *on
c                   return


问:
How to I index the transformed log records into AWS Elasticsearch?

TLDR

The lambda function is not able to index the firehose logs into the AWS managed ES due to an "encoding problem".

Actual Error Response

I do not get any error when I base64 encode a single logEvent from a firehose record and send the collected records to the AWS managed ES.

See the next section for more details.

The base 64 encoded compressed payload is being sent to ES as the resulting json transformation is too big for ES to index - see this ES link.

I get the following error from the AWS managed ES:

```<code>{
    "deliveryStreamARN": "arn:aws:firehose:us-west-2:*:deliverystream/*",
    "destination": "arn:aws:es:us-west-2:*:domain/*",
    "deliveryStreamVersionId": 1,
    "message": "The data could not be decoded as UTF-8",
    "errorCode": "InvalidEncodingException",
    "processor": "arn:aws:lambda:us-west-2:*:function:*"
  }
</code>```

If the output record is not compressed, the body size is too long (as small as 14MB). Without compression and a simple base64 encoded payload, I get the following error in the Lambda logs:

```<code>{
  "type": "mapper_parsing_exception",
  "reason": "failed to parse",
  "caused_by": {
    "type": "not_x_content_exception",
    "reason": "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
  }
}
</code>```

Description

I have Cloudwatch logs that are getting buffered by size / interval which gets fed into a Kinesis Firehose. The firehose transports the logs into a lambda function which transforms the log into a json record which should then send it over to the AWS managed Elasticsearch cluster.

The lambda function gets the following JSON structure:

```<code>{
    "invocationId": "cf1306b5-2d3c-4886-b7be-b5bcf0a66ef3",
    "deliveryStreamArn": "arn:aws:firehose:...",
    "region": "us-west-2",
    "records": [{
        "recordId": "49577998431243709525183749876652374166077260049460232194000000",
        "approximateArrivalTimestamp": 1508197563377,
        "data": "some_compressed_data_in_base_64_encoding"
    }]
}
</code>```

The lambda function then extracts .records[].data and decodes the data as base64 and decompresses the data which results in the following JSON:

```<code>{
  "messageType": "DATA_MESSAGE",
  "owner": "aws_account_number",
  "logGroup": "some_cloudwatch_log_group_name",
  "logStream": "i-0221b6ec01af47bfb",
  "subscriptionFilters": [
    "cloudwatch_log_subscription_filter_name"
  ],
  "logEvents": [
    {
      "id": "33633929427703365813575134502195362621356131219229245440",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_1"
    },
    {
      "id": "33633929427703365813575134502195362621356131219229245441",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_2"
    },
    {
      "id": "33633929427703365813575134502195362621356131219229245442",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_3"
    }
  ]
}
</code>```

Individual item from .logEvents[] gets transformed into a json structure where the keys are the desired columns when searching logs within Kibana - something like this:

```<code>{
    'journalctl_host': 'ip-172-11-11-111',
    'process': 'haproxy',
    'pid': 15507,
    'client_ip': '172.11.11.111',
    'client_port': 3924,
    'frontend_name': 'http-web',
    'backend_name': 'server',
    'server_name': 'server-3',
    'time_duration': 10,
    'status_code': 200,
    'bytes_read': 79,
    '@timestamp': '1900-10-16T23:46:01.0Z',
    'tags': ['haproxy'],
    'message': 'HEAD / HTTP/1.1'
}
</code>```

The transformed json gets collected into an array which gets zlib compressed and base64 encoded string which is then transformed into a new json payload as the final lambda result:

```<code>{
"records": [
    {
        "recordId": "49577998431243709525183749876652374166077260049460232194000000",
        "result": "Ok",
        "data": "base64_encoded_zlib_compressed_array_of_transformed_logs"
    }
]}
</code>```

Cloudwatch configuration

13 log entries (~4kb) can get transformed to about 635kb.

I have also decreased the thresholds for the awslogs, hoping that the size of the logs that are being sent to Lambda function is going to small:

```<code>buffer_duration = 10
batch_count = 10
batch_size = 500
</code>```

Unfortunately, when there is a burst - the spike can be upwards of 2800 lines where the size is upwards of 1MB.

When the resulting payload from the lambda function is "too big" (~13mb of transformed logs), an error is logged in the lambda cloudwatch logs - "body size is too long". There doesn't seem to be any indication where this error is coming from or whether there is a size limit on the lambda fn's response payload.

答:
So, the AWS support folks have told me that the following limitations can't be mitigated to solve this flow:

lambda payload size

compressed firehose payload incoming into lambda which is directly proportional to the lambda output.

Instead, I have modified the architecture to the following:

Cloudwatch logs are backed up in S3 via Firehose.

S3 events are processed by the lambda function.

The lambda function returns a success code if the lambda transforms and is able to successfully bulk index the logs into ES.

If the lambda function fails, a Dead Letter Queue (AWS SQS) is configured with a cloudwatch alarm. A sample cloudformation snippet can be found here.

If there are SQS messages, one could manually invoke the lambda function with those messages or set up a AWS batch job to process the SQS messages with the lambda function. However, one should be careful, that the lambda function doesn't failover again into the DLQ. Check the lambda cloudwatch logs to check why that message was not processed and sent over to the DLQ.

Body:
TLDR

The lambda function is not able to index the firehose logs into the AWS managed ES due to an "encoding problem".

Actual Error Response

I do not get any error when I base64 encode a single logEvent from a firehose record and send the collected records to the AWS managed ES.

See the next section for more details.

The base 64 encoded compressed payload is being sent to ES as the resulting json transformation is too big for ES to index - see this ES link.

I get the following error from the AWS managed ES:

{
    "deliveryStreamARN": "arn:aws:firehose:us-west-2:*:deliverystream/*",
    "destination": "arn:aws:es:us-west-2:*:domain/*",
    "deliveryStreamVersionId": 1,
    "message": "The data could not be decoded as UTF-8",
    "errorCode": "InvalidEncodingException",
    "processor": "arn:aws:lambda:us-west-2:*:function:*"
  }

If the output record is not compressed, the body size is too long (as small as 14MB). Without compression and a simple base64 encoded payload, I get the following error in the Lambda logs:

{
  "type": "mapper_parsing_exception",
  "reason": "failed to parse",
  "caused_by": {
    "type": "not_x_content_exception",
    "reason": "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
  }
}

Description

I have Cloudwatch logs that are getting buffered by size / interval which gets fed into a Kinesis Firehose. The firehose transports the logs into a lambda function which transforms the log into a json record which should then send it over to the AWS managed Elasticsearch cluster.

The lambda function gets the following JSON structure:

{
    "invocationId": "cf1306b5-2d3c-4886-b7be-b5bcf0a66ef3",
    "deliveryStreamArn": "arn:aws:firehose:...",
    "region": "us-west-2",
    "records": [{
        "recordId": "49577998431243709525183749876652374166077260049460232194000000",
        "approximateArrivalTimestamp": 1508197563377,
        "data": "some_compressed_data_in_base_64_encoding"
    }]
}

The lambda function then extracts .records[].data and decodes the data as base64 and decompresses the data which results in the following JSON:

{
  "messageType": "DATA_MESSAGE",
  "owner": "aws_account_number",
  "logGroup": "some_cloudwatch_log_group_name",
  "logStream": "i-0221b6ec01af47bfb",
  "subscriptionFilters": [
    "cloudwatch_log_subscription_filter_name"
  ],
  "logEvents": [
    {
      "id": "33633929427703365813575134502195362621356131219229245440",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_1"
    },
    {
      "id": "33633929427703365813575134502195362621356131219229245441",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_2"
    },
    {
      "id": "33633929427703365813575134502195362621356131219229245442",
      "timestamp": 1508197557000,
      "message": "Oct 16 23:45:57 some_log_entry_3"
    }
  ]
}

Individual item from .logEvents[] gets transformed into a json structure where the keys are the desired columns when searching logs within Kibana - something like this:

{
    'journalctl_host': 'ip-172-11-11-111',
    'process': 'haproxy',
    'pid': 15507,
    'client_ip': '172.11.11.111',
    'client_port': 3924,
    'frontend_name': 'http-web',
    'backend_name': 'server',
    'server_name': 'server-3',
    'time_duration': 10,
    'status_code': 200,
    'bytes_read': 79,
    '@timestamp': '1900-10-16T23:46:01.0Z',
    'tags': ['haproxy'],
    'message': 'HEAD / HTTP/1.1'
}

The transformed json gets collected into an array which gets zlib compressed and base64 encoded string which is then transformed into a new json payload as the final lambda result:

{
"records": [
    {
        "recordId": "49577998431243709525183749876652374166077260049460232194000000",
        "result": "Ok",
        "data": "base64_encoded_zlib_compressed_array_of_transformed_logs"
    }
]}

Cloudwatch configuration

13 log entries (~4kb) can get transformed to about 635kb.

I have also decreased the thresholds for the awslogs, hoping that the size of the logs that are being sent to Lambda function is going to small:

buffer_duration = 10
batch_count = 10
batch_size = 500

Unfortunately, when there is a burst - the spike can be upwards of 2800 lines where the size is upwards of 1MB.

When the resulting payload from the lambda function is "too big" (~13mb of transformed logs), an error is logged in the lambda cloudwatch logs - "body size is too long". There doesn't seem to be any indication where this error is coming from or whether there is a size limit on the lambda fn's response payload.


问:
Regex delete duplicate urls
I have a list with several urls like

```<code>google.com
google.com/1
google.com/2
google.com/3
google.com/4
google.com/5
google.com/6
yahoo.com
yahoo.com/1
yahoo.com/2
yahoo.com/3
yahoo.com/4
yahoo.com/5
yahoo.com/6
</code>```

How can I remove the first the 3 entries keeping google.com/3 to 6 and same goes for yahoo?

答:
In C#:

```<code>resultString = Regex.Replace(subjectString,
    @"^        # Start at the start of a line
    [^/\r\n]+  # Match one or more characters except /
    $          # Match the end of the line, thereby ensuring that
               # the entire line does not contain a /
    (?:        # Match the following group:
     \r\n      # - a linebreak
     .*        # - an entire line
    ){2}       # exactly twice
    \r\n       # Match the final line break",
    "", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
</code>```

Resulting string:

```<code>google.com/3
google.com/4
google.com/5
google.com/6
yahoo.com/3
yahoo.com/4
yahoo.com/5
yahoo.com/6
</code>```

Title: Regex delete duplicate urls

Body:
I have a list with several urls like

google.com
google.com/1
google.com/2
google.com/3
google.com/4
google.com/5
google.com/6
yahoo.com
yahoo.com/1
yahoo.com/2
yahoo.com/3
yahoo.com/4
yahoo.com/5
yahoo.com/6

How can I remove the first the 3 entries keeping google.com/3 to 6 and same goes for yahoo?

Body:
In C#:

resultString = Regex.Replace(subjectString,
    @"^        # Start at the start of a line
    [^/\r\n]+  # Match one or more characters except /
    $          # Match the end of the line, thereby ensuring that
               # the entire line does not contain a /
    (?:        # Match the following group:
     \r\n      # - a linebreak
     .*        # - an entire line
    ){2}       # exactly twice
    \r\n       # Match the final line break",
    "", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

Resulting string:

google.com/3
google.com/4
google.com/5
google.com/6
yahoo.com/3
yahoo.com/4
yahoo.com/5
yahoo.com/6


问:
Correct way to queue items for a worker goroutine?
I am writing an app that allows users to upload files and queue them for processing. My approach to this is to create a goroutine for processing uploaded files and to use a channel to signal that a new file is ready for processing.

Basically, the processing goroutine does this:

```<code>for {
    while itemForProcessing() {
        processNextItem()
    }
    select {
    case <-signalChan:
    case <-stopChan:
        return
    }
}
</code>```

The code for signaling that a new item is ready for processing looks something like this:

```<code>select {
case signalChan <- true:
default:
}
</code>```

Note that this is a non-blocking send on the channel.

There are three possible scenarios:

the processing goroutine is in the select{} block — the first case runs and the next item is processed

the processing goroutine is executing processNextItem() — the next item will be processed once processNextItem() returns since the loop condition will be true

the processing goroutine has exited the loop but has not yet entered the select{} block

The last scenario will result in a problem. The non-blocking send will not send anything on the channel and the goroutine will wait in the select{} block until something else happens.

How can I avoid this problem? I cannot use a blocking send because the processing goroutine may be running processNextItem(), which would cause the send to block for an extremely long period of time.

答: To avoid a lost signal, use a channel with capacity 1.</p>

Body:
I am writing an app that allows users to upload files and queue them for processing. My approach to this is to create a goroutine for processing uploaded files and to use a channel to signal that a new file is ready for processing.

Basically, the processing goroutine does this:

for {
    while itemForProcessing() {
        processNextItem()
    }
    select {
    case <-signalChan:
    case <-stopChan:
        return
    }
}

The code for signaling that a new item is ready for processing looks something like this:

select {
case signalChan <- true:
default:
}

Note that this is a non-blocking send on the channel.

There are three possible scenarios:

the processing goroutine is in the select{} block — the first case runs and the next item is processed

the processing goroutine is executing processNextItem() — the next item will be processed once processNextItem() returns since the loop condition will be true

the processing goroutine has exited the loop but has not yet entered the select{} block

The last scenario will result in a problem. The non-blocking send will not send anything on the channel and the goroutine will wait in the select{} block until something else happens.

How can I avoid this problem? I cannot use a blocking send because the processing goroutine may be running processNextItem(), which would cause the send to block for an extremely long period of time.


问:
TextView - setting the text size programmatically doesn't seem to work
I am using Eclipse Indigo, testing on 2 emulators(2.2 and 3.0).

the code below shows what I am testing now, however setting the text size reveals nothing on the screen when trying to run the emulator.(if i comment out the text size the text shows up with a red color). I thought that somehow eclipse wasn't rebuilding the code but i added the line of code to add the blue background and that worked. I have tried setting the text size after setting the text with still no success. the code is below. thanks for your help!
(disclaimer) - i am trying to stay away from xml. Being that i already know java i don't want to depend on that.

```<code>import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TestAndroidvs2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView text = new TextView(this);
    text.setTextColor(Color.RED);
    text.setTextSize(2);
    text.setBackgroundColor(Color.BLUE);
    text.setText("Hello Android");

    setContentView(text);
  }
}
</code>```

答:
Currently, setTextSize(float size) method will work well so we don't need to use another method for change text size

android.widget.TextView.java source code

```<code>/**
 * Set the default text size to the given value, interpreted as "scaled
 * pixel" units.  This size is adjusted based on the current density and
 * user font size preference.
 *
 * <p>Note: if this TextView has the auto-size feature enabled than this function is no-op.
 *
 * @param size The scaled pixel size.
 *
 * @attr ref android.R.styleable#TextView_textSize
 */
@android.view.RemotableViewMethod
public void setTextSize(float size) {
    setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
</code>```

Example using

```<code>textView.setTextSize(20); // set your text size = 20sp
</code>```

Body:
I am using Eclipse Indigo, testing on 2 emulators(2.2 and 3.0).

the code below shows what I am testing now, however setting the text size reveals nothing on the screen when trying to run the emulator.(if i comment out the text size the text shows up with a red color). I thought that somehow eclipse wasn't rebuilding the code but i added the line of code to add the blue background and that worked. I have tried setting the text size after setting the text with still no success. the code is below. thanks for your help!
(disclaimer) - i am trying to stay away from xml. Being that i already know java i don't want to depend on that.

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TestAndroidvs2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView text = new TextView(this);
    text.setTextColor(Color.RED);
    text.setTextSize(2);
    text.setBackgroundColor(Color.BLUE);
    text.setText("Hello Android");

    setContentView(text);
  }
}

Body:
Currently, setTextSize(float size) method will work well so we don't need to use another method for change text size

android.widget.TextView.java source code

/**
 * Set the default text size to the given value, interpreted as "scaled
 * pixel" units.  This size is adjusted based on the current density and
 * user font size preference.
 *
 * <p>Note: if this TextView has the auto-size feature enabled than this function is no-op.
 *
 * @param size The scaled pixel size.
 *
 * @attr ref android.R.styleable#TextView_textSize
 */
@android.view.RemotableViewMethod
public void setTextSize(float size) {
    setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

Example using

textView.setTextSize(20); // set your text size = 20sp


问:
Is decrypting and setting environment variables inside an AWS Lambda function a vulnerability?
Inside of AWS Lambda functions I have some libs that look for sensitive information in the os environment. I can encrypt the env vars using KMS, but I've found myself having to overwrite the encrypted env vars in the lambda handler module -- is this a vulnerability? Eg.

```<code># lambda_handler.py

encrypted_env_var = os.environ["SECRET_KEY"]
decrypted_env_var = decrypt(encrypted_env_var)
os.environ["SECRET_KEY"] = decrypted_env_var

def lambda_function(event, context):
    ... libs get and use SECRET_KEY ...
</code>```

I understand that encrypting them covers you eg. when using awscli, but could setting this in the container be a vulnerability? As I understand from here, the container may not be destroyed immediately.

Furthermore, in the suggested decryption code snippet that AWS gives you (in the lambda dashboard), the comments caught my attention:

```<code># lambda_handler.py

ENCRYPTED = os.environ['SECRET_KEY']
# Decrypt code should run once and variables stored outside of the function
# handler so that these are decrypted once per container
DECRYPTED = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED))['Plaintext']

def lambda_handler(event, context):
  # handle the event here
</code>```

Would it be sufficient (albeit messy) to just unset the relevant variables at the end of the function?

Thanks

答:
the container may not be destroyed immediately.

The container almost certainly will not be destroyed immediately... but that isn't bad.

Containers can persist for minutes to hours, and are reused -- this is why Lambda functions are usually able to execute so quickly, because under ideal conditions, the majority of your function invocations will find an idle container that they can reuse.

However... containers are reused only by one single version of one single function of yours.  Nobody else, and no other functions or different versions of the same function, even from the same account, reuse your containers.  Only the function version that caused the container to be created.

The infrastructure destroys containers that are no longer needed, and it does this based on the traffic your function is seeing (or not seeing).  Anecdotal observations suggest that all idle containers will disappear completely after 10-15 minutes if you completely stop invoking your function, but this is not documented and may vary.

the comments caught my attention

The comments are saying that you should store the decrypted variables outside the handler function so they are in a global scope -- it would be very inefficient to call the KMS API to decrypt the variables with each invocation of the function.  You could conceivably do that, but you should store the encrypted environment variables under a different name, and then set the decrypted values into the names your code expects.

Would it be sufficient (albeit messy) to just unset the relevant variables at the end of the function?

That won't work, unless you store the original encrypted values elsewhere (such as using different initial environment variable names), because your example code would overwrite the encrypted values, and the next time your function runs in that container, it would fail to decrypt those unset values.

The point of encrypted environment variables is that while they are stored at rest by the Lambda infrastructure, they are encrypted.  Once they are in the memory space of your container, there is not a meaningful chance of them being compromised, since (as noted above) those containers are exclusive to your function.

Body:
Inside of AWS Lambda functions I have some libs that look for sensitive information in the os environment. I can encrypt the env vars using KMS, but I've found myself having to overwrite the encrypted env vars in the lambda handler module -- is this a vulnerability? Eg.

# lambda_handler.py

encrypted_env_var = os.environ["SECRET_KEY"]
decrypted_env_var = decrypt(encrypted_env_var)
os.environ["SECRET_KEY"] = decrypted_env_var

def lambda_function(event, context):
    ... libs get and use SECRET_KEY ...

I understand that encrypting them covers you eg. when using awscli, but could setting this in the container be a vulnerability? As I understand from here, the container may not be destroyed immediately.

Furthermore, in the suggested decryption code snippet that AWS gives you (in the lambda dashboard), the comments caught my attention:

# lambda_handler.py

ENCRYPTED = os.environ['SECRET_KEY']
# Decrypt code should run once and variables stored outside of the function
# handler so that these are decrypted once per container
DECRYPTED = boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED))['Plaintext']

def lambda_handler(event, context):
  # handle the event here

Would it be sufficient (albeit messy) to just unset the relevant variables at the end of the function?

Thanks


问:
g++.exe: error: libgomp-1.dll: No such file or directory; make: ***[Out-release] Error 1
I am trying to build a ReformAlign executable from a makefile.  I am using MINGW-64.  However I get the error message
g++.exe: error: libgomp-1.dll: No such file or directory
make: ***[Out-release] Error 1
what am I missing</p>

答:
I had to copy the following dlls:
1. libgcc_s_dw2-1.dll
2. libgomp-1.dll
3. libstdc++-6.dll
4. pthreadGC2.dll
from my compiler to the folder containing the makefile to generate the executable.

Thank you for the help and suggestions.